You are on page 1of 8

Session 13

Inheritance and Derived classes


Contents
Objectives
Introduction
13.1 What is Inheritance
13.1.1 How to declare a derived class
13.1.2 The Protected Keyword
13.2.3 A demonstration on creating a base class and derived classes

13.2 Constructors and destructors in inheritance


13.2.1 A demonstration on the use of constructors and destructors in inheritance
Summary

Objectives

After reading this lesson you should be able to:

 Understand the concepts behind inheritance


 Understand how to derive one class from another.
 Understand the correct use of the ‘protected’ access specifier.
 Understanding ho to use constructors and destructors in inheritance

Introduction
In the previous sessions you learnt how to declare classes and create objects using them.
In all the sessions we have created only a single class. In OOP it is possible to declare
many classes in a single programme.

Previously class data were declared as private and public. This session introduces the
protected keyword in more detail with examples.

13.1 What is Inheritance


Inheritance is a way of creating new classes using existing classes that has been already
defined. The new classes that are created are called derived classes and the already
existing class is called the base class. A base class will have more than one or more

1
derived class. The derived classes inherit all the characteristics (data member) and
behaviours (methods) of the base class.

Inheritance is sometimes called generalization, because the relationship between the base
class and the derived classes represent a hierarchy between classes of objects. The
concept of inheritance can be easily understood using simple real world examples.

Example 13.1: If there’s a base class called ‘birds’, derived classes like crow, parrot,
eagle etc can be created.

Birds Base class

Crow Parrot Eagle

Derived classes
Figure 13.1 – Base class and derived classes

In example 13.1, in order to create data members it is vital to consider the characteristics
of a bird. All the birds have feathers and beaks. Hence two attributes of the birds class
can be ‘feathers’ and ‘beaks’. If birds like crow, parrot or eagle are derived from the birds
class they inherit those attributes as well. As a result these three derived classes have the
characteristics or data members, ‘feathers’ and ‘beaks’. In the real world also it is obvious
that those birds have feathers and beaks.

In considering the behavior, birds can fly; they eat using the beak etc. These can be
defined as methods of the base class. Then all the derived classes inherit these methods
and the programmer doesn’t have to state explicitly that the crow, parrot and the eagle fly,
because it inherit that from the bird class.

The derived classes can be further divided. There are several types of parrots. For
example Eclectus parrot, African gray parrot, Hawkheaded parrot etc. They also have all
the characteristics of a parrot but they differ in other aspects like color, type of food they
eat etc. Then the classes like Eclectus parrot, African gray parrot, Hawkheaded parrot
becomes the derived classes of parrot and the parrot class in the base class for these
derived classes.

2
In a similar manner, using the crow class and the eagle class, more derived classes can be
created since there are different types of crows and eagles.

13.1.1 How to declare a derived class

A derived class must be declared before it is used. Therefore it is vital to specify what
class it derives from. The general format of declaring a derived class is:

class derived_class_name : type_of_derivation base_class_name {


//member data
:
:
//methods
:
:
};

The keyword ‘class’ followed by the name of the derived class, needs to be specified
first, just like declaring a normal class. Then a colon after the class name should be used
followed by type of derivation (public, private or protected) and then the class from
which it derives.

Example 13.2 : class Parrot : public bird{

//member data

//methods
};

13.1.2 The Protected Keyword


When the data members are declared using the protected access specifier, the members
are accessible from within the methods of the same class and from the members of their
derived classes. Protected data members and functions are fully visible to derived classes,
but are otherwise private.

The below given table summarizes different types of access specifiers according to which
methods can access them:

Access public protected private


members of the same class yes yes yes
members of derived classes yes yes no
not members yes no no
Table 13.1 – Access specifier

3
Note: Where "not members" represent any access from outside the class, such as from
main(), from another class or from a function.

13.2.3 A demonstration on creating a base class and derived


classes

1. #include<iostream.h>
2. #include<conio.h>

//base class
3. class Polygon {
4. protected:
5. int width, height;

6. public:
7. void set_values (int w, int h){
8. width=w;
9. height=h;
10. }
11. };

//derived class- Rectangle


12. class Rectangle: public Polygon {
13. public:
14. float area (){
15. float answer=width * height;
16. return answer;
17. }
18. };

//derived class- Rectangle


19. class Triangle: public Polygon {
20. public:
21. float area (){
22. float answer=0.5 * width * height;
23. return answer;
24. }
25. };

26. void main () {


27. clrscr();

//creating objects
28. Rectangle rect;

4
29. Triangle trg;
30. rect.set_values (2,6);
31. trg.set_values (3,5);

32. cout << "Area of the rectangle is "<<rect.area() << endl;


33. cout << "Area of the triangle is "<<trg.area() << endl;
34. getch();
35. }
Output:
Area of the rectangle is 12
Area of the triangle is 7.5

A polygon can be described as a closed circuit, composed of a finite sequence of straight


lines. A rectangle and a triangle have all the above characteristics plus their own
characteristics. A rectangle has four straight lines where the triangle has three straight
lines. A polygon has a height and width and a rectangle and a triangle also has these
characteristics. Hence a hierarchy can be created as given below.

Polygon

Rectangle Triangle

Figure 13.2 – A simple inheritance

From line 3 to 11, the base class called ‘Polygon’ has been created. Width and height has
been declared as ‘protected’ data members and the Polygon class has a public method
called set values. The derived classes rectangle and triangle inherit these data members
and the method. The main method accesses these data members through public set
method.

Scope of the class rectangle is from line 12 to 18. Rectangle class has a public method
called area, and by multiplying the width and the height (line 15) the area of the triangle
has been calculated. Scope of the class triangle is from line 19 to 25. Class triangle also
has the same method. Since the area of a triangle is calculated differently, it is calculated
accordingly by dividing the width and height by two (line 22).

An object has been created, from each of the two derived classes and width and height
has passed as arguments to the set values method. (Refer line 28 to 31). In line 32 and 33
the area method has been called for each object and the results has been displayed.

5
13.2 Constructors and destructors in inheritance
It is possible to create constructors and destructors for the base class as well as for the
derived classes. This concept has been explained in depth using the example given below.

13.2.1 A demonstration on the use of Constructors and


destructors in inheritance

1. #include<iostream.h>
2. #include<conio.h>

3. class Polygon {
4. protected:
5. float width, height;
6. public:

7. Polygon(){
8. cout<<"Polygon constructor\n";
9. width=7;
10. height=3;
11. }

12. ~Polygon(){
13. }
14. };

15. class Rectangle: public Polygon {


16. public:

17. Rectangle(){
18. width=6;
19. height=10;
20. }

21. Rectangle (int w, int h){


22. width=w;
23. height=h;
24. }

25. Rectangle (float w, float h){


26. width=w;
27. height=h;
28. }

29. ~Rectangle(){

6
30. }

31. float area (){


32. float answer = width * height;
33. return answer;
34. }
35. };

36. class Triangle: public Polygon {


37. public:

38. float area (){


39. float answer=0.5 *width * height;
40. return answer;
41. }
42. };

43. void main () {


44. clrscr();

45. Rectangle rect1;


46. Triangle trg1;
47. Rectangle rect2(1,5);
48. Rectangle rect3(1.2f,5.6f);

49. cout<<endl;
50. cout << "Area of rectangle1 = "<<rect1.area() << endl;
51. cout << "Area of triangle1 = "<<trg1.area() << endl;
52. cout << "Area of rectangle2 = "<<rect2.area() << endl;
53. cout << "Area of rectangle3 = "<<rect3.area() << endl;
54.
55. getch();
56. }

Output:

Polygon constructor
Polygon constructor
Polygon constructor
Polygon constructor

Area of rectangle1 = 60
Area of triangle1 = 10.5
Area of rectangle2 = 5
Area of rectangle3 = 6.72

7
In the example 13.2.1, class polygon is the base class (Refer line 3 to 14) and there are
two derived classes call class rectangle (line 15 to 35) and class triangle (line 36 to 42).
The base class has a default constructor (line 7 to 11) and the derived class rectangle has
two general constructors and a default constructor. Inside the main method four objects
have been created and the method named area() is called.

Generally in programme execution, initially the constructor of the base class is called and
then the constructor of the derived class will be called completing the creation of the
object. This is evident from the programme output. The message “Polygon constructor” is
displayed four times because each time a object is created the default constructor of the
base class (Polygon()) is called.

When the object rect1 is created (refer line 45) the default constructor of the base class is
called first and then the default constructor of the rectangle class is called overriding the
base constructor. Hence the answer is 60. When the object trg11 is created (refer line 46)
the default constructor of the base class is called and since there are no constructors for
the traingle class, the default base class constructor assign the width and height to 7 and 3
respectively and the answer is 10.5.

In the creation of the objects rect2 (line 47) and rect3 (line 48) the base class constructors
will be called first and then the general constructors of the rectangle class will be called
accordingly.

Summary

 Inheritance is a way of creating new classes using existing classes that has been
already defined.

 When the data members are declared using the protected access specifier, the
members are accessible from within the methods of the same class and from the
members of their derived classes.

 It is possible to create constructors and destructors for the base class as well as for
the derived classes.

 Generally in programme execution, initially the constructor of the base class is


called and then the constructor of the derived class will be called completing the
creation of the object.

You might also like