// ptr9.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates pointers to class objects. // // Key Points: 1) Pointers to objects. #include "ptr9.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]; // Declare 3 car objects // Intialization.. done manually here. car_ptr -> SetModel( "BMW" ); car_ptr -> SetColor( green ); ( car_ptr + 1 ) -> SetModel( "VW" ); ( car_ptr + 1 ) -> SetColor(); ( car_ptr + 2 ) -> SetModel( "BENZ" ); ( car_ptr + 2 ) -> SetColor( black ); cout << " The model of the first car is: " << car_ptr -> GetModel() << "\n"; cout << " and its color is: " << car_ptr -> GetColor() << "\n"; cout << " The model of the second car is: " << (car_ptr + 1) -> GetModel() << "\n"; cout << " and its color is: " << (car_ptr + 1) -> GetColor() << "\n"; cout << " The model of the third car is: " << (car_ptr + 2) -> GetModel() << "\n"; cout << " and its color is: " << (car_ptr + 2) -> 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 the first car is: BMW and its color is: green The model of the second car is: VW and its color is: white The model of the third car is: BENZ and its color is: black */