Develop a C++ program to Subtract two complex number by overloading - operator using Friend function.
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
private:
double _re, _im;
public:
double GetReal()const
{
return _re;
}
double GetImagine()const
{
return _im;
}
Complex ()
{
_re = 0;
_im = 0;
};
Complex (double r, double i)
{
_re = r;
_im = i;
}
friend Complex operator-(Complex c1, Complex c2);
};
Complex operator-(Complex c1, Complex c2)
{
Complex c3;
c3._re = c1._re - c2._re;
c3._im = c1._im - c2._im;
return c3;
}
int
main ()
{
Complex a (5, 2);
Complex b (3, -3);
Complex c = a - b;
cout << "Your complex number: " << c.GetReal() << " + " << c.GetImagine() << "i";
return 0;
}
Comments
Leave a comment