Name of staff:- Prof. Bhandare P.S.
Chapter 4
Inheritance: Concept
of Reusability
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur
Chapter 4: Inheritance: Concept of Reusability 1
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Syllabus:
4.1 Introduction, defining a derived class, visibility modes & effects.
4.2 Types of Inheritance : Single, multilevel, multiple, hierarchical,
hybrid
4.3 Virtual base class, abstract class, constructors in derived class.
Assignment No 4.
Chapter 4: Inheritance: Concept of Reusability 2
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
About title of chapter :
The title of Inheritance gives idea about the new concept defined in C++.
In this chapter we will get information about concept of reusability.
In this chapter students are going to learn new programming approach.
Central Idea of chapter
Central Idea behind including this chapter in subject is that the student
will importance of inheritance for the classes in OOP.
Also Student will learn how programming can be organized in C++
language with the help of base classes & derived classes.
Importance of chapter :
Studying this chapter is important is because this chapter introduce new
concept in OOP language.
Also this chapter include defining inheritance & use of inheritance.
Also it will include different types of inheritance.
Objectives of the chapter
To study concepts inheritance.
To study inheritance definition & calling.
To study types of Inheritance.
To study virtual base class.
To study abstract class.
To study constructors in derived class.
Chapter 4: Inheritance: Concept of Reusability 3
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Introduction
Reusability is yet another important feature of 00P.
It is always nice if we could reuse something that already exists rather
than trying to create the same all over again.
It would not only save time and money but also reduce frustration and
increase reliability.
For instance, the reuse of a class that has already been tested, debugged
and used many times can save us the effort of developing and testing the
same again.
Fortunately, C++ strongly supports the concept of reusability.
Once a class has been written and tested, it can be adapted by other
programmers to suit their requirements.
This is basically done by creating new classes, reusing the properties of
the existing ones.
The mechanism of deriving a new class from an old one is called
inheritance (or derivation).
The old class is referred to as the base class and the new one is called the
derived class or subclass.
The derived class inherits some or all of the traits from the base class.
A class can also inherit properties from more than one class or from more
than one level.
A derived class with only one base class, is called single inheritance and
one with several base classes is called multiple inheritance.
On the other hand, the traits of one class may be inherited by more than
one class.
This process is known as hierarchical inheritance.
The mechanism of deriving a class from another 'derived class' is known
as multilevel inheritance.
The direction of arrow indicates the direction of inheritance.
Chapter 4: Inheritance: Concept of Reusability 4
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Defining Derived Classes
A derived class can be defined by specifying its relationship with the base
class in addition to its own details.
The general form of defining a derived class is:
The colon indicates that the derived-class-name is derived from the
base_class_name.
The visibility mode is optional and, if present, may be either private or
public.
The default visibility-mode is private.
Chapter 4: Inheritance: Concept of Reusability 5
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Visibility mode specifies whether the features of the base class are
privately derived or publicly derived.
Examples:
class ABC: private XYZ, // private derivation
{
members of ABC class
}
class ABC: public XYZ // public derivation
{
members of ABC
}
class ABC: XYZ // private derivation by default
{
members of ABC ;
}
When a base class is privately inherited by a derived class, 'public
members' of the base class become 'private members' of the derived class
and therefore the public members of the base class can only be accessed
by the member functions of the derived class.
They are inaccessible to the objects of the derived class.
Remember, a public member of a class can be accessed by its own objects
using the dot operator.
The result is that no member of the base class is accessible to the objects
of the derived class.
On the other hand, when the base class is publicly inherited, 'public
members' of the base class become 'public members' of the derived class
and therefore they are accessible to the objects of the derived class.
Chapter 4: Inheritance: Concept of Reusability 6
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
In both the cases, the private members are not inherited and therefore, the
private members of a base class will never become the members of its
derived class.
In inheritance, some of the base class data elements and member
functions are 'inherited' into the derived class.
We can add our own data and member functions and thus extend the
functionality of base class.
Single Inheritance
Let us consider a simple example to illustrate inheritance.
Program shows a base class B and a derived class D.
The class B contains one private data member, one public data member,
and three public member functions.
The class D contains one private data member and two public member
functions.
The class D is a public derivation of the base class B.
Therefore, D inherits all the public members of B and retains their
visibility.
Thus a public member of the base class B is also a public member of the
derived class D.
The private members of B cannot be inherited by a The class D.
Chapter 4: Inheritance: Concept of Reusability 7
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
class B
◦ {
◦ int a;
◦ public:
◦ int b;
◦ void get_ab();
◦ void get a();
◦ void show a();
◦ };
class D : private B // private derivation
◦ {
◦ int c;
◦ public:
◦ void mul();
◦ void display();
◦ };
The membership of the derived class D is shown in Fig.
In private derivation, the public members of the base class become
private members of the derived class.
Therefore, the objects of D can not have direct access to the public
member functions of B.
Example…
Chapter 4: Inheritance: Concept of Reusability 8
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Chapter 4: Inheritance: Concept of Reusability 9
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Ambiguity may also arise in single inheritance applications.
For instance, consider the following situation:
◦ class A
◦ {
◦ public:
◦ void display() {
◦ cout « "A\n”;
◦ }
◦ };
◦ class B : public A
◦ {
◦ public: void display(){
◦ cout « "B\e;
◦ };
In this case, the function in the derived class overrides the inherited
function and, therefore, a simple call to display() by B type object will
invoke function defined in B only.
However, we may invoke the function defined in A by using the scope
resolution operator to specify the class.
int main
{
B b; // derived class object
b.display(); // invokes display() in B
b.A::display(); // invokes display() in A
b.B::display(); // invokes display() in B
}
Chapter 4: Inheritance: Concept of Reusability 10
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Making a Private Member Inheritable
We have just seen how to increase the capabilities of an existing class
without modifying it.
We have also seen that a private member of a base class cannot be
inherited and therefore it is not available for the derived class directly.
What do we do if the private data needs to be inherited by a derived
class?
This can be accomplished by modifying the visibility limit of the private
member by making it public.
This would make it accessible to all the other functions of the program,
thus taking away the advantage of data hiding.
C++ provides a third visibility modifier, protected, which serve a limited
purpose in inheritance.
A member declared as protected is accessible by the member functions
within its class and any class immediately derived from it.
It cannot be accessed by the functions outside these two classes.
A class can now use all the three visibility modes as illustrated below:
◦ class A
◦ {
◦ private: //optional
◦ -------- // visible to member function
◦ -------- within class
◦ protected: // visible to member funtion
◦ --------- to its own class & derived class
◦ --------
◦ public:
◦ -------- //visible to all function in prog.
◦ --------
◦ }
Chapter 4: Inheritance: Concept of Reusability 11
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
When a protected member is inherited in public mode, it becomes
protected in the derived class too and therefore is accessible by the
member functions of the derived class.
It is also ready for further inheritance.
A protected member, inherited in the private mode derivation, becomes
private in the derived class.
Although it is available to the member functions of the derived class, it is
not available for further inheritance.
Chapter 4: Inheritance: Concept of Reusability 12
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The keywords private, protected, and public may appear in any order and
any number of times in the declaration of a class. For example.
class beta
{
◦ protected:
◦ public:
◦ private:
◦ public:
};
is a valid class definition.
However, the normal practice is to use them as follows:
class beta
{
◦ -------- // private by default
◦ --------
protected:
◦ --------
◦ --------
public:
◦ ---------
◦ --------
};
Chapter 4: Inheritance: Concept of Reusability 13
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
It is also possible to inherit a base class in protected mode (known as
protected derivation).
In protected derivation, both the public and protected members of the
base class become protected members of the derived class.
Table summarizes how the visibility of base class members undergoes
modifications in all the three types of derivation.
Multilevel Inheritance
It is not uncommon that a class is derived from another derived class.
The class A serves as a base class for the derived class B, which in turn
serves as a base class for the derived class C.
The class B is known as intermediate base class since it provides a link
for the inheritance between A and C.
The chain ABC is known as inheritance path.
Chapter 4: Inheritance: Concept of Reusability 14
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
A derived class with multilevel inheritance is declared as follows:
class A{---------}; // Base class
class B: public A {---------}; //B derived from A
class C: public B {---------}; // C derived from B
This process can be extended to any number of levels.
Let us consider a simple example.
Assume that the test results of a batch of students are stored in three
different classes.
Class student stores the roll-number, class test stores the marks obtained
in two subjects and class result contains the total marks obtained in the
test.
The class result can inherit the details of the marks obtained in the test
and the roll-number of students through multilevel inheritance.
Multiple Inheritance
A class can inherit the attributes of two or more classes.
This is known as multiple inheritance.
Multiple inheritance allows us to combine the features of several existing
classes as a starting point for defining new classes.
It is like a child inheriting the physical features of one parent and the
intelligence of another.
Chapter 4: Inheritance: Concept of Reusability 15
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
class M
◦ {
◦ protected : int m;
◦ public: void display() { }
◦ };
◦ class N
◦ {
◦ protected : int N;
◦ public: void display() { }
◦ };
class P :public M,public N
◦ {
◦ // body of P
◦ };
Ambiguity Resolution in Inheritance
Which display() function is used by the derived class when we inherit
these two classes?
We can solve this problem by defining a named instance within the
derived class, using the class resolution operator with the function as
shown below:
class P : public M, public N
{
public: void display(void) // overrides display() of M and N
{
M :: display();
Chapter 4: Inheritance: Concept of Reusability 16
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
}
};
We can now use the derived class as follows:
int main()
{
P p;
p.display();
}
Hierarchical Inheritance
We have discussed so far how inheritance can be used to modify a class
when it did not satisfy the requirements of a particular problem on hand.
Additional members are added through inheritance to extend the
capabilities of a class.
Another interesting application of inheritance is to use it as a support to
the hierarchical design of a program.
Many programming problems can be cast into a hierarchy where certain
features of one level are shared by many others below that level.
As an example, fig shows a hierarchical classification of students in a
university.
Chapter 4: Inheritance: Concept of Reusability 17
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
Hybrid Inheritance
There could be situations where we need to apply two or sore types of
inheritance to design a program.
Virtual Base Classes
We have just discussed a situation which would require the use of both
the multiple and multilevel inheritance.
Consider a situation where all the three kinds of inheritance, namely,
multilevel, multiple and hierarchical inheritance, are involved.
This is illustrated in Fig
Chapter 4: Inheritance: Concept of Reusability 18
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The 'child' has two direct base classes 'parentl' and 'parent2' which
themselves have a common base class 'grandparent'.
The 'child' inherits the traits of 'grandparent' via two separate paths.
It can also inherit directly as shown by the broken line.
The 'grandparent' is sometimes referred to as indirect base class.
Inheritance by the 'child' as shown in Fig. might pose some problems.
All the public and protected members of 'grandparent' are inherited into
'child' twice, first via 'parent 1' and again via `parent2'.
This means, 'child' would have duplicate sets of the members inherited
from 'grandparent'.
This introduces ambiguity and should be avoided.
The duplication of inherited members due to these multiple paths can be
avoided by making the common base class (ancestor class) as virtual base
class while declaring the direct or intermediate base classes as shown
below:
Chapter 4: Inheritance: Concept of Reusability 19
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
For example, consider again the student results processing system .
Assume that the class sports derives the roll_number from the class
student.
Then, the inheritance relationship will be as shown in Fig.
Abstract Classes
An abstract class is one that is not used to create objects.
An abstract class is designed only to act as a base class (to be inherited by
other classes).
It is a design concept in program development and provides a base upon
which other classes may be built.
In the previous example, the student class is an abstract class since it was
not used to create any objects.
Constructors in Derived Classes
As we know, the constructors play an important role in initializing
objects.
We did not use them earlier in the derived classes for the sake of
simplicity.
Chapter 4: Inheritance: Concept of Reusability 20
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
One important thing to note here is that, as long as no base class
constructor takes any arguments, the derived class need not have a
constructor function.
However, if any base class contains a constructor with one or more
arguments, then it is mandatory for the derived class to have a constructor
and pass the arguments to the base class constructors.
Remember, while applying inheritance we usually create objects using
the derived class.
Thus, it makes sense for the derived class to pass arguments to the base
class constructor.
When both the derived and base classes contain constructors, the base
constructor is executed first and then the constructor in the derived class
is executed.
In case of multiple inheritance, the base classes are constructed in the
order in which they appear in the declaration of the derived class.
Similarly, in a multilevel inheritance, the constructors will be executed in
the order of inheritance.
The constructor of the derived class receives the entire list of values as its
arguments and passes them on to the base constructors in the order in
which they are declared in the derived class.
The base constructors are called and executed before executing the
statements in the body of the derived constructor.
The general form of defining a derived constructor is:
The header line of derived-constructor function contains two parts
separated by a colon(:).
The first part provides the declaration of the arguments that are passed to
the derived-constructor and the second part lists the function calls to the
base constructors
Chapter 4: Inheritance: Concept of Reusability 21
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
basel(arglist), base2(arglist2) ... are function calls to base constructors
base 1( ), base2( ), ... and therefore arglist1, arglist2, ... etc. represent the
actual parameters that are passed to the base constructors.
Arglistl through ArglistN are the argument declarations for base
constructors base I through baseN.
ArglistD provides the parameters that are necessary to initialize the
members of the derived class.
E.g.
Chapter 4: Inheritance: Concept of Reusability 22
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
The constructors for virtual base classes are invoked before any non-
virtual base classes.
If there are multiple virtual base classes, they are invoked in the order in
which they are declared.
Any non-virtual bases are then constructed before the derived class
constructor is executed.
Chapter 4: Inheritance: Concept of Reusability 23
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
\ Chapter 4:
2 Marks:
1. State general format of defining derived class.
2. Give the types of inheritance for following diagram :
3. State any two access specifier with example.
4.
4Marks:
1. State different types of inheritances and describe any one.
2. How protected access specifier is different from private?
3. What is Virtual Base class? Describe with suitable diagram.
4. How hierarchical inheritance is achieved, explain with example.
5. Explain any two visibility modes with example.
6. Write a program to show use of single inheritance.
7. Explain hybrid inheritance with example.
8. Differentiate between multiple inheritance and multilevel
inheritance.
9. Explain different visibility modes used in inheritance.
10. Illustrate the hierarchical inheritance.
11. What is the use of Inheritance ? State different visibility
modes.
12. Explain concept of virtual base class with suitable
example.
Chapter 4: Inheritance: Concept of Reusability 24
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
13. Write a program to show use of multilevel inheritance for
following diagram to calculate the gross salary. gs = bs + 0.5 ∗ bs
+ 0.6 ∗ bs ;
14. Write a program to implement single inheritance from
following Figure No. 1 accept & display the data for one
table.
Chapter 4: Inheritance: Concept of Reusability 25
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
15. Identify the type of inheritance and implement it by
writing a program for the following Figure No. 2. Assume
suitable member functions.
16. Write a program to implement following inheritance :
17. What are the advantages of inheritance ?
Chapter 4: Inheritance: Concept of Reusability 26
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.
8 Marks
1. Write a program to demonstrate constructor in derived class
with respect to order of calling constructor and passing
parameters to base class constructor.
2. Explain various types of inheritance with example.
3. Explain the concept of virtual base class with it’s general
syntax & suitable example.
Chapter 4: Inheritance: Concept of Reusability 27
Subject: Object oriented programming (17432)