// inherit13.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // More explanation on why virtual functions are so convenient. // Notice what kinds of troubles could be created as a result of // having the programmer to control the operations. // See the output of the program. (Sure, the problem shown here // can easily be avoided. However some extra code is needed.) #include "inherit12.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; name = "VEC"; } Vehicle::Vehicle( avl_colors clr, int cyl, char* nm ) { cout << "\n Vehicle...Executing the parameterized constructor of the Vehicle class." << "\n"; color = clr; numcyl = cyl; name = nm; } 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"; } char* Vehicle::GetName( void ) { return name; } // Define Bus's member functions Bus::Bus( avl_colors aclr, int cyl, yes_no bar, char* nm ) : Vehicle( aclr, cyl, nm ) { cout << " Bus...Executing the constructor of the bus class. " << "\n"; barexist = bar; } void Bus::ShowInfo( void ) { // Bus had to redefine this function 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, char* nm ) : Bus( aclr, cyl, no, nm ) // 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 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 ) { Vehicle *Vec_arr[6]; Bus bs1( white, 12, yes, "BUs" ); // Notice the error Van vn1( black, 8, yes ); Van vn2( white, 6, no ); Bus bs2( green, 12, no ); Bus bs3( black, 8, yes, "VAN" ); // Notice the error Bus bs4( red, 8, no ); Vec_arr[0] = &bs1; Vec_arr[1] = &vn1; Vec_arr[2] = &vn2; Vec_arr[3] = &bs2; Vec_arr[4] = &bs3; Vec_arr[5] = &bs4; // It becomes the programmer's responsibility to determine // the derived class type addressed by the Vehicle pointer. // Notice the need of the knowledge of type representation. Burden // of type resolution is on the programmer's code. for ( int counter = 0; counter < 6; counter++ ) { // Note that strcmp() returns zero if strings are the same if ( !strcmp( Vec_arr[ counter ] -> GetName( ), "BUS" ) ) ((Bus* )Vec_arr[ counter ]) ->ShowInfo( ); else if ( !strcmp( Vec_arr[ counter ] -> GetName( ), "VAN" ) ) ((Van*)Vec_arr[ counter ]) ->ShowInfo( ); /* This will need to be repeated for all the derived classes.... // . // . // . // . .........etc. */ } 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. 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. Vehicle...Executing the parameterized constructor of the Vehicle class. Bus...Executing the constructor of the bus class. 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. Bus...Here is all the needed information: The bus color is: green, the number of cylinders is: 12, and the bus does not have a bar. Van...Here is all the needed information: The van color is: black, the number of cylinders is: 8, and the van is not turbo. Bus...Here is all the needed information: The bus color is: red, the number of cylinders is: 8, and the bus does not have a bar. */