Answer to Question #179861 in C++ for yuuhij

Question #179861

create a calculator for a complex number by creating a class of complex number with overloading all operators in it.(operators:++,--,+,-,/,*,>>,<<).

1
Expert's answer
2021-04-10T01:13:14-0400
#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;
}

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS