// inherit7.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program is just another example of inheritance. No new ideas are there. // It shows the creation of objects from the base and the inherited classes. // Key Points: 1) How the creation of objects from an inherited class leads to // automatic creation of objects from the base class(s). // 2) Use of constructor initialization lists. // 3) Making sure that the classes have default constructors. #include "inherit7.h" // Definition of Vehicle's member functions Vehicle::Vehicle( avl_colors clr, int cyl ) { cout << " Vehicle...Executing the 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 inforamtion: " << "\n"; cout << " The vehcile 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 ) { // The following switch statement is needed in order to show the color as // a string; otherwise an integer value ( 0, 1, 2 or 3 ) will return // instead of ( red, black, white or green ), as avl_colors is an enum type. 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 class // 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 mamber 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 ) { // Create object from all the classes cout << " Creating a Vehicle object *********************** " << "\n"; Vehicle vec1( red, 4 ); cout << " \n Creating a Bus object *********************** " << "\n"; Bus bs1( white, 12, yes ); cout << " \n Creating a Van object *********************** " << "\n"; Van vn1( black, 8, yes ); // Execute each object's version of function ShowInfo vec1.ShowInfo( ); bs1.ShowInfo( ); vn1.ShowInfo( ); return 0; } // The result of running the program is: /* Creating a Vehicle object *********************** Vehicle...Executing the constructor of the Vehicle class. Creating a Bus object *********************** Vehicle...Executing the constructor of the Vehicle class. Bus...Executing the constructor of the bus class. Creating a Van object *********************** Vehicle...Executing the constructor of the Vehicle class. Bus...Executing the constructor of the bus class. Van...Executing the constructor of the Van class. Vehicle....Here is all the known inforamtion: The vehcile color is: red, and the number of cylinders is: 4 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 */