// inherit11.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program fixes the compilation error that inherit10.cpp had. // What is the difference? Why this program is working now? #include "inherit10.h" // Definition of Vehicle's member functions Vehicle::Vehicle( void ) { cout << "\n Vehicle...Executing the default constructor of the Vehicle class. " << "\n"; color = green; numcyl = 4; } Vehicle::Vehicle( avl_colors clr, int cyl ) { cout << "\n Vehicle...Executing the parameterized constructor of the Vehicle class." << "\n"; color = clr; numcyl = cyl; } void Vehicle::ShowInfo( void ) { // Is the following information enough? cout << " Vehicle....Here is all the known information: " << "\n"; cout << " The vehicle color is: " << GetColor( ) << ",\n"; cout << " and the number of cylinders is: " << GetNumCyl( ) << "\n\n\n"; } int Vehicle::GetNumCyl( void ) { return numcyl; } char* Vehicle::GetColor( void ) { switch ( color ) { case red: return "red"; case black: return "black"; case white: return "white"; case green: return "green"; } return "unknown"; } // Define Bus's member functions Bus::Bus( avl_colors aclr, int cyl, yes_no bar ) : Vehicle( aclr, cyl ) { cout << " Bus...Executing the constructor of the bus class. " << "\n"; barexist = bar; } void Bus::ShowInfo( void ) { // Bus had to redefine this function from the base clas // to include its specific information cout << " Bus...Here is all the needed information: " << "\n"; cout << " The bus color is: " << GetColor( ) << ",\n"; cout << " the number of cylinders is: " << GetNumCyl( ) << ",\n"; if ( HasBar( ) == yes ) cout << " and the bus has a bar. " << "\n\n\n"; else cout << " and the bus does not have a bar. " << "\n\n\n"; } yes_no Bus::HasBar ( void ) { return barexist; } // Define Van's member functions Van::Van( avl_colors aclr, int cyl, yes_no tur ) : Bus( aclr, cyl, no ) // always no bar { cout << " Van...Executing the constructor of the Van class. " << "\n\n\n"; turbo = tur; } void Van::ShowInfo( void ) { // Van had to redefine this function from the base clas // to include its specific information cout << " Van...Here is all the needed information: " << "\n"; cout << " The van color is: " << GetColor( ) << ",\n"; cout << " the number of cylinders is: " << GetNumCyl( ) << ",\n"; if ( IsTurbo( ) == yes ) cout << " and the van is turbo " << "\n\n\n"; else cout << " and the van is not turbo. " << "\n\n\n"; } yes_no Van::IsTurbo( void ) { return turbo; } int main( void ) { // Make use of virtual functions.. // Step #1 : create an array of 'pointers' to Vehicle class Vehicle *Vec_arr[3]; // Step #2 : create the individual, derived objects. // Notice that you can declare objects from Vehicle class. However, Does this // make sense? Why? Bus bs1( white, 12, yes ); Van vn1( black, 8, yes ); Van vn2( white, 6, no ); // Step #3 : put the addresses of these derived objects into our array Vec_arr[0] = &bs1; Vec_arr[1] = &vn1; Vec_arr[2] = &vn2; // Step #4 : Execute all ShowInfo() of the objects by iterating // through the array. for ( int counter = 0; counter < 3; counter++ ) Vec_arr[ counter ] -> ShowInfo( ); // Step 5. See vehicle4.h // Definition of Vehicle's member functions return 0; } // The result of running the program is as follow: /* Vehicle...Executing the parameterized constructor of the Vehicle class. Bus...Executing the constructor of the bus class. Vehicle...Executing the parameterized constructor of the Vehicle class. Bus...Executing the constructor of the bus class. Van...Executing the constructor of the Van class. Vehicle...Executing the parameterized constructor of the Vehicle class. Bus...Executing the constructor of the bus class. Van...Executing the constructor of the Van class. Bus...Here is all the needed information: The bus color is: white, the number of cylinders is: 12, and the bus has a bar. Van...Here is all the needed information: The van color is: black, the number of cylinders is: 8, and the van is turbo Van...Here is all the needed information: The van color is: white, the number of cylinders is: 6, and the van is not turbo. */