Sunday, 8 April 2018

Virtual Base Class

Consider a situation where multilevel multiple and hierarchical all the three kinds are inherited.



In the above figure the base class is inherited by both Derived1 and Derived2. Derived 3 directly inherits both Derived and Derived2.

All the public and protected member of base are inherited into Derived3 twice through both Derived 1 and Derived2.

Therefore Derived3 would have duplicate sets of members inherited. This causes ambiguity when member of base is used by Derived.

To Resolve this ambiguity, cpp includes a mechanism by which only one copy of base will be included in derived3. And this feature is called as virtual base class.

When a class is made virtual, cpp takes necessary care to inherit only only one copy of the class.

The keyword virtual precedes the base class access specifiers when it is inherited by derived class.

Ex:
Class Base
{

};
Class Derived1 : virtual public Base
{

};
Class Derived2 : virtual  public Base
{

};
Class Derived3 : public Derived1, public Derived2
{

};

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getrollno(int n)
{
rollno=n;
}
void display1( )
{
cout<<"\n\n\tRoll no is--->"<<rollno;
}
};
class test:virtual public student
{
protected:
int cpp;
int rdbms;
public:
void getmarks(int c, int r)
{
cpp=c;
rdbms=r;
}
void display2( )
{
cout<<"\n\n\tMarks of cpp is--->"<<cpp;
cout<<"\n\n\tMarks of rdbms is--->"<<rdbms;
}
};
class spots:virtual public student
{
protected:
int sportwt;
public:
void getsportmark(int s)
{
sportwt=s;
}
void display3( )
{
cout<<"\n\n\tSport Marks is--->"<<sportwt;
}
};
class rest:public test,public sport
{
protected:
int total;
public:
void calresult( )
{
total=cpp+rdbms+sportwt;
}
void display( )
{
display1( );
display2( );
display3( );
cout<<"\n\n\tTotal Result is--->"<<total;
}
};
void main( )
{
result R;
clrscr( );
R.getrollno(101);
R.getmarks(78,87)
R.getsportmark(10);
R.calresult( );
R.display( );
getch( );
}

Output:-
Roll no is--->101
Marks of cpp is--->78
Marks of rdbms is--->87
Sport Marks is--->10
Total Result is--->175

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses