/* Author: Arun Somasundaram, CSE Dept, OSU. Simple Program to understand operator overloading, friend functions. Answer various questions asked throughout the program. This program has the Complex class and the main function. g++ -o opFriendExec operatorFriendExamples.cpp */ #include using namespace std; class Complex{ private: float re; //real part of Complex Number float im; //imaginary part of Complex Number public: Complex(); Complex(float realPart, float imPart); Complex(const Complex &c);//Copy Constructor ~Complex(); Complex & operator= (const Complex &op); //Assignment operator Complex operator+ (const Complex &op); Complex operator* (float factor); Complex& operator+= (const Complex &op); friend ostream& operator <<(ostream &os, const Complex &c); //Why is the above function defined as a friend function void print() const; }; Complex::Complex(){ cout << "Default constructor called." << endl; re = im = 0.0; } Complex::Complex(float realPart , float imPart){ cout << "Constructor with 2 arguments called." << endl; re = realPart; im = imPart; } Complex::Complex(const Complex &c){ cout << "Copy Constructor called." << endl; re = c.re; im = c.im; } Complex::~Complex(){ cout << "Destructor called" << endl; } Complex& Complex::operator= (const Complex &op){ cout << "Assignment operator called" << endl; if (this != &op){ re = op.re; im = op.im; } else{ cout << "Warning: Self assignment." << endl; } return *this; } Complex Complex::operator+ (const Complex &op){ //overload + operator float realPart = re + op.re; float imPart = im + op.im; cout << "Inside operator + " << realPart << ", " << imPart << endl; Complex addAns(realPart, imPart); return addAns; } Complex& Complex::operator+= (const Complex &op){ //overload += operator re += op.re; im += op.im; return *this; //What is *this? //Why is the return type Complex&? //What happens if you make the return type just Complex? // Hint:Note what constructors are called. } Complex Complex::operator* (float factor){ //overload * operator re *= factor; im *= factor; cout << "Inside operator * " << re << ", " << im << endl; Complex multAns(re, im); return multAns; } void Complex::print() const{ cout << "(" << re << ", " << im << ")" << endl; //re = 5.0; //error. Why? } ostream& operator<< (ostream &os, const Complex &c){ //Why don't you have Complex:: before operator? //Is this function in the scope of the Complex class? //If not, how does the function know which class has << operator overloaded //How can this function access the private data of c? os << "(" << c.re << ", " << c.im << ")"; return os; } int main(){ Complex c1 = Complex(1, 2); Complex c2 = Complex(10, 20); Complex c3 = Complex(100, 200); cout << endl; Complex c4; c4.print(); cout << endl; c4 = c1 + c2; //Is copy constructor called? Why? c4.print(); cout << endl; Complex c5; c5 = c1 + c2 + c3; //In what order are additions done. cout << c5 << endl; //What happens here? How? cout << endl; Complex c6; c6 += c1; //What happens here? How? cout << c6 << endl; cout << endl; Complex c7; Complex c8; c8 = c7 = c1; //What happens here? How? //In what order are assignments done? cout << c7 << endl; cout << c8 << endl; cout << endl; cout << "Precedence of operators" << endl; Complex c9; c9 = c1 + c2 * 2.0; //What happens here? //Is there a precedence of operators? cout << c9 << endl; //Where all are destructors called in the execution of the program? return 0; }