create a calculator for a complex number by creating a class of complex number with overloading all operators in it.(operators:++,--,+,-,/,*,>>,<<).
#include<iostream>
using namespace std;
class complexNumber
{
private:
double realPart;
double imaginaryPart;
public:
complexNumber(){realPart=0.0; imaginaryPart=0.0;}
~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}
double get_real(){return realPart;}
double get_imaginary(){return imaginaryPart;}
void set_real(double r){realPart=r;}
void set_imaginary(double i){imaginaryPart=i;}
friend ostream &operator<<( ostream &output, complexNumber &c )
{
return output << c.realPart << " + " << c.imaginaryPart <<"i";
}
complexNumber operator+(complexNumber c)
{
complexNumber temp;
double temp_realPart,temp_imaginaryPart;
temp_realPart= realPart + c.get_real();
temp_imaginaryPart = imaginaryPart + c.get_imaginary();
temp.set_real(temp_realPart);
temp.set_imaginary(temp_imaginaryPart);
return temp;
}
// complexNumber operator-(const complexNumber& c);
};
int main()
{
complexNumber c1,c2,c3;
c1.set_real(3.4);
c1.set_imaginary(4.5);
c2.set_real(5.6);
c2.set_imaginary(6.2);
c3= c1 + c2;
cout<< c3<<endl;
cin.get();
return 0;
}
Comments
Leave a comment