Advertisement
193030

Operator overloading

Feb 24th, 2021
853
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6. private:
  7.     int real;
  8.     int img;
  9. public:
  10.     Complex(int r = 0, int i = 0)
  11.     {
  12.         real = r;
  13.         img = i;
  14.     }
  15.     void display()
  16.     {
  17.         cout << real << " +i" << img << endl;
  18.     }
  19.     Complex operator+(Complex c)
  20.     {
  21.         Complex temp;
  22.         temp.real = real + c.real;
  23.         temp.img = img + c.img;
  24.         return temp;
  25.     }
  26. };
  27. int main()
  28. {
  29.     Complex c1(5, 3), c2(10, 5), c3;
  30.  
  31.     c3 = c1 + c2;
  32.  
  33.     c3.display();
  34. }
  35.  
Advertisement
RAW Paste Data Copied
Advertisement