// has relational operators // has [] for const and non-const // has friends // calls to input operator are commented out so we can run to disk #include using std::cin; using std::cout; using std::endl; using std::boolalpha; using std::istream; using std::ostream; class complex { friend ostream & operator<< (ostream &, const complex &); // output operator friend istream & operator>> (istream &, complex &); // input operator private: double real; double imag; public: complex(double r=0, double i=0); // constructor w/ default values complex operator+ (const complex&) const; // addition operator bool operator== (const complex&) const; // equality operator complex& operator++(); // pre-increment operator double& operator[] (const int index); // [] for non-const (both sides) const double& operator[] (const int index) const; // [] for const (RHS only) const double get_real() const; // accessor for real const double get_imag() const; // accessor for imag }; // *********************************** int main() { bool check_equality; cout << boolalpha; complex num1(2, 3); cout << "Original value of num1 is " << num1 << endl; const complex num2(9, 9); cout << "Original value of num2 is " << num2 << endl; num1 = num1 + num2; cout << "Num1 is now " << num1 << endl; num1 = num1 + num1; cout << "Num1 is now " << num1 << endl << endl; cout << "Now print two numbers" << endl; cout <<"Num1 is " << num1 << " and num2 is " << num2 << endl << endl; check_equality = (num1 == num1); cout << "Num1 == num1? : " << check_equality << endl; if (num1 == num2) cout << "num1 == num2" << endl; else cout << "num1 and num2 are not equal" << endl; cout << endl; ++num1; cout << num1 << endl; cout << "Num1 has real part " << num1[0] << " and imag part " << num1[1] << endl; num1[0] = 9; num1[1] = 8; cout << num1 << endl; cout << "Num2 has real part " << num2[0] << " and imag part " << num2[1] << endl; // cin >> num1; // cout << "New value of num1 is " << num1 << endl; return 0; } // *********************************** complex::complex(double r, double i) { cout << "** Called constructor " << r << " " << i << endl; real = r; imag = i; } complex complex::operator+ (const complex& inp) const { cout << "** Called operator+ " << endl; complex temp; temp.real = real + inp.real; temp.imag = imag + inp.imag; return (temp); } bool complex::operator== (const complex& inp) const { bool return_value; cout << "** Called operator==" << endl; return_value = (real == inp.real && imag == inp.imag); return return_value; } complex& complex::operator++() { cout << "** Called operator++ (pre-increment)" << endl; real++; imag++; return *this; } const double complex::get_real() const { return real; } const double complex::get_imag() const { return imag; } const double& complex::operator [] (const int index) const { cout << "** Called operator[] const " << index << endl; if (index == 0) return real; else if (index == 1) return imag; } double& complex::operator [] (const int index) { cout << "** Called operator[] non-const " << index << endl; if (index == 0) return real; else if (index == 1) return imag; } ostream& operator<< (ostream & o, const complex& n) { o << n.real << " + " << n.imag << "i" << endl; return o; } istream& operator>> (istream & i, complex& n) { cout << "calling operator<<" << endl; cout << "enter two numbers to make up a complex number" << endl; i >> n.real >> n.imag; return i; }