// Program to demonstrate the use of operator overloading for a complex class. // Assignment-24 // opovrld.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #else #include using namespace std; #endif #include // For getch. class Box { int l, b, h; public: Box(int fl = 0, int fb = 0, int fh = 0) : l(fl), b(fb), h(fh) { } long int volume(); Box operator+(const Box&); Box operator+(const int&); Box operator-(const Box&); Box operator-(const int&); Box operator++(); // Pre. Box operator++(int); // Post. Box operator--(); // Pre. Box operator--(int); // Post. friend ostream& operator<<(ostream&, Box&); }; long int Box::volume() { long int vol; vol = l * b * h; return vol; } Box Box::operator+(const Box& bObj) { Box tempObj; tempObj.l = this->l + bObj.l; tempObj.b = this->b + bObj.b; tempObj.h = this->h + bObj.h; return tempObj; } Box Box::operator+(const int& inc) { Box tempObj; tempObj.l = this->l + inc; tempObj.b = this->b + inc; tempObj.h = this->h + inc; return tempObj; } Box Box::operator-(const Box& bObj) { Box tempObj; tempObj.l = this->l - bObj.l; tempObj.b = this->b - bObj.b; tempObj.h = this->h - bObj.h; return tempObj; } Box Box::operator-(const int& dec) { Box tempObj; tempObj.l = this->l - dec; tempObj.b = this->b - dec; tempObj.h = this->h - dec; return tempObj; } Box Box::operator++() { *this = *this + 1; return *this; } Box Box::operator++(int dummy) { Box tempObj = *this; *this = *this + 1; return tempObj; } Box Box::operator--() { *this = *this - 1; return *this; } Box Box::operator--(int dummy) { Box tempObj = *this; *this = *this - 1; return tempObj; } ostream& operator<<(ostream& os, Box& obj) { os << "l=" << obj.l << ", b=" << obj.b << ", h=" << obj.h << endl; return os; } int main() { #ifdef TC clrscr(); #endif Box objA(4, 5, 9); Box objB(1, 1, 1); Box objC; cout << "objA - " << objA; cout << "Voulme with objA : " << objA.volume() << endl << endl; cout << "objB - " << objB; cout << "Volume with objB : " << objB.volume() << endl << endl; objC = objA + objB; cout << "objC - " << objC; cout << "Volume with objC (objA + objB), " << objC.volume() << endl; objC = objA + 4; cout << "objC - " << objC; cout << "Volume with objC (objA + 4), " << objC.volume() << endl << endl; getch(); objC = objA - objB; cout << "objC - " << objC; cout << "Volume with objC (objA - objB), " << objC.volume() << endl; objC = objA - 1; cout << "objC - " << objC; cout << "Volume with objC (objA - 1), " << objC.volume() << endl << endl; getch(); ++objC; cout << "objC - " << objC; cout << "Volume with ++objC, " << objC.volume() << endl; objC++; cout << "objC - " << objC; cout << "Volume with objC++, " << objC.volume() << endl << endl; getch(); --objC; cout << "objC - " << objC; cout << "Volume with --objC, " << objC.volume() << endl; objC--; cout << "objC - " << objC; cout << "Volume with objC--, " << objC.volume(); cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }