Answer to Question #247763 in C++ for Elena

Question #247763
2 Write a program to perform addition of two complex numbers using
constructor overloading.The first constructor which takes no argument is
used to create objects which are not initialized, second which takes one
argument is used to initialize real and imaginary parts to equal values and
third which takes two argument is used to initialize real and imaginary to
two different values.
1
Expert's answer
2021-10-07T03:04:56-0400
#include<iostream>
using namespace std;
class Complex{
	private:
	int imag, real;
	public:
		Complex(){
			
		}
		Complex(int r){
			imag = r;
			real = r;
		}
		
		Complex(int i, int r){
			imag = r;
			real = i;
		}
		
		Complex add(Complex a, Complex b){
			Complex c;
			c.imag = a.imag + b.imag;
			c.real = a.real + b.real;
			return c;
		}
		void display(){
			cout<<real<< "+"<<imag<<"i"<<endl;
		}
};
int main(){
	Complex a(2,3), d;
	Complex b(3,4), c;
	c = d.add(a,b);
	c.display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS