// inherit14.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program concerns of Access Rights. (No new ideas are introduced in this program.) // Notice the private sections in the header file. // Note that this program is not working. Why? // Key Points: 1) When should we declare functions or data as private? // 2) Who has the right to access private sections of any class? // This program does not work. Why? #include "inherit14.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 information: " << "\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 clr, int cyl, yes_no bar ) { cout << " Bus...Executing the constructor of the Bus class. " << "\n"; color = clr; numcyl = cyl; 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 clr, int cyl, yes_no tur ) { cout << " Van...Executing the constructor of the Van class. " << "\n\n\n"; color = clr; numcyl = cyl; 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 ) { // Create object from all the classes cout << " Creating a Vehicle object *********************** " << "\n"; Vehicle vec1( red, 4 ); cout << " ############################################### " << "\n"; cout << " \n Creating a Bus object *********************** " << "\n"; Bus bs1( white, 12, yes ); cout << " ############################################### " << "\n"; 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 compiling the program: /* inherit14.cpp(64) : error C2248: 'color' : cannot access private member declared in class 'Vehicle' inherit14.h(23) : see declaration of 'color' inherit14.cpp(65) : error C2248: 'numcyl' : cannot access private member declared in class 'Vehicle' inherit14.h(22) : see declaration of 'numcyl' inherit14.cpp(76) : error C2248: 'GetColor' : cannot access private member declared in class 'Vehicle' inherit14.h(21) : see declaration of 'GetColor' inherit14.cpp(77) : error C2248: 'GetNumCyl' : cannot access private member declared in class 'Vehicle' inherit14.h(20) : see declaration of 'GetNumCyl' inherit14.cpp(101) : error C2248: 'color' : cannot access private member declared in class 'Vehicle' inherit14.h(23) : see declaration of 'color' inherit14.cpp(102) : error C2248: 'numcyl' : cannot access private member declared in class 'Vehicle' inherit14.h(22) : see declaration of 'numcyl' inherit14.cpp(112) : error C2248: 'GetColor' : cannot access private member declared in class 'Vehicle' inherit14.h(21) : see declaration of 'GetColor' inherit14.cpp(113) : error C2248: 'GetNumCyl' : cannot access private member declared in class 'Vehicle' inherit14.h(20) : see declaration of 'GetNumCyl' */