bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_2 / eduincs / pgrm_021.cpp

pgrm_021.cpp (900B)


      1 /* operator overloading using friend function */
      2 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 class Complex {
      7 private:
      8     int real, img;
      9 
     10 public:
     11     Complex (int r = 0, int i = 0) {
     12         real = r; img = i;
     13     }
     14 
     15     friend Complex operator + (const Complex &obj1, const Complex &obj2);
     16 
     17     void output() {
     18         cout << "Complex number: " << real << " + " << img << "i";
     19     }
     20 };
     21 
     22 Complex operator + (const Complex &obj1, const Complex &obj2) {
     23     Complex temp;
     24     temp.real = obj1.real + obj2.real;
     25     temp.img = obj1.img + obj2.img;
     26     return temp;
     27 }
     28 
     29 int main() {
     30     Complex complex1(10, 20), complex2(20, 30), result;
     31     cout << "First complex number: " << endl;
     32     complex1.output();
     33     cout << endl << "Second complex number: " << endl;
     34     complex2.output();
     35     cout << endl << "After Addition: " << endl;
     36     result = complex1 + complex2;
     37     result.output();
     38     return 0;
     39 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror