// inherit8.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program gives an introduction "to the need" of virtual functions. // In fact, the program does not mention anything about virtual functions; // rather it illustrates the need of virtual functions. // In addition, this program is not working as expected. Why? // What can we modify in order to make it work? // Key points: 1) Virtual functions. Why are they needed. // 2) Array of class objects. #include "inherit8.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 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 // We need to create 4 different vehicle objects. // ( 1 vehicle, 1 bus & 2 van objects ). Then we need to show // the information for each of these objects. // Note that the following part of code is commented out. // 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 ); // // cout << " \n Creating a Van object *********************** " << "\n"; // Van vn2( white, 6, yes ); // Execute each object's version of function ShowInfo // Instead of calling object by object for the same function, could we // have an array of objects and execute this same function through a // for loop? // vec1.ShowInfo( ); // bs1.ShowInfo( ); // vn1.ShowInfo( ); // vn2.ShowInfo( ); // Declare array of 4 objects Vehicle Vec_arr[4] = { Vehicle( red,4 ), Bus( white, 12, yes ), Van( black, 8, yes ), Van( white, 6, yes ) }; // Notice that we could assign different objects // to the array. However, what are the real objects // in the array? and why? for ( int counter = 0; counter < 4; counter++ ) Vec_arr[ counter ].ShowInfo( ); 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 Vehicle....Here is all the known information: The vehicle color is: white, and the number of cylinders is: 12 Vehicle....Here is all the known information: The vehicle color is: black, and the number of cylinders is: 8 Vehicle....Here is all the known information: The vehicle color is: white, and the number of cylinders is: 6 */