#include using namespace std; /** * class to implement complex number */ class Complex { double real, imag; public: /** * default constructor with no argument */ Complex() : real(0), imag(0) { cout << "call default constructor" << endl; } /** * copy constructor */ Complex(const Complex& c) { real = c.real; imag = c.imag; } /** * constructor with 2 arguments but one is assigned ! * declared explicit !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ explicit Complex( double r, double i = 0.0) { cout << "call constructor with two arguments: real=" << r << ", imag=" << i << endl; real = r; imag = i; } /** * overloading of addition operator */ Complex operator+(const Complex& c) { double tmp_real = real + c.real; double tmp_imag = imag + c.imag; return Complex(tmp_real, tmp_imag); } bool operator==(Complex rhs) { return (real == rhs.real && imag == rhs.imag) ? true : false; } friend ostream& operator<<(ostream& out, Complex& c) { out << "(" << c.real << "," << c.imag << ")"; } }; int main() { Complex one(1); Complex sum; cout << one << endl; // *** explicit *** conversion of 3 into Complex(3, 0) sum = one + Complex(3); cout << sum << endl; // *** explicit *** conversion of 4 into Complex(4, 0) if (sum == Complex(4.0)) { cout << "result is valid" << endl; } else { cout << "error !!!" << endl; } return 0; }