You are on page 1of 5

Dr Jan Pajak

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.

#3. Introduction ([3] pp. 577-579):


-Inheritance is a form of software reusability in which new classes are created from existing
classes by absorbing their attributes and behaviours.
-Polymorphism allows to reuse code by creating new classes that override some attributes and
behaviours of old ones.
-While using inheritance and polymorphism, instead of writing completely new classes, a
programmer reuses code of previously defined base class (also called a superclass).
-The new class that inherits attributes and methods from an old class is called a derived class
(also called a subclass).
-C++ offers three kinds of inheritance, namely: public, protected, and private.

#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.

#5. Protected members: ([3] p. 581)


-A base class’ public members are accessible by all functions in the program.
-A base class’ private members are accessible only by member functions and friends of the base
class.
-A base class’ protected members may be accessed only by members and friends of the base
class, and members and friends of derived classes.

#6. Overriding base-class member in a derived class: ([3] pp. 587-592)


A derived class can override a base-class member function by supplying a new version of
that function with the same signature (if the signature were different, this would be function
overloading rather than function overriding). An example of such overriding is provided in [3],
pages 588 and 589, Fig. 9.5.

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:

HourlyWorker::HourlyWorker(…) : Employee (first, last) //This calls base-class constructor

The above example shows a constructor for derived-class named HourlyWorker, which is calling
a constructor for the base class named Employee.

#8. Implicit Derived-class object to Base-class object conversion:


The derived-class type and the base-class type are different, in spite that a derived-class
object “is also a” base-class object. With public inheritance, a pointer to a derived-class object
may be converted into a pointer to a base-class object. Example how to accomplish this is shown
in Fig. 9.9. on page 603.

#9. Example of a driver program that illustrates inheritance and polymorphism.


For an example of such driver program, let us consider a program that creates a base-
class and then creates a derived class from it. The base class is a class named “Parents”. In turn a
derived class is called “Children”. The program does nothing else but creates various objects that
belong to a base class and to derived class. Here is the code of that program in C++ (console
notation):

//---------------------------------------------------------------------------
//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

baby.facial_features(9); //set baby child_no to 9


//output objects and there data members
cout<<"\nfather facial_features "<<father.getfacial_features()<<endl;
cout<<"\nfather_2 facial_features "<<father.getfacial_features()<<endl;
cout<<"\nbaby facial_features "<<father.getfacial_features()
<<", child_no "<<baby.getchild_no()<<endl;
cout<<"\nbaby2 facial_features "<<baby2.getfacial_features()
<<", child_no "<<baby2.getchild_no()<<endl;
cout<<"\nbaby3 facial_features "<<baby3.getfacial_features()
<<", child_no "<<baby3.getchild_no()<<endl;
displayEnd();
return 0;
}
//---------------------------------------------------------------------------
void displayEnd(void) //This function terminates the program
{
textcolor(9);
cprintf("\nFinish!\n");
cout<<"\nPress any key to continue ...";
getch();
}
//---------------------------------------------------------------------------

#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.

You might also like