Answer to Question #206062 in C++ for Ahmad

Question #206062

Create a class 'COMPLEX' to hold a complex number. Write a friend function to add two

complex numbers. Write a main function to add two COMPLEX objects.


1
Expert's answer
2021-06-12T06:13:42-0400
#include <iostream>


using namespace std;


class Complex
{
	private:
		float real;
		float img;
	public:
	    Complex(){
	  
	    }
		Complex(float r, float i){
			real = r;
			img = i;
		}
		friend Complex addComplex(Complex c1, Complex c2); 
		void display(){
			cout<<real<<","<<img<<endl;
		}
};


Complex addComplex(Complex c1, Complex c2){
	Complex t;
	t.real = c1.real + c2.real;
	t.img = c1.img + c2.img;
	return t;
}
int main(){
	Complex c1(9.7, 6.8), c2(4.9,5.1), c3;
	c3 = addComplex(c1, c2);
	c1.display();
	c2.display();
	cout<<"Sum is :"<<endl;
	c3.display();
	return 0;
}

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

Ahmad
12.06.21, 03:48

You are not using friend function its just operator overloading.

Leave a comment

LATEST TUTORIALS
New on Blog