0% found this document useful (0 votes)
4 views

Chapter-4(2) Ambiguity in inheritance, virtual base class

The document discusses inheritance in Object Oriented Programming, focusing on ambiguity in multiple inheritance and solutions such as the scope resolution operator and virtual base classes. It explains how ambiguity arises when derived classes have members with the same names and how virtual base classes can prevent multiple instances of a base class in an inheritance hierarchy. Additionally, it provides examples and assessment questions related to these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter-4(2) Ambiguity in inheritance, virtual base class

The document discusses inheritance in Object Oriented Programming, focusing on ambiguity in multiple inheritance and solutions such as the scope resolution operator and virtual base classes. It explains how ambiguity arises when derived classes have members with the same names and how virtual base classes can prevent multiple instances of a base class in an inheritance hierarchy. Additionally, it provides examples and assessment questions related to these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

• 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
characteristics from another class is called
Inheritance.
Classes.
Inheritance is one of the most important feature
of Object Oriented Programming

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

2
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

3
EXAMPLE TO RESOLVE AMBIGUITY IN
MULTIPLE INHERITANCE

4
5
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
A : : num and B : :num
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 ( );

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

7
EXAMPLE # 1 OF HIERARCHICAL
INHERITANCE

Figure 4.11 Program made on Dev-C++ 8


HIERARCHICAL INHERITANCE

Figure 4.12 Program made on Dev-C++ 9


HIERARCHICAL INHERITANCE

Figure 4.13 Program made on Dev-C++ 10


EXAMPLE # 2

11
12
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.

13
AMBIGUITY IN HYBRID INHERITANCE

14
AMBIGUITY IN HYBRID INHERITANCE

Figure 4.19 Program made on Dev-C++


15
AMBIGUITY IN HYBRID INHERITANCE

Figure 4.20 Program made on Dev-C++


16
AMBIGUITY IN HYBRID INHERITANCE

Figure 4.21 Program made on Dev-C++


17
OUTPUT

Figure 4.22 Program output made on Dev-C++


18
SOLUTION- VIRTUAL BASE CLASS
• 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.

• 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 also have another class D that inherits from B and C.

• You can declare the base class A as virtual to ensure that B and C share the same
sub-object of A.
19
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:

20
VIRTUAL BASE CLASS (Continued)

From the previous figure we can see that

• data members/ functions of class A are inherited twice to class D.

• One through class B and second through class C.

• 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 ?

• One inherited through B or the other inherited through C.

• This confuses compiler and it flashes error message.

• 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:
21
SOLUTION- VIRTUAL BASE CLASS

Figure 4.23 Program 4 made on Dev-C++


22
OUTPUT

Figure 4.24 Program output made on Dev-C++

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

24
PROGRAM TO ILLUSTRATE VIRTUAL BASE CLASS

25
EXPLANATION:

The class A has just one data member a which is public.

This class is virtually inherited in class B and class C.

Now class B and class C becomes virtual base and no


duplication of data member a is done.

In the main we display the value of a using a pointer.

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

27
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

28
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?

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

2. Avoid ambiguity using virtual base class.

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

31

You might also like