Consider class Complex given in Figs 10141016 these figs are

Consider class Complex given in Figs. 10.14–10.16 (these figs. are in S:\\cst3503\\Assign\\). The class has two private data members, real and imaginary.
Overload the << operator to enable output of data members of complex numbers. Make the overloaded function a friend of class Complex.
Overload the == operator. The function compares two complex objects. It returns true if two objects are equal and false otherwise.
It first checks whether real data member of two complex objects are equal or not.
If real data members of two objects are equal, then it checks whether imaginary data member of two objects are equal or not.
Overload -- operator that will decrement the real data member of a complex object by 1.
Test the overloaded <<, == and -- operator by using them in fig 10.16

Solution

#include <iostream>
using namespace std;
class Complex
{
    private:
      float real;
      float imag;
    public:
       Complex(): real(0), imag(0){ }
       void input()
       {
           cout << \"Enter real and imaginary parts respectively: \";
           cin >> real;
           cin >> imag;
       }
       // Operator overloading
       Complex operator - (Complex c2)
       {
           Complex temp;
           temp.real = real - c2.real;
           temp.imag = imag - c2.imag;

           return temp;
       }
       Complex operator -- (int)
       {
           Complex temp;
           temp.real = real - 1;
           temp.imag = imag;
           return temp;
       }
       int operator==(Complex c2)
       {
           if(real == c2.real)
                if(imag == c2.imag)
                        return 1;
           return 0;   
       }
       void output()
       {
           if(imag < 0)
               cout << \"Output Complex number: \"<< real << imag << \"i\";
           else
               cout << \"Output Complex number: \" << real << \"+\" << imag << \"i\";
       }
       friend std::ostream &operator<< (std::ostream &output, Complex &c);
};
std::ostream &operator<< (std::ostream &output, Complex &c){
       output << c.real << \"+\" << c.imag << \"i\ \";
       return output;
}
int main()
{
        Complex c1, c2, result;
        cout<<\"Enter first complex number:\ \";
        c1.input();
        cout<<\"Enter second complex number:\ \";
        c2.input();
        cout<<\"First Complex Number\ \"<<c1;
        cout<<\"Second Complex Number\ \"<<c2;
        if(c1==c2)
                cout<<\"Both Numbers are Equal\ \";
        else
                cout<<\"Both Numbers are not Equal\ \";
        // Decrement Opeartor...      
        result = c1--;                                  //Post Decrement
        cout<<\"Decrement the first complex number:\"<<result;
        return 0;
}

Consider class Complex given in Figs. 10.14–10.16 (these figs. are in S:\\cst3503\\Assign\\). The class has two private data members, real and imaginary. Overlo
Consider class Complex given in Figs. 10.14–10.16 (these figs. are in S:\\cst3503\\Assign\\). The class has two private data members, real and imaginary. Overlo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site