write a program to overload '-' operator to find difference of two complex object
/* program in C++ to overload '-' operator to find difference of two complex object */
#include<iostream>
using namespace std;
class Complex{
public:
float a, b;
complex(): a(0), b(0) {}
complex(float x, float y): a(x), b(y){}
void display(){
cout<<this->a<<"+"<<this->b<<"i"<<endl;
}
friend Complex operator-(const Complex&, const Complex&);
};
complex operator-(const Complex& com, const Complex& comp){
float x= com.a - comp.a;
foat y= com.b - comp.b;
return Complex(x,y);
}
int main(){
Complex a(1,7), b(6,9);
cout<<"A = ";a.display();
cout<<"B = ";b.display();
cout<<"A - B = ";(a-b).display();
cout<<"B - A = ";(b-a).display();
return 0;
}
No comments:
Post a Comment
If you have any doubts, please let me know