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
//Complex.h file
// Fig. 10.14: Complex.h
 // Complex class definition.
 #include<iostream>
 using namespace std;
 #ifndef COMPLEX_H
 #define COMPLEX_H
class Complex
 {
 public:
 explicit Complex( double = 0.0, double = 0.0 ); // constructor
 Complex operator+( const Complex & ) const; // addition
 Complex operator-( const Complex & ) const; // subtraction
 friend ostream& operator<<( ostream &out,const Complex & ) ; // output
 bool operator==( const Complex &) ;//overload equal operator
 Complex& operator--() ;//overload equal operator
 void print() const; // output
 private:
 double real; // real part
 double imaginary; // imaginary part
 }; // end class Complex
#endif
------------------------------------------------------------------------------------------------------------------------------
//Complex.cpp
//...........................................
 // Fig. 10.15: Complex.cpp
 // Complex class member-function definitions.
 #include <iostream>
 #include \"Complex.h\" // Complex class definition
 using namespace std;
// Constructor
 Complex::Complex( double realPart, double imaginaryPart )
 : real( realPart ),
 imaginary( imaginaryPart )
 {
 // empty body
    real = realPart; // real part
 imaginary = imaginaryPart; // imaginary part
 } // end Complex constructor
// addition operator
 Complex Complex::operator+( const Complex &operand2 ) const
 {
 return Complex( real + operand2.real,
 imaginary + operand2.imaginary );
 } // end function operator+
// subtraction operator
 Complex Complex::operator-( const Complex &operand2 ) const
 {
 return Complex( real - operand2.real,
 imaginary - operand2.imaginary );
 } // end function operator-
// display a Complex object in the form: (a, b)
 void Complex::print() const
 {
 cout << \'(\' << real << \", \" << imaginary << \')\'<<endl;
 } // end function print
ostream& operator<<( ostream &out,const Complex &r )// output
 {
    out<<r.real<<\"+\"<<r.imaginary<<\"i\"<<endl;
     return out;
}
 bool Complex:: operator==( const Complex &r2 ) //overload equal operator
 {
   
    if((real != r2.real) || (imaginary!=r2.imaginary))
   
        return false;
   
   return true;
   
 }
 Complex& Complex:: operator-- ()
 {
 --real;
    --imaginary;
    return *this;
 }
---------------------------------------------------------------------------------------------------------------------------
//main.cpp
#include \"Complex.h\"
//.......................................
 // Fig. 10.16: fig10_16.cpp
 // Complex class test program.
 #include <iostream>
 #include \"Complex.h\"
 using namespace std;
int main()
 {
 Complex x;
 Complex y( 4.3, 8.2 );
 Complex z( 3.3, 1.1 );
 Complex a( 4.3, 8.2 );
 Complex b(4.3, 8.2 );
cout << \"x: \";
 x.print();
 cout << \"\ y: \";
 y.print();
 cout << \"\ z: \";
 z.print();
x = y + z;
 cout << \"\ \ x = y + z:\" << endl;
 x.print();
 //check for output overloading <<
 cout<<\"check for overloading << operator\"<<endl;
 cout<<x;
 cout << \" = \";
 y.print();
 cout<<y;
 cout << \" + \";
 z.print();
x = y - z;
 cout << \"\ \ x = y - z:\" << endl;
 x.print();
 cout << \" = \";
 y.print();
 cout << \" - \";
 z.print();
 //check for overloading -- operator
 cout<<\"check for overloading -- operator\"<<endl;
 --z;
 cout<<z;
 //check for == operator overloaing
 cout<<\"check for overloading == operator\"<<endl;
 if( a == b)
    cout<<\" Complex numbers \"<<a<<\"and\"<<b<<\"are equal \"<<endl;
 else
    cout<<\" Complex numbers \"<<a<<\"and\"<<b<<\"are not equal \"<<endl;
 cout << endl;
 } // end main



