// overload5.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // // This program illustrates more of operator overloading. The program // shows how operators can be overloaded as a nonmember functions // of a class (such as free functions). Notice the difference in the // number of passed parameters to the overloaded function in this case. // // Key Points: 1) Operator overloading // 2) "friend" functions // #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 increment the price of a vehicle object by the price // of another one. Notice that this function is not a "friend" function to // the Vehicle class. void operator+= ( Vehicle& vec1, Vehicle& vec2 ) { vec1.SetPrice( vec1.GetPrice() + vec2.GetPrice()); } 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 << " ==============================================================" << endl << endl; v1.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$. */