Professional Documents
Culture Documents
Inheritance
(OO C++, topic 7)
#1.Textbook:
[3] Deitel & Deitel, “C++ How to program” (3rd edition, Prentice Hall, 2001, ISBN 0-13-089571-7,
pb, pp. 1168), chapter 9, pages 576-612.
#2. Objective ([3] p. 576): To be able to create new C++ classes by inheriting from existing
classes, to understand the notions of base classes and derived classes.
#4. Inheritance: base classes and derived classes ([3] pp. 579-581):
Inheritance forms tree-like structures. For example shape divides into 2Ds and 3Ds, in
turn 2Ds divide into circles, squares, triangles, etc.
Inheritance (a public kind) is indicated by the following syntax:
Class CommissionWorker : public Employee
{
…
};
This specifies that the subclass (derived-class) CommissionWorker is derived from the
superclass (base-class) Employee. This inheritance is public. (In private and protected
inheritances the keyword public must be replaced by either private or protected.) The public
inheritance means that the derived-class objects inherit all public data members and all public
member functions from the base-class.
1
#7. Using constructors and destructors in derived classes: ([3] pp.593-597)
A derived class inherits its base class’ members. Thus when an object of a derived class
is installed, the base class’ constructor must be called to initialise the base-class members of the
derived-class object. Here is an example of a base class initalizer used in [3] p. 590, Fig. 9.5:
The above example shows a constructor for derived-class named HourlyWorker, which is calling
a constructor for the base class named Employee.
//---------------------------------------------------------------------------
//Derives class Children from base class Parents
//(means produces a lot of parent and child objects,
//setting or changing various parameters for some of them)
//Adds member function 'facial_features' and 'getchild_no'
//By Dr Jan Pajak, at ... on ....
//---------------------------------------------------------------------------
#include <condefs.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;
//-------------------------
class Parents
{
public:
Parents(); //default constructor
~Parents(); //default destructor
Parents(int); //parametrised constructor
Parents(const Parents&); //copy constructor
void Mariage_no(){facial_features++;}
int getfacial_features(){return facial_features;} //return facial_features
static int spouse_number; //data variable for all objects of the class
2
private:
int facial_features;
int* ptrInteger; //some heap value used just for demo purposes
};
//---------------------------------------------------------------------------
class Children:public Parents
{
public:
Children(); //constructor
~Children(); //destructor
Children(int); //initial child_no
Children (int, int); //initial child_no, facial_features
void facial_features(int); //set facial_features
int getchild_no(){return child_no;}
static int children_counting;
private:
int child_no;
};
//---------------------------------------------------------------------------
Parents::Parents()
{ //default constructor
Parents::spouse_number++;
facial_features = 0;
ptrInteger = new int;
cout<<"\nParents Constructor for parent no.: "
<<Parents::spouse_number<<endl;
}
Parents::Parents (int sp)
{ //Parametrised constructor
Parents::spouse_number++;
facial_features=sp;
ptrInteger = new int;
cout<<"\nParents Constructor for parent no.: "
<<Parents::spouse_number<<endl;
}
Parents::Parents (const Parents &child) //copy constructor
{ //child refers to the Parents being copied
Parents::spouse_number++;
Parents::facial_features++;
cout<<"\nCopy Constructor for parent no.: "
<<Parents::spouse_number<<endl;
ptrInteger = new int; //create a new int for the copy
//and assigns value of the integer being copied
*ptrInteger = *child.ptrInteger;
}
Parents::~Parents()
{ //cleanup by freeing memory allocated to ptrInteger
delete ptrInteger;
cout<<"\nParents Destructor for parent no.: "
<<Parents::spouse_number<<endl;
3
}
int Parents::spouse_number = 0; //init static class variable - outside class!
//---------------------------------------------------------------------------
Children::Children()
{ //default constructor
Children::children_counting++;
children_counting = 0;
cout<<"\nDefault Constructor Children "<<Children::children_counting<<endl;
}
Children::Children(int no_param)
{ //parametrised constructor
Children::children_counting++;
children_counting = no_param;
cout<<"\nParameter Constructor Children " <<Children::children_counting<<endl;
}
Children::Children(int facial_features_param, int no_param):Parents(facial_features_param)
{ //double parametrised constructor: passing facial_features to base class
Children::children_counting++;
children_counting = no_param;
cout<<"\nDounble Parameter Constructor Children "
<<Children::children_counting<<endl;
}
Children::~Children()
{ //default destructor
cout<<"\nChildren Destructor for a child no: "
<<Children::children_counting<<endl;
Children::children_counting--;
}
void Children::facial_features (int facial_features_param)
{
facial_features_param=child_no;
}
int Children::children_counting=0; //initialise static class variable
//---------------------------------------------------------------------------
void displayEnd(void); //displays ending
void foo(Parents child_param){} //does nothing but illustrates the declaration
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
Parents mother_1;
Parents mother_2(20);
mother_1.Mariage_no();
Parents mother_3=mother_1;//invokes copy constructor to copy mother_1 to mother_3
foo (mother_1); //invokes copy constructor to copy mother_1 to child_param
//pass-by-value
Parents* ptrParents = new Parents();
4
//put a parent on the heap
//use ptrParents here ...
delete ptrParents; //to free the heap memory
Parents father;
Parents father_2(10); //initialise facial_features
Children baby;
Children baby2(5); //initialize child_no=5
Children baby3(10, 5); //initialise facial_features, child_no=5
#10. Practical exercise that tests your skills on inheritance and polymorphism.
Try to modify the program provided above in such a manner, that every separate line of
display that this program produces includes a name of the object for which a given display was
made.