Skip to content
doubtly

1 answer

Best answer

#include <iostream>

using namespace std;

class Complex {
 private:
 double real;
 double imag;

 public:
 Complex() {
 real = 0;
 imag = 0;
 }

 Complex(double r, double i) {
 real = r;
 imag = i;
 }

 // overload binary + operator
 Complex operator+(Complex const &obj) {
 Complex res;
 res.real = real + obj.real;
 res.imag = imag + obj.imag;
 return res;
 }

 // display complex number
 void display() {
 cout << real << " + " << imag << "i" << endl;
 }
};

int main() {
 Complex c1(3, 4);
 Complex c2(5, 6);

 Complex sum = c1 + c2;

 cout << "c1 = ";
 c1.display();

 cout << "c2 = ";
 c2.display();

 cout << "Sum = ";
 sum.display();

 return 0;
}
image
Doubtly · 19 Mar 2023 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.