// composition2.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program is very similar to composition1.cpp. The main difference between the // two programs is that this program does not have default constructors for the // supplier classes. // The significant point here is that you must make sure that the supplier class // objects are initialized. // Key Points: 1) Initializing objects of the supplier classes. #include "composition2.h" // Implementation of the Engine class Engine::Engine(double hp, double pr) { cout << "\n Creating an Engine object. " << endl; horsepower = hp; price = pr; } double Engine::GetEngHp( void ) { return horsepower; } void Engine::SetEngHp( double hp ) { horsepower = hp; } double Engine::GetEngPrice( void ) { return price; } void Engine::SetEngPrice( double pr ) { price = pr; } // Implementation of the CarBody class CarBody::CarBody( ava_color cl, double pr ) { cout << " Creating a CarBody object. " << endl; color = cl; price = pr; } ava_color CarBody::GetCarBodyColor( void ) { return color; } void CarBody::SetCarBodyColor( ava_color cl ) { color = cl; } double CarBody::GetCarBodyPrice( void ) { return price; } void CarBody::SetCarBodyPrice( double pr ) { price = pr; } // Implementation of the Car class Car::Car( double hp, ava_color cl, double engpr, double bodpr ):eng(hp, engpr), body(cl, bodpr) { cout << " Creating an CarBody object. " << endl; // Since there is no default constructor for the supplier classes, the "eng" // and "body" will not be initialized when they are created ( that is actually when a // Car object is created). As a result, these two objects must be initialized first // upon creating the Car object. // Notice that there is no way to initialize these objects within the class // declaration since this in not allowed by the language. Hence, you must use // initialization list for that. // Initial price of the car is set to 1.5 * ( engine price + body price ) price = 1.5 * ( eng.GetEngPrice() + body.GetCarBodyPrice() ); // The above line has the same effect as: price = 1.5 * ( engpr + bodpr ); } double Car::GetCarHorsepower( void ) { return eng.GetEngHp( ); } void Car::SetCarHorsepower( double hp ) { eng.SetEngHp( hp ); } double Car::GetCarPrice( void ) { return price; } void Car::SetCarPrice( double pr ) { // Set the car price is the passed value. If the price is less than the total price of the // engine and the body, issue a warning message. price = pr; if ( price < ( eng.GetEngPrice() + body.GetCarBodyPrice() ) ) cout << " Warning: Selling with that price is a losing sale. Verify the price. " << endl; } void Car::SetCarColor( ava_color cl ) { body.SetCarBodyColor( cl ); } char* Car::GetCarColor( void ) { switch (body.GetCarBodyColor()) { case white: return "white"; // There is no need for "break" here case black: return "black"; case blue: return "blue"; case green: return "green"; case red: return "red"; }; return "Unknown"; } void Car::ShowCarInfo() { cout << " The car has a horsepower of: " << GetCarHorsepower( ) << " and the color of the car is: " << GetCarColor() << "." << endl << " The price of this car is: " << GetCarPrice() << endl; } // main function int main() { Car c1, c2, c3; // Create 3 car objects. cout << " \n Here is the information of car object c1: " << endl; c1.ShowCarInfo(); cout << " \n Here is the information of car object c2: " << endl; c2.ShowCarInfo(); cout << " \n Here is the information of car object c3: " << endl; c3.ShowCarInfo(); cout << " \n Here is the information of car object c1 after updates: " << endl; c1.SetCarColor( black ); c1.SetCarHorsepower( 200 ); c1.SetCarPrice( 7000 ); c1.ShowCarInfo(); cout << " \n Here is the information of car object c2 after updates: " << endl; c2.SetCarColor( red); c2.SetCarHorsepower( 250 ); c2.SetCarPrice( 5000 ); c2.ShowCarInfo(); cout << " \n Here is the information of car object c3 after updates: " << endl; c3.SetCarColor( blue); c3.SetCarHorsepower( 300 ); c3.SetCarPrice( 20000 ); c3.ShowCarInfo(); return 0; } // The result of running the program /* Creating an Engine object. Creating a CarBody object. Creating an CarBody object. Creating an Engine object. Creating a CarBody object. Creating an CarBody object. Creating an Engine object. Creating a CarBody object. Creating an CarBody object. Here is the information of car object c1: The car has a horsepower of: 40 and the color of the car is: white. The price of this car is: 9000 Here is the information of car object c2: The car has a horsepower of: 40 and the color of the car is: white. The price of this car is: 9000 Here is the information of car object c3: The car has a horsepower of: 40 and the color of the car is: white. The price of this car is: 9000 Here is the information of car object c1 after updates: The car has a horsepower of: 200 and the color of the car is: black. The price of this car is: 7000 Here is the information of car object c2 after updates: Warning: Selling with that price is a losing sale. Verify the price. The car has a horsepower of: 250 and the color of the car is: red. The price of this car is: 5000 Here is the information of car object c3 after updates: The car has a horsepower of: 300 and the color of the car is: blue. The price of this car is: 20000 */