// composition1.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates the concept of object composition. // Composition refers to the case when a class contains one or more data members that // are objects from other classes. In this program, a car object contains an engine // and a car body. Both the engine and the body are objects of other classes; these // are the Engine and the CarBody classes. // // The class that uses objects from other classes is called the client class, while // the classes that are used in the composition are called the supplier classes. In // this program, the Car class is a client class, while the Engine and CarBody classes // are supplier classes. // // Key Points: 1) Object Composition. // 2) Client and supplier classes. // 3) Notice the use of default constructors for the supplier classes. #include "composition1.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( void ) { cout << " Creating a Car object. " << endl; // Initial price of the car is set to 1.5 * ( engine price + body price ) price = 1.5 * ( eng.GetEngPrice() + body.GetCarBodyPrice() ); } 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 a Car object. Creating an Engine object. Creating a CarBody object. Creating a Car object. Creating an Engine object. Creating a CarBody object. Creating a Car 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 */