// overload5_b.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // // This program is very similar to overload5.cpp. The difference between the // two programs lies in the way the overloaded "+=" operator is implemented. // With the default "+=" operator, we can write a statement like: // x = y += z; // where x, y and z are of type "int" for example. // With the implementation of the "+=" operator in overload5.cpp, such a // statement would be illegal. The intention of this program is to show an // example of how overloaded operators should be implemented so that // they are capable of providing as much flexibility as possible compared // to the original operators. // // Key Points: 1) What kind of functionality should an // overloaded operator provide? // #include "overload5.h" // Free function that overloads the "+" operator. double operator+ (const Vehicle& vec1, const Vehicle& vec2) { return vec1.price + vec2.price; } // Free function that overloads the "==" operator. bool operator== ( const Vehicle& vec1, const Vehicle& vec2 ) { return ((vec1.numdoors == vec2.numdoors) && (vec1.maxspeed == vec2.maxspeed) && (vec1.price == vec2.price)); } // Free function that increments the price of a vehicle object by the price // of another object. Notice that this function is not a "friend" function to // the Vehicle class. The function should return the incremented object if // the "+=" operator is to behave in a similar fashion to the one provided // by the language. Vehicle& operator+= ( Vehicle& vec1, Vehicle& vec2 ) { vec1.SetPrice( vec1.GetPrice() + vec2.GetPrice()); return vec1; } Vehicle::Vehicle( int nd, double mxspd, double pr ) { numdoors = nd; maxspeed = mxspd; price = pr; } int Vehicle::GetNumDrs( void ) { return numdoors; } void Vehicle::SetNumDrs( int nd ) { numdoors = nd; } double Vehicle::GetmaxSpeed( void ) { return maxspeed; } void Vehicle::SetMaxSpeed( double mxspd) { maxspeed = mxspd; } double Vehicle::GetPrice( void ) { return price; } void Vehicle::SetPrice( double pr ) { price = pr; } void Vehicle::ShowInfo( void ) { cout << " The vehicle has " << numdoors << " doors, " << maxspeed << " KM maximum speed, and its price is " << price << "$." << endl << endl; } int main() { Vehicle v1(2, 180, 22000), v2(3, 220, 12000); cout << "\n Here is the information of v1: " << endl; cout << " ============================== " << endl; v1.ShowInfo(); cout << "\n Here is the information of v2: " << endl; cout << " ============================== " << endl; v2.ShowInfo(); if (v1 == v2) // Use the overloaded operator "==" to the two vehicles are identical. cout << " v1 and v2 are identical objects. " << endl; else cout << " v1 and v2 are not identical objects. " << endl; cout << "\n The total price of v1 and v2 is: " << v1 + v2 << endl; v1 += v2; // Use the "+=" operator to increment the price of v1 by the price of v2. cout << "\n Here is the information of v1 after incrementing its price: " << endl; cout << " ============================================================== \n" << endl; v1.ShowInfo(); Vehicle v3(3, 180, 1000), v4(5, 200, 2000), v5(4, 220, 30000); cout << "\n Here is the information of v3: " << endl; cout << " ============================== " << endl; v3.ShowInfo(); cout << "\n Here is the information of v4: " << endl; cout << " ============================== " << endl; v4.ShowInfo(); cout << "\n Here is the information of v5: " << endl; cout << " ============================== " << endl; v5.ShowInfo(); // Now you can use the "+=" operator in a similar fashion to the original operator // provided by the language. Notice that with the implementation of the overloaded // "+=" operator in the previous program (overload5.cpp), a statement like that // would be illegal. v3 = v4 += v5; cout << "\n Here is the information of v3 after being updated: " << endl; cout << " ==================================================== " << endl; v3.ShowInfo(); cout << "\n Here is the information of v4 after being updated: " << endl; cout << " ==================================================== " << endl; v4.ShowInfo(); return 0; } // The result of running the program /* Here is the information of v1: ============================== The vehicle has 2 doors, 180 KM maximum speed, and its price is 22000$. Here is the information of v2: ============================== The vehicle has 3 doors, 220 KM maximum speed, and its price is 12000$. v1 and v2 are not identical objects. The total price of v1 and v2 is: 34000 Here is the information of v1 after incrementing its price: ============================================================== The vehicle has 2 doors, 180 KM maximum speed, and its price is 34000$. Here is the information of v3: ============================== The vehicle has 3 doors, 180 KM maximum speed, and its price is 1000$. Here is the information of v4: ============================== The vehicle has 5 doors, 200 KM maximum speed, and its price is 2000$. Here is the information of v5: ============================== The vehicle has 4 doors, 220 KM maximum speed, and its price is 30000$. Here is the information of v3 after being updated: ==================================================== The vehicle has 5 doors, 200 KM maximum speed, and its price is 32000$. Here is the information of v4 after being updated: ==================================================== The vehicle has 5 doors, 200 KM maximum speed, and its price is 32000$. */