menu search
brightness_auto
more_vert
Write a program in C++ to overload binary + operator for addition of complex numbers?
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

1 Answer

more_vert
 
verified
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
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

Related questions

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 1 like thumb_down_off_alt 0 dislike
0 answers
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

648 users

...