// overload7.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // // This program illustrates the "this" class pointer. "this" is a pointer to the // current object. That is, the address stored in the "this" pointer is the address // of the current object. "*this" indicates the contents of the "this" pointer. // Hence a statement such as "return *this;" would indicate return the invoking object // (which is the current object). // // This program overloads the "+=" operator to enable it to be used with // Vehicle objects. To clearly illustrate the idea, we assume that a statement // such as: v1 += v2; for two Vehicles v1, and v2 is to increment each data of // v1 by the similar data of v2. That is the number of doors of v1 will be // incremented by the number of doors of v2. Similarly, the maximum speed and // price of v1 will also be incremented by the maximum speed and price of v2. // Sure, this is very unrealistic in real life!! // // Much more information concerning the "this" class pointer will be provided // when pointers are discussed. // // Key Points: 1) The "this" class pointer. // #include "overload7.h" // Free function that overloads the iostream ">>" operator. istream& operator>> ( istream& is, Vehicle& vec ) { is >> vec.numdoors >> vec.maxspeed >> vec.price; return is; } // Free function that overloads the iostream "<<" operator. ostream& operator<< ( ostream& os, Vehicle& vec ) { os << " Here is the vehicle information: " << endl << " the vehicle has " << vec.numdoors << " doors, maximum speed of " << vec.maxspeed << ", and its price is " << vec.price << endl << 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; } Vehicle& Vehicle::operator+= ( const Vehicle& vec ) { // More information on "*" and "->" will be provided when pointers are discussed. this -> numdoors += vec.numdoors; // same as numdoors += vec.numdoors; this -> maxspeed += vec.maxspeed; // same as maxspeed += vec.maxspeed; this -> price += vec.price; // same as price += vec.price; return *this; // return the current object } int main() { Vehicle v1(2, 120, 22000), v2(3, 100, 12000); cout << "\n Here is the information of v1: " << endl; cout << " ============================== " << endl; cout << v1; cout << "\n Here is the information of v2: " << endl; cout << " ============================== " << endl; cout << v2; v1 += v2; // Use the "+=" operator to increment data of v1 by data of v2. cout << "\n Here is the information of v1 after incrementing its price: " << endl; cout << " ============================================================== \n" << endl; cout << v1; Vehicle v3(3, 180, 1000), v4(2, 120, 2000), v5(3, 140, 30000); cout << "\n Here is the information of v3: " << endl; cout << " ============================== " << endl; cout << v3; cout << "\n Here is the information of v4: " << endl; cout << " ============================== " << endl; cout << v4; cout << "\n Here is the information of v5: " << endl; cout << " ============================== " << endl; cout << v5; // Now you can use the "+=" operator in a similar fashion to the original operator // provided by the language. That is the reason we have a "Vehicle&" as a return // type from the function. v3 = v4 += v5; cout << "\n Here is the information of v3 after being updated: " << endl; cout << " ==================================================== " << endl; cout << v3; cout << "\n Here is the information of v4 after being updated: " << endl; cout << " ==================================================== " << endl; cout << v4; return 0; } // The result of running the program /* Here is the information of v1: ============================== Here is the vehicle information: the vehicle has 2 doors, maximum speed of 120, and its price is 22000 Here is the information of v2: ============================== Here is the vehicle information: the vehicle has 3 doors, maximum speed of 100, and its price is 12000 Here is the information of v1 after incrementing its price: ============================================================== Here is the vehicle information: the vehicle has 5 doors, maximum speed of 220, and its price is 34000 Here is the information of v3: ============================== Here is the vehicle information: the vehicle has 3 doors, maximum speed of 180, and its price is 1000 Here is the information of v4: ============================== Here is the vehicle information: the vehicle has 2 doors, maximum speed of 120, and its price is 2000 Here is the information of v5: ============================== Here is the vehicle information: the vehicle has 3 doors, maximum speed of 140, and its price is 30000 Here is the information of v3 after being updated: ==================================================== Here is the vehicle information: the vehicle has 5 doors, maximum speed of 260, and its price is 32000 Here is the information of v4 after being updated: ==================================================== Here is the vehicle information: the vehicle has 5 doors, maximum speed of 260, and its price is 32000 */