You are on page 1of 21

BCA 3.

3 Non Linear Data structure using C++

Program BCA
Subject Non Linear Data structure using C++
Semester III
University Kuvempu university
Session 36
BCA 3.3 Non Linear Data structure using C++

Unit 03
Title Operators Overloading and Inheritance

Sub – title Hybrid inheritance, virtual base classes


Recap of previous class

 Understanding the hierarchical inheritance


Learning objectives

To understand the Hybrid inheritance, virtual base classes

 
Session Outcomes

 The Student will be able to understand Hybrid


inheritance, virtual base classes

 
Hybrid Inheritance:
 

If two or more different types of inheritances are


combined then it is called as hybrid inheritance
syntax:
class derived : visibility base1, visibility base2 ….
{
// statements
};
Example :
Class student {……};
Class test : public student {……};
Class result : public test {…….};
Class result : public sports {…….};

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void geta()
{ a=2; }
};
class B : public A
{
public:
void puta()
{ cout<<a; }
};
class C
{
public:
int b;
void getb()
{ b=4;}
};
class D : public B, public C
{
public:
void show()
{
cout<<b;
cout<<"Inherits A,B,C";
}
};
 void main()

{
clrscr();

D d;
d.geta();

d.getb();
d.puta();
d.show();

getch();
}
Virtual Base Classes
Classes which are inherited virtually using the keyword
virtual are called as virtual base class.

Consider a situation where all the three kinds of


inheritance, namely, multilevel, multiple and hierarchical
inheritance, are involved. The 'child' has two direct base
classes 'parent1' and 'parent2' which themselves have a
common base class 'grandparent'. The 'child' inherits the
properties 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.
 
Example for virtual base class
Inheritance by the 'child' might pose some problems. All
the public and protected members of 'grandparent' are
inherited into 'child' twice, first via 'parent1' 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 as virtual base class while declaring the direct
or intermediate base classes as shown below:
class A
{ ….. };
class B1 : virtual public A
{ ….. };
class B2 : virtual public A
{ ….. };
class C : public B1, public B2
{ ….. };

When a class is made a virtual base class, C++ takes


necessary care to see that only one copy of that class is
inherited, regardless of how many inheritance paths exist
between the virtual base class and a derived class.
MCQs
1.Which among the following best defines the
hybrid inheritance?
a) Combination of two or more inheritance types
b) Combination of same type of inheritance
c) Inheritance of more than 7 classes
d) Inheritance involving all the types of
inheritance
Ans: A)
2. How many types of inheritance should be used
for hybrid?
a) Only 1
b) At least 2
c) At most two
d) Always more than 2
Ans:B)
3. Which amongst the following is true for hybrid
inheritance?
a) Constructor calls are in reverse
b) Constructor calls are priority based
c) Constructor of only derived class is called
d) Constructor calls are usual
Ans: d)
4. A virtual base class ?
a) is qualified as virtual in base class definition.
b) do not qualified as virtual in base class
definition.
c) allows to inherit more than one copy of the
base class members.
d) strict the path of inheritance.
Ans: a)
5. ……………….. inheritance may lead to
duplication of inherited members from a
‘grandparent’ base class.
A) multipath
B) multiple
C) multilevel
D) hierarchical
Ans:A)
Session Summary

 The Student can be able to understand Hybrid


inheritance, virtual base classes
Reference

Author: E Balaguruswamy
Title of the book: Object Oriented Programming with C++  

You might also like