// ptr14.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program "intends" to find the car with the shortest model name. // However, Does it work as expected? #include "ptr13.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_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. // Find the car with the shortest model name. Will really this following // logic work as expected!!?. char* shrt_mdl_name = car_ptr -> GetModel(); // Assume the first car has the shortest model name for (int i = 1; i < 3 ; i++) { car_ptr++; if ( strcmp(car_ptr -> GetModel() , shrt_mdl_name) < 0 ) shrt_mdl_name = car_ptr -> GetModel(); } cout << " The shortest model name for all the existing car objects is: " << shrt_mdl_name << "\n"; return 0; } /* // The following is the result of compilation //Compilation successful // The following is the result of running the program. As it shows, it is NOT the // right answer for what we want. The shortest model name for all the existing car objects is: BENZ */