Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code

Operator overloading

Operator overloading in C++ allows you to define how operators behave when applied to objects of user-defined classes. This feature enables you to use standard operators like +, -, *, /, =, etc., with your custom types, making your code more intuitive and readable.

Here's an example that demonstrates operator overloading:

#include <iostream>

class Complex {
private:
    double real;
    double imag;

public:
    // Constructor
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // Overload the + operator
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }

    // Overload the * operator
    Complex operator*(const Complex& other) const {
        return Complex(real * other.real - imag * other.imag,
                       real * other.imag + imag * other.real);
    }

    // Overload the == operator
    bool operator==(const Complex& other) const {
        return (real == other.real) && (imag == other.imag);
    }

    // Overload the << operator for easy output
    friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
        os << c.real;
        if (c.imag >= 0) os << "+";
        os << c.imag << "i";
        return os;
    }
};

int main() {
    Complex a(1.0, 2.0);
    Complex b(3.0, 4.0);

    Complex sum = a + b;
    Complex product = a * b;

    std::cout << "a = " << a << std::endl;
    std::cout << "b = " << b << std::endl;
    std::cout << "a + b = " << sum << std::endl;
    std::cout << "a * b = " << product << std::endl;
    std::cout << "a == b: " << (a == b) << std::endl;

    return 0;
}

In this example, we've created a Complex class to represent complex numbers and overloaded several operators:

  1. + operator: This allows us to add two Complex numbers.

    Complex operator+(const Complex& other) const { ... }
  2. * operator: This enables multiplication of Complex numbers.

    Complex operator*(const Complex& other) const { ... }
  3. == operator: This allows comparison of Complex numbers.

    bool operator==(const Complex& other) const { ... }
  4. << operator: This is overloaded as a friend function to allow easy output of Complex numbers.

    friend std::ostream& operator<<(std::ostream& os, const Complex& c) { ... }

Key points about operator overloading:

  1. You can overload most built-in operators in C++, but not all (e.g., ., ::).

  2. Operator overloading doesn't change the precedence or associativity of operators.

  3. You can overload operators as member functions or as non-member functions.

  4. Some operators (like =, [], (), ->) must be overloaded as member functions.

  5. The friend keyword is often used with operator overloading to give the operator function access to private members of the class.

When you run this program, it will output:

Copya = 1+2i
b = 3+4i
a + b = 4+6i
a * b = -5+10i
a == b: 0

This demonstrates how we can now use standard operators with our custom Complex class, making the code more intuitive and closer to mathematical notation.

PreviousCastingNextAvailable Operators

Last updated 9 months ago

Run it .

Try it .

🔃
here
here