// inherit10.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program explains what "pure" virtual functions are, and how can we // use pure virtual functions to disallow creation of objects from a base // class. [ In such a case, the base class is called "abstract" base class. // Note that this program does not work. Why? // Key points: 1) Pure virtual functions. // 2) Abstract base classes. #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[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 ); // This is the line where the compilation stops. 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 vehicle4.h return 0; } // The result of compiling the program is as follow: // On VC++ /* Compiling... inherit10.cpp inherit10.cpp(152) : error C2259: 'Vehicle' : cannot instantiate abstract class due to following members: inherit10.h(14) : see declaration of 'Vehicle' inherit10.cpp(152) : warning C4259: 'void __thiscall Vehicle::ShowInfo(void)' : pure virtual function was not defined inherit10.h(20) : see declaration of 'ShowInfo' inherit10.cpp(152) : error C2259: 'Vehicle' : cannot instantiate abstract class due to following members: inherit10.h(14) : see declaration of 'Vehicle' inherit10.cpp(152) : warning C4259: 'void __thiscall Vehicle::ShowInfo(void)' : pure virtual function was not defined inherit10.h(20) : see declaration of 'ShowInfo' */ // On UNIX /* CC: "inherit10.cpp", line 152: error: declaration of object of abstract class Vehicle - pure virtual function(s) have not been defined (1570) */