tampurus

10 operator overloading ( operator+ )

Dec 22nd, 2021 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. //operator+
  2.  
  3. // how to do with no operator+
  4. #include <iostream>
  5. using namespace std;
  6. class complex
  7. {
  8. private:
  9.     int real;
  10.     int img;
  11. public:
  12.     complex(int a=0 , int b=0 ){ // non parameterized or default constructor
  13.         real = a;
  14.         img = b;
  15.     }
  16.     complex add(complex x){
  17.         complex temp; // creating new object, if no value passed defaulf it will be zero
  18.         temp.real = real + x.real;
  19.         temp.img = img + x.img;
  20.         return temp;
  21.     }
  22.     void show(){
  23.         cout<<real<<" + i"<<img<<endl;
  24.     }
  25. };
  26. int main()
  27. {
  28.     complex c1(8,5) , c2(1,7);
  29.     complex c3 = c1.add(c2); // complex c3 = c2.add(c1);
  30.     c1.show();
  31.     c2.show();
  32.     c3.show();
  33.     return 0;
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. // if you want to do same thing as above code has done using opertor+ see below code
  41. #include <iostream>
  42. using namespace std;
  43. class complex
  44. {
  45. private:
  46.     int real;
  47.     int img;
  48. public:
  49.     complex(int a=0 , int b=0 ){ // non parameterized or default constructor
  50.         real = a;
  51.         img = b;
  52.     }
  53.     complex operator+(complex x){
  54.         complex temp; // creating new object, if no value passed defaulf it will be zero
  55.         temp.real = real + x.real;
  56.         temp.img = img + x.img;
  57.         return temp;
  58.     }
  59.     void show(){
  60.         cout<<real<<" + i"<<img<<endl;
  61.     }
  62. };
  63. int main()
  64. {
  65.     complex c1(8,5) , c2(1,7);
  66.     complex c3 = c1 + c2;
  67.     c1.show();
  68.     c2.show();
  69.     c3.show();
  70.     return 0;
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. // same operator+ but with friend function
  80. #include <iostream>
  81. using namespace std;
  82. class complex
  83. {
  84. private:
  85.     int real;
  86.     int img;
  87. public:
  88.     complex(int a=0 , int b=0 ){ // non parameterized or default constructor
  89.         real = a;
  90.         img = b;
  91.     }
  92.     friend complex operator+(complex x , complex y);
  93.     void show(){
  94.         cout<<real<<" + i"<<img<<endl;
  95.     }
  96. };
  97. complex operator+(complex x , complex y){
  98.     complex temp;     // creating new object, if no value passed defaulf it will be zero
  99.     temp.real = x.real + y.real;
  100.     temp.img = x.img + y.img;
  101.     return temp;
  102. }
  103. int main()
  104. {
  105.     complex c1(8,5) , c2(1,7);
  106.     complex c3 = c1 + c2; // complex c3 = c2.add(c1);
  107.     c1.show();
  108.     c2.show();
  109.     c3.show();
  110.     return 0;
  111. }
  112.  
  113. /*
  114. Output
  115. 8 + i5
  116. 1 + i7
  117. 9 + i12
  118. */
Add Comment
Please, Sign In to add comment