// overload6.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // // This program illustrates how operators of the iostream class can be overloaded. // // Key Points: 1) Overloading operators of the iostream class // 2) "friend" functions #include "overload6.h" // Free function that overloads the iostream ">>" operator. // With this function it will be possible to input information // into a Vehicle object using cin >>. istream& operator>> ( istream& is, Vehicle& vec ) { is >> vec.numdoors >> vec.maxspeed >> vec.price; return is; } // Free function that overloads the iostream "<<" operator. // With this function it will be possible to output all information // of a Vehicle object using cout <<. 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; } int main() { Vehicle v1(2, 180, 22000), v2(3, 220, 12000), v3(5, 240, 8000); 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; cout << "\n Here is the information of v3: " << endl; cout << " ============================== " << endl; cout << v3; cout << " Updating v1 and v2 information: " << " \n ============================= " << endl << endl; cout << " Enter the number of doors, the maximum speed " << " and the price of the two vehicles: "; cin >> v1; cin >> v2; cout << "\n Here is the information of v1 after being updated: " << endl; cout << " ==================================================== " << endl; cout << v1; cout << "\n Here is the information of v2 after being updated: " << endl; cout << " ==================================================== " << endl; cout << v2; cout << " Updating v1, v2 and v3 information: " << " \n ============================= " << endl << endl; cout << " Enter the number of doors, the maximum speed " << " and the price of the three vehicles: "; cin >> v1 >> v2 >> v3; cout << "\n Here is the information of v1 after being updated: " << endl; cout << " ==================================================== " << endl; cout << v1; cout << "\n Here is the information of v2 after being updated: " << endl; cout << " ==================================================== " << endl; cout << v2; cout << "\n Here is the information of v3: " << endl; cout << " ============================== " << endl; cout << v3; 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 180, and its price is 22000 Here is the information of v2: ============================== Here is the vehicle information: the vehicle has 3 doors, maximum speed of 220, and its price is 12000 Here is the information of v3: ============================== Here is the vehicle information: the vehicle has 5 doors, maximum speed of 240, and its price is 8000 Updating v1 and v2 information: ============================= Enter the number of doors, the maximum speed and the price of the two vehicle: 4 140 2000 5 160 3000 Here is the information of v1 after being updated: ==================================================== Here is the vehicle information: the vehicle has 4 doors, maximum speed of 140, and its price is 2000 Here is the information of v2 after being updated: ==================================================== Here is the vehicle information: the vehicle has 5 doors, maximum speed of 160, and its price is 3000 Updating v1, v2 and v3 information: ============================= Enter the number of doors, the maximum speed and the price of the three vehices: 2 200 10000 4 220 15000 5 240 20000 Here is the information of v1 after being updated: ==================================================== Here is the vehicle information: the vehicle has 2 doors, maximum speed of 200, and its price is 10000 Here is the information of v2 after being updated: ==================================================== Here is the vehicle information: the vehicle has 4 doors, maximum speed of 220, and its price is 15000 Here is the information of v3: ============================== Here is the vehicle information: the vehicle has 5 doors, maximum speed of 240, and its price is 20000 */