// inherit9.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program explains what virtual functions are, and how they can be // used. Note the different needed steps to declare / implement and use // virtual functions. // Key points: 1) Steps to create and use virtual functions. // 2) Does it make sense to have objects from any base class? Why? // 3) Compare the result of this program to the one you got from // inherit8.cpp #include "inherit9.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 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 ) { // Make use of virtual functions.. // Step #1 : create an array of 'pointers' to Vehicle class Vehicle *Vec_arr[4]; // Step #2 : create the individual, derived objects. // Notice that you can declare objects from Vehicle class. However, Does this // make sense? Why? Vehicle vec1( red, 4 ); 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] = &vec1; Vec_arr[1] = &bs1; Vec_arr[2] = &vn1; Vec_arr[3] = &vn2; // Step #4 : Execute all ShowInfo() of the objects by iterating // through the array. for ( int counter = 0; counter < 4; counter++ ) Vec_arr[ counter ] -> ShowInfo( ); // Step 5. See inherit9.h return 0; } // The result of running the program is: /* Vehicle...Executing the parameterized constructor of the Vehicle 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...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....Here is all the known information: The vehicle 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 Van...Here is all the needed information: The van color is: white, the number of cylinders is: 6, and the van is not turbo. */