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.
#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;
}
Comments
You are not using friend function its just operator overloading.
Leave a comment