// inherit1.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program gives an introduction to Inheritance. // Key Pints: 1) What happen when an object is created from an inherited class. // 2) 'Parameterized' and 'non-parameterized' constructors. #include "inherit1.h" // Define the base class Geometric::Geometric( void ) { cout << "\n 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 << "\n Gm...Executing the 'parameterized' constructor of the base class. " << "\n"; left = lf; top = tp; bottom = bt; right = rt; } 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; } 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"; } 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; } void Rectangle::SetBoarderShape( boarder brd ) { boardershape = brd; } int main( void ) { cout << " Creating objects from the 3 classes will start" << "\n"; Geometric Gm; TwoDimension TD; Rectangle Rec; cout << "\n Finished Creating objects from the 3 classes. Will start using" << " the functions." << "\n\n"; cout << " Gm...Now the draw function of the Geometric class will start. " << "\n"; Gm.Draw( ); cout << "*************************************************\n\n"; cout << " TD...Now the draw function of the TwoDimension class will start. " << "\n"; TD.Draw ( ); cout << "*************************************************\n\n"; cout << " Rec...Now the draw function of the Rectangle class will start. " << "\n"; Rec.Draw ( ); cout << "*************************************************\n\n"; return 0; } /// The output of this progarm is: /* Creating objects from the 3 classes will start Gm...Executing the default constructor of the base class. Gm...Executing the default constructor of the base class. TD...Executing the default constructor of the TwoDimension class. Gm...Executing the default constructor of the base class. TD...Executing the default constructor of the TwoDimension class. Rec...Executing the default constructor of the Rectangle class. Finished Creating objects from the 3 classes. Will start using the functions. Gm...Now the draw function of the Geometric class will start. Gm...Function Draw in the base class is executing. Gm...This function will execute for all the derived classes! unless it is redefined!! ************************************************* TD...Now the draw function of the TwoDimension class will start. TD...Function Draw in the TwoDimension class is executing. TD...This function redefines the function Draw in the base class ************************************************* Rec...Now the draw function of the Rectangle class will start. TD...Function Draw in the TwoDimension class is executing. TD...This function redefines the function Draw in the base class ************************************************* */