// overload8.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // // This program shows how operator overloading should be implemented, so that the // overloaded operator behaves, as well as can be used, in a very similar ways to // the original operator implemented by the standard language. // As the "+" operator is implemented now, it is possible to write a statement that // includes: "v1 + v2 + v3 + v4;" where v1, v2, v3 and v4 are all Vehicle objects. // // Key Points: 1) Operator overloading. // 2) "static" objects. // #include "overload8.h" // Free function that overloads the "+" operator. Vehicle& operator+ (const Vehicle& vec1, const Vehicle& vec2) { static Vehicle tempvec; tempvec.price = vec1.price + vec2.price; return tempvec; } // Free function that overloads the iostream "<<" operator. ostream& operator<< ( ostream& os, Vehicle& vec ) { os << vec.price << "$." << endl; return os; } 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, 3000), v2(3, 220, 4000), v3(3, 220, 5000), v4(2, 200, 6000); 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(); 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 The total price of v1 and v2 is: " << v1 + v2 << endl; cout << "\n The total price of v1, v2 and v3 is: " << v1 + v2 + v3 << endl; cout << "\n The total price of v1, v2, v3 and v4 is: " << v1 + v2 + v3 + v4 << endl; cout << "\n The total price of v1, v3 and v4 is: " << v1 + v3 + v4 << endl; 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 3000$. Here is the information of v2: ============================== The vehicle has 3 doors, 220 KM maximum speed, and its price is 4000$. Here is the information of v3: ============================== The vehicle has 3 doors, 220 KM maximum speed, and its price is 5000$. Here is the information of v4: ============================== The vehicle has 2 doors, 200 KM maximum speed, and its price is 6000$. The total price of v1 and v2 is: 7000$. The total price of v1, v2 and v3 is: 12000$. The total price of v1, v2, v3 and v4 is: 18000$. The total price of v1, v3 and v4 is: 14000$. */