// ptr12b.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program is a similar to ptr12.cpp. It however shows how to // initialize an array of class objects. #include "ptr12.h" car::car(int doors, int cyl) { num_doors = doors; num_cyl = cyl; } char* car::GetModel() { return car_model; } void car::SetModel(char* mdl) { car_model = mdl; } void car::SetColor( avail_colors col) { car_color = col; } char* car::GetColor() { switch (car_color) { case white: return "white"; case black: return "black"; case green: return "green"; case red: return "red"; } return "Unknown"; } int car::GetNumDrs() { return num_doors; } int car::GetNumCyl() { return num_cyl; } void car::SetNumCyl(int nc) { num_cyl = nc; } int main() { car car_arr1[3]; // Create 3 car objects. All of these objects will use the default constructor. car car_arr2[3] = { car( 2, 6 ), car( 3, 4 ), car( ) }; // Create any objects using the constructor that you need. car_arr1[0].SetModel( "BMW" ); car_arr1[0].SetColor( green ); car_arr1[1].SetModel( "VW" ); car_arr1[1].SetColor(); car_arr1[2].SetModel( "BENZ" ); car_arr1[2].SetColor( black ); for (int i = 0; i < 3 ; i++) { cout << " The color of car # " << i+1 << " is: " << car_arr1[i].GetColor(); cout << "\n and its model is: " << car_arr1[i].GetModel() << "\n"; } for ( i = 0; i < 3 ; i++) { cout << "\n Information of the first array of cars ... " << endl; cout << " The number of doors of car # " << i+1 << " is: " << car_arr1[i].GetNumDrs(); cout << "\n and its number of cylinders is: " << car_arr1[i].GetNumCyl() << "\n"; } for ( i = 0; i < 3 ; i++) { cout << "\n Information of the second array of cars ... " << endl; cout << " The number of doors of car # " << i+1 << " is: " << car_arr2[i].GetNumDrs() << "\n"; cout << " and its number of cylinders is: " << car_arr2[i].GetNumCyl() << "\n"; } return 0; } // The following is the result of running the program /* The color of car # 1 is: green and its model is: BMW The color of car # 2 is: white and its model is: VW The color of car # 3 is: black and its model is: BENZ Information of the first array of cars ... The number of doors of car # 1 is: 4 and its number of cylinders is: 6 Information of the first array of cars ... The number of doors of car # 2 is: 4 and its number of cylinders is: 6 Information of the first array of cars ... The number of doors of car # 3 is: 4 and its number of cylinders is: 6 Information of the second array of cars ... The number of doors of car # 1 is: 2 and its number of cylinders is: 6 Information of the second array of cars ... The number of doors of car # 2 is: 3 and its number of cylinders is: 4 Information of the second array of cars ... The number of doors of car # 3 is: 4 and its number of cylinders is: 6 */