Explain virtual base class in c++ with program.

Virtual Base Classes

We have just discussed a situation which would require the use of both multiple and multi level inheritance. Consider a situation, where all the three kinds of inheritance, namely multi-level, multiple and hierarchical are involved.

Let us say the 'child' has two direct base classes 'parent1' and 'parent2' which themselves has a common base class 'grandparent'. The child inherits the traits of 'grandparent' via two separate paths. It can also be inherit directly as shown  by the broken line. The grandparent is sometimes referred to as 'INDIRECT BASE CLASS' . Now, the inheritance by the child might cause some problems. All the public and  protected members of 'grandparent' are inherited into 'child' twice. First via 'parent1' and again via 'parent2'. So, there occurs a duplicate which should be avoided. 


The duplication of the inherited members can be avoided by making common base class as the virtual base class: 

for e.g.

class g_parent
{
//Body
};
class parent1: virtual public g_parent
{
// Body
};
class parent2: public virtual g_parent
{
// Body};
class child : public parent1, public parent2
{
// body
};

When a class is virtual base class, C++ takes necessary care to see that only one copy of that class   is  inherited,   regardless   of   how   many   inheritance   paths   exists   between virtual base class and derived class.   Note   that   keywords   ‘virtual’   and   ‘public’   can  be used in either order.

//Program to show the virtual base class

#include<iostream.h> 
#include<conio . h>
class student // Base class declaration
{
protected:
int r_no; public:
void get_n (int a)
{ r_no = a; } void put_n (void)
{ cout << “Roll No. “ << r_no<< “ln”;}
};
 
class test : virtual public student // Virtually declared common
{  //base class1 
protected:
int part1; int part2; public:
void get_m (int x, int y)
{ part1= x; part2=y;}
void putm (void)
{
cout << “marks obtained: “ << “\n”;
cout << “part1 = “ << part1<< “\n”; 
cout << “part2 = “<< part2 << “\n”;
}
};
class sports : public  virtual student // virtually declared common
{        //base class 2
protected:
int score;
public:
void get_s (int a) { score = a ;
}
void put_s (void)
{ cout << “sports wt.: “ <<score<< “\n”;}
};
class result: public test, public sports //derived class
{
private : int total ; public:
void show (void) ;
};
void result : : show (void)
{
 total = part1 + part2 + score ; put_n ( );
put_m ( );
put_s ( ) ; cout << “\n total score= “ <<total<< “\n” ;
}
main ( )
{
clrscr ( ) ; result S1 ; S1.get_n (345)
S1.get_m (30, 35) ;
S1.get-S (7) ;
S1. show ( ) ;
}

//Program to show hybrid inheritance using virtual base classes 

#include<iostream.h>
#include<conio.h> Class A
{
protected:
int x;
public:
};
void get (int) ; 
void show (void) ;
void A : : get (int a)
{
x = a ;
 }
void A : : show (void)
{
 cout << X ;

Class A1 : Virtual Public A
{
protected:
int y ;
public:
};
void get (int) ; void show (void);
void A1 :: get (int a)
{
y = a;
}
void A1 :: show (void)
{
cout <<y ;
}
class A2 : Virtual public A
{
protected:
int z ;
public:
};
 void get (int a)
{
z =a;
}
void show (void)

cout << z;
}
class A12 : public A1, public A2
{
int r, t ; 
public:
void get (int a)

r = a;
}
void show (void)
{
t = x + y + z + r ;
cout << “result =” << t ;
}
};
int main ()
{
clrscr ( ) ;
A12 r ;
r.A : : get (3) ; r.A1 : : get (4) ; r.A2 : : get (5) ; r.get (6) ;
r . show ( ) ;
}


Md Ashraf

'KNOWLEDGE WITH ASHRAF' is the platform where you find all the type of knowledge especially on programming based. Our goal is to give you a deeper grasp of technology in specifics that will help you increase your knowledge.

Previous Post Next Post

Contact Form