Lecture 2.1
Lecture 2.1
BCA/BCA(ARVR)/B.Sc.(CS)
OBJECT ORIENTED PROGRAMMING
23CAH-111/23SCH-151/23CAH-151
UNIT-2
2
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is
called Base or Parent class. The New class is called as Child or Derived Class.
Advantages
• It permits code reusability. So, save time and increase the program reliability.
• A programmer can use a class created by another person or company without
modifying it derive other classes from it that are suited to particular situations
• Improve Program Reliability
• It Permits code sharing
• The base class need not be changed but can be adapted to suit the requirements in
different applications.
3
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is
called Base or Parent class. The New class is called as Child or Derived Class.
4
WHY AND WHEN TO USE
INHERITANCE?
• Consider a group of vehicles. You need to create classes for Bus, Car and Truck.
The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the
three classes. If we create these classes avoiding inheritance then we have to write
all of these functions in each of the three classes as shown in below figure:
5
WHY AND WHEN TO USE
INHERITANCE?
• If we create a class Vehicle and write these three functions in it and inherit the rest
of the classes from the vehicle class, then we can simply avoid the duplication of
data and increase re-usability
7
IMPLEMENTING INHERITANCE IN C++
• EXAMPLE
11
Pre-requisites (Public, Private and Protected
Inheritance)
1. Public Inheritance
Consider the dummy code given below for inheritance.
class B : public A
{
}:
The line class B : public A tells the compiler that we are inheriting class A in
class B in public followings :
(a) All the public members of class A becomes public members of class B.
(b) All the protected members of class A becomes protected members of class B.
(c) Private members are never inherited.
12
Private Inheritance
Consider the dummy code given below for inheritance:
class B : private A
{
}:
The line class B : private A tells the compiler that we are inheriting class A in class
B in private mode. In private mode inheritance note the following points:
(a) All the public members of class A becomes private members of the class B.
(b) All the protected members of the class A becomes private members of class B.
(c) Private members are never inherited.
13
Private Inheritance
14
Protected Inheritance
Consider the dummy code given below for inheritance:
class B : protected A
{
}:
The line class B : protected A tells the compiler that we are inheriting class A in
class B in protected mode. In protected mode inheritance note the following points:
(a) All the public members of class A becomes protected members of class B.
(b) All the protected members of class A becomes protected members of class B.
(c) Private members are never inherited.
15
NOTE:
•Note that: A class A is inherited in class B in public mode, all protected members
of class A becomes protected for class B. Now if this class B is inherited to some
new class C, then these protected members inherited from A will be available from
A will be available to class C, in whatever mode you inherit class B to class C.
•Initially let us assume that you inherited class A into class B in private mode, then
all protected members of class A becomes private for class B. They can be used
inside the class B. But if you inherit class B into new class C then these members
won’t be available in C as private members are never inherited.
16
TYPES OF INHERITANCE
1. SINGLE INHERITANCE
3. MULTI-LEVEL INHERITANCE
2. MULTIPLE INHERITANCE
5. HYBRID/VIRTUAL INHERITANCE
4. HIERARCHICAL INHERITANCE
20
Figure 4.8 Program made on Dev-C++
MULTIPLE INHERITANCE
CLASS
21
Figure 4.8 Program made on Dev-C++
MULTILEVEL INHERITANCE
28
APPLICATIONS
• You use nested classes to reflect and to enforce the relationship between two
classes. You should define a class within another class when the nested class
makes sense only in the context of its enclosing class or when it relies on the
enclosing class for its function. For example, a text cursor might make sense only
in the context of a text component.
29
Frequently Asked Questions: -
1. Is inheritance important to C++?
2. When would I use inheritance?
3. How do you express inheritance in C++?
4. Is it okay to convert a pointer from a derived class to its base class?
5. What’s the difference between public, private, and protected?
6. Why can’t my derived class access private things from my base class?
7. How can I protect derived classes from breaking when I change the internal parts of
the base class?
8. I’ve been told to never use protected data, and instead to always use private data
with protected access functions. Is that a good rule?
9. Okay, so exactly how should I decide whether to build a “protected interface”?
30
SUMMARY
1. Inheritance is the mechanism of deriving new class from an existing class. The old class and new
class is known as in pair : super-sub, base-derived, parent-child.
2. Inheritance provides the idea of reusability. Code once written can be used again and again in
several different classes.
3. C++ provides support for: single level and multilevel inheritance, multiple inheritance,
hierarchical inheritance, hybrid inheritance.
4. Private members are never inherited.
5. Virtual base class is used to avoid duplicity of data members into the target derived class.
31
SUMMARY (Continued)
• In an inheritance hierarchy constructor of base class is called first,
then constructor of derived class is called.
32
• Defining derived class,
• modes of inheritance,
• types of inheritance,
CONTENTS • ambiguity in inheritance,
• virtual base class,
• Function overriding,
The capability of a class to derive properties and • Member Classes: Nesting of Classes.
characteristics from another class is called
Inheritance.
Inheritance is one of the most important feature
of Object Oriented Programming
33
AMBIGUITY IN INHERITANCE
•What is ambiguity?
•In multiple inheritance when two base classes having same name for their functions and data, ambiguity arises
when they are used in the derived class.
•For example, consider the two classes A and B having same function signature for function named show like
void show().
•Both the classes are inherited in the third class C.
•How class C can use show() of class A and show of class B individually?
•Simply accessing show function in class C creates confusion as to which show we want to refer; show of class
A or show of class B.
34
SCOPE RESOLUTION OPERATOR
•In these situations we can use the scope resolution operator with class names to
functions for accessing the function of individual class. Thus inside the member
functions of class C we can access show of class A as A : : show( ) and show of
class B as B : : show. If function show is also defined in the class C and all show are
public then they can access in the main using an object of class C as:
C O1;
O1.A : :show( ); //calls show of A class
O1.B : :show( ); //calls show of B class
O1.show( ); // calls show of C class
35
EXAMPLE TO RESOLVE AMBIGUITY IN
MULTIPLE INHERITANCE
36
37
EXPLANATION
•In the program we have three classes : A, B and C. In all the classes we have a data
member num of int type and function show with same signature. The num of A and
B are accessed in the class C as
Even though they are not static members. In the main the show of A and B classes
are accessed with an object o1 of class C as :
O1.A : : show( );
O1.B : : show ( );
38
HIERARCHICAL INHERITANCE
• When number of classes have a direct access to one common class, then this type
of hierarchical inheritance is visible.
• The main base class can be modified alone if required and all derived classes will
see that effect.
39
EXAMPLE # 1 OF HIERARCHICAL
INHERITANCE
43
44
EXPLANATION
• In the second program:
• we have university class which has just data member, a char array of 40 characters named uname.
• This class is inherited by two classes’ college1 and college2.
• The two classes display their name and the university to which they are affiliated.
45
AMBIGUITY IN HYBRID INHERITANCE
46
AMBIGUITY IN HYBRID INHERITANCE
• This can be achieved by preceding the base class’ name with the word virtual.
• Virtual base classes are created before non-virtual base classes, which ensures all bases get created before
their derived classes.
• Suppose you have two derived classes B and C that have a common base class A,
• You can declare the base class A as virtual to ensure that B and C share the same sub-object of A.
51
VIRTUAL BASE CLASS
•Consider the situation where we have one class A.
•This class A is inherited by two other classes B and C.
•Both these classes are inherited in a new class D.
•This is as shown in figure given below. In dummy code form this is shown below:
52
VIRTUAL BASE CLASS (Continued)
From the previous figure we can see that
• When any data/ function members of class A is accessed by an object of class D, ambiguity arises as to
which data/function members would be called ?
• To resolve this ambiguity when class A is inherited in both class B and class C, it is declared as virtual base
class by placing the keyword virtual as:
53
SOLUTION- VIRTUAL BASE CLASS
55
VIRTUAL BASE CLASS
•Virtual can be written before or after the public.
•Only one copy of data/function member will be copied to class C and class B and
class A becomes virtual base class.
•Virtual base classes offer a way to save space and avoid ambiguities in class
hierarchies that uses multiple inheritances.
•When a base class is specified as a virtual base, it can act as an indirect base more
than once without duplication of its data members.
•A single copy of its members is shared by all the base classes that use it as a virtual
base.
56
PROGRAM TO ILLUSTRATE VIRTUAL BASE CLASS
57
EXPLANATION:
58
Assessment Questions
1. (a) Define ambiguity in inheritance.
(b) How to resolve the problem of ambiguity in C++?
(c) Write a program to resolve ambiguity in C++?
2. What are the rules of resolving ambiguity?
3. Write a C++ program to demonstrate virtual base class.
4. Write a C++ program to differentiate ambiguity through different modes of inheritance.
59
APPLICATIONS/RULES
• Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a
given class appearing in an inheritance hierarchy when using multiple inheritances.
• virtual can be written before or after the public.
• Now only one copy of data/function member will be copied to class C and class B and class A becomes the
virtual base class.
• Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple
inheritances.
• When a base class is specified as a virtual base, it can act as an indirect base more than once without
duplication of its data members.
• A single copy of its data members is shared by all the base classes that use virtual bas
60
Frequently Asked Questions: -
1. What is ambiguity?
2. When a problem of ambiguity occurs?
3. What special considerations needs to be taken when virtual
inheritance is used?
4. What are the points to consider when a class is inherited that uses
virtual inheritance?
5. Why can’t my derived class access private things from my base
class?
61
SUMMARY
1. When you derive classes, ambiguities can result if base and derived classes have members with the same
names. The declaration of a member with an ambiguous name in a derived class is not an error. ... The
ambiguity is only flagged as an error if you use the ambiguous member name.
2. Solution of ambiguity in multiple inheritance: The ambiguity can be resolved by using the scope resolution
operator to specify the class in which the member function lies as given below: obj. a :: abc(); This
statement invoke the function name abc() which are lies in base class a
3. We remove ambiguity occurred in the case of hybrid inheritance Multipath Inheritance
4. There are two ways to avoid c++ ambiguity.
1. Avoid ambiguity using scope resolution operator.
62
SUMMARY (Continued)
5. Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given
class appearing in an inheritance hierarchy when using multiple inheritances.
6. When two or more objects are derived from a common base class, we can prevent multiple copies of the
base class being present in an object derived from those objects by declaring the base class as virtual when
it is being inherited. Such a base class is known as virtual base class.
63
Discussion forum.
• What are the various inheritance form?
• Examples of various types of iinheritance.
64
REFERENCES
TEXT BOOKS
T1 E Balagurusamy., “Object Oriented Programming in C++”, Tata McGraw-Hill.
T2 Robert Lafore, “Object Oriented Programming in C++”, Waite Group.
REFERENCE BOOKS
R1 Herbert Schildt , “C++- The Complete Reference”, Tata McGraw-Hill 2003, New
Delhi.
R2 Bjarne Stroustrup: “The C++ Programming Language” (4th Edition). Addison-
Wesley.
R3 Ravichandran , “Programming with C++”,Tata McGraw-Hill Education.
R4 Joyce M. Farrell,” Object Oriented Programming Using C++”, Learning.
R5 Programming Languages: Design and Implementation (4th Edition), by Terrence W.
Pratt, Marvin V. Zelkowitz, Pearson.
R6 Programming Language Pragmatics, Third Edition, by Michael L. Scott, Morgan
Kaufmann.
Websites:
1. https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
2. https://www.geeksforgeeks.org/virtual-base-class-in-c/?ref=gcse
65
3. https://www.studytonight.com/cpp/types-of-inheritance.php