// inherit3.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna /// This program will not work. why? Check inherit3.h // Key Points: 1) Access Rights. // - What is visible to an inherited class? What is not? #include "inherit3.h" // Define the base class Geometric::Geometric( void ) { cout << " Gm...Executing the default constructor of the base class. " << "\n"; top = left = bottom = right = 0; } Geometric::Geometric( int lf , int tp, int bt, int rt ) { cout << " Gm...Executing the 'parameterized' constructor of the base class. " << "\n"; left = lf; top = tp; bottom = bt; right = rt; cout << " Gm...Left is: " << left << " , top: is " << top << " , bottom is: " << bottom << " and right is: " << right << "\n"; } void Geometric::Draw( void ) { cout << " Gm...Function Draw in the base class is executing." << "\n"; cout << " Gm...This function will execute for all the derived classes!" << " unless it is redefined!! " << "\n"; } void Geometric::Erase( void ) { cout << " Gm...Function Erase in the base class is executing." << "\n"; cout << " Gm...This function will execute for all the derived classes!" << " unless it is redefined!! " << "\n"; } void Geometric::Move( void ) { cout << " Gm...Function Erase in the base class is executing." << "\n"; cout << " Gm...This function will execute for all the derived classes!" << " unless it is redefined!! " << "\n"; } // define the TwoDimension class TwoDimension::TwoDimension( void ) { cout << " TD...Executing the default constructor of the TwoDimension class. " << "\n"; fillpattern = 0; } TwoDimension::TwoDimension( int lf, int tp, int bt, int rt, int fill ): Geometric( lf, tp, bt, rt ) { cout << " TD...Executing the 'parameterized' constructor of the" << " TwoDimension class. " << "\n"; fillpattern = fill; cout << " TD...Left is: " << left << " , top: is " << top << " , bottom is: " << bottom << " and right is: " << right << "\n"; } void TwoDimension::Draw( void ) { cout << " TD...Function Draw in the TwoDimension class is executing." << "\n"; cout << " TD...This function redefines the function Draw in the base class" << "\n"; } // define the Rectangle class Rectangle::Rectangle( void ) { cout << " Rec...Executing the default constructor of the Rectangle class. " << "\n"; boardershape = bolddashed; } Rectangle::Rectangle( int lf , int tp, int bt, int rt, int fill ): TwoDimension( lf, tp, bt, rt, fill ) { cout << " Rec...Executing the 'parameterized' constructor of the" << " Rectangle class. " << "\n"; boardershape = bolddashed; cout << " Rec...Left is: " << left << " , top: is " << top << " , bottom is: " << bottom << " and right is: " << right << "\n"; } void Rectangle::SetBoarderShape( boarder brd ) { boardershape = brd; } int main( void ) { Geometric Gm( 2, 5, 8, 10 ); cout << " Gm...Now the draw function of the Geometric class will start. " << "\n"; Gm.Draw( ); cout << "*************************************************\n\n"; TwoDimension TD( 4, 7, 9, 12, 1 ); cout << " TD...Now the draw function of the TwoDimension class will start. " << "\n"; TD.Draw( ); TD.Erase( ); cout << "*************************************************\n\n"; Rectangle Rec( 5, 11, 16, 20, 2 ); cout << " Rec...Now the draw function of the Rectangle class will start. " << "\n"; Rec.Draw( ); Rec.Move( ); cout << "*************************************************\n\n"; return 0; } /// The result of compliling the program: /* On VC++, the errors would like like: Compiling... inherit3.cpp inherit3.cpp(95) : error C2248: 'left' : cannot access private member declared in class 'Geometric' \inherit3.h(19) : see declaration of 'left' inherit3.cpp(95) : error C2248: 'top' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'top' inherit3.cpp(96) : error C2248: 'bottom' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'bottom' inherit3.cpp(96) : error C2248: 'right' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'right' inherit3.cpp(132) : error C2248: 'left' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'left' inherit3.cpp(133) : error C2248: 'top' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'top' inherit3.cpp(134) : error C2248: 'bottom' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'bottom' inherit3.cpp(134) : error C2248: 'right' : cannot access private member declared in class 'Geometric' inherit3.h(19) : see declaration of 'right' /* /* On Unix, the errors would be similar. They would look like: CC: "inherit3.cpp", line 88: error: TwoDimension::TwoDimension() cannot access Geometric::left: private member (1299) CC: "inherit3.cpp", line 88: error: TwoDimension::TwoDimension() cannot access Geometric::top: private member (1299) CC: "inherit3.cpp", line 88: error: TwoDimension::TwoDimension() cannot access Geometric::bottom: private member (1299) CC: "inherit3.cpp", line 88: error: TwoDimension::TwoDimension() cannot access Geometric::right: private member (1299) CC: "inherit3.cpp", line 128: error: Rectangle::Rectangle() cannot access Geometric::left: private member (1299) CC: "inherit3.cpp", line 128: error: Rectangle::Rectangle() cannot access Geometric::top: private member (1299) CC: "inherit3.cpp", line 128: error: Rectangle::Rectangle() cannot access Geometric::bottom: private member (1299) CC: "inherit3.cpp", line 128: error: Rectangle::Rectangle() cannot access Geometric::right: private member (1299) */