// ptr11.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates more of pointers to class objects. Additionally, the // program examines some of the standard string operations. // Key Points: 1) String operations. // 2) Pointers to objects. #include "ptr11.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 main() { car *car_ptr = new car[3], *tmp_ptr; // Declare 3 car objects tmp_ptr = car_ptr; car_ptr -> SetModel( "BMW" ); car_ptr -> SetColor( green ); car_ptr++; car_ptr -> SetModel( "VW" ); car_ptr -> SetColor(); car_ptr++; car_ptr -> SetModel( "BENZ" ); car_ptr -> SetColor( black ); car_ptr = tmp_ptr; // Set it back to it initial place. for (int i = 0; i < 3 ; i++) { cout << "The model of car # " << i+1 << " is: " << car_ptr -> GetModel() << "\n"; cout << " and its color is: " << car_ptr -> GetColor() << "\n"; cout << " The name of this model consists of " << strlen( car_ptr -> GetModel() ) << " characters \n"; car_ptr++; } 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 name of this model consists of 3 characters The model of car # 2 is: VW and its color is: white The name of this model consists of 2 characters The model of car # 3 is: BENZ and its color is: black The name of this model consists of 4 characters */