// ptr12.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program is similar to ptr10.cpp. The main difference between the two programs // is that this program is using array of objects, instead of pointers to objects. // Notice that this program, although it works correctly, has few potential problems. // In other words, that program is not really that good. Why? See ptr18.cpp for more details. #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::GetNumCyl() { return num_cyl; } void car::SetNumCyl(int nc) { num_cyl = nc; } int main() { car car_arr[3]; // Declare 3 objects of car car_arr[0].SetModel( "BMW" ); car_arr[0].SetColor( green ); car_arr[1].SetModel( "VW" ); car_arr[1].SetColor(); car_arr[2].SetModel( "BENZ" ); car_arr[2].SetColor( black ); for (int i = 0; i < 3 ; i++) { cout << "The model of car # " << i+1 << " is: " << car_arr[i].GetModel() << "\n"; cout << " and its color is: " << car_arr[i].GetColor() << "\n"; } return 0; } // The following is the result of compilation //Compilation successful // The following is the result of running the program /* The model of car # 1 is: BMW and its color is: green The model of car # 2 is: VW and its color is: white The model of car # 3 is: BENZ and its color is: black */