Virtual Base Class in Inheritance in C++
Virtual Base Class in Inheritance in C++
#include <iostream>
using namespace std;
class A {
public:
void show()
{
cout << "Hello form A \
n";
}
};
class B : public A {
};
class C : public A {
};
int main()
{
D object;
object.show();
}
Compile Errors:
prog.cpp: In function 'int main()':
prog.cpp:29:9: error: request for member 'show' is
ambiguous
object.show();
^
prog.cpp:8:8: note: candidates are: void A::show()
void show()
^
prog.cpp:8:8: note: void A::show()
Syntax 2:
class C : public virtual A
{
};
Note:
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 base.
Example 1
#include <iostream>
using namespace std;
class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
int main()
{
D object; // object creation
of class d
cout << "a = " << object.a
<< endl;
return 0;
}
Output:
a = 10
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 class and no duplication of data
member a is done.
Example 2:
#include <iostream>
using namespace std;
class A {
public:
void show()
{
cout << "Hello from A \
n";
}
};
int main()
{
D object;
object.show();
}
Output:
Hello from A
Now, in this article, you will explore the virtual base class in C++ as a
whole. Consider the case where there is just one class A. Two other
grades, B and C, descend from this class A.
The data members/functions of class A are inherited twice to class D, as
seen in the diagram. The first will take you through class B, and the
second will take you through class C. When an object of class D accesses
any data or function member of class A, it's unclear which data or function
member would be named. One was inherited via B, and it inherited the
other via C. This throws the compiler into a loop and causes an error to
appear.
Example:
#include <iostream>
using namespace std;
class A {
public:
int a;
A(){
a = 10;
}
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
int main(){
//creating class D object
D object;
cout << "a = " << object.a << endl;
return 0;
}
Output
#include<iostream>
#include<conio.h>
class student {
int rno;
public:
void getnumber() {
cout << "Enter Roll No:";
cin>>rno;
}
void putnumber() {
cout << "\n\n\tRoll No:" << rno << "\n";
}
};
void getmarks() {
cout << "Enter Marks\n";
cout << "Part1:";
cin>>part1;
cout << "Part2:";
cin>>part2;
}
void putmarks() {
cout << "\tMarks Obtained\n";
cout << "\n\tPart1:" << part1;
cout << "\n\tPart2:" << part2;
}
};
void getscore() {
cout << "Enter Sports Score:";
cin>>score;
}
void putscore() {
cout << "\n\tSports Score is:" << score;
}
};
void display() {
total = part1 + part2 + score;
putnumber();
putmarks();
putscore();
cout << "\n\tTotal Score:" << total;
}
};
int main() {
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0;
}
Aim: - WAP to illustrate how the constructors are implemented and the order
in which they are called when the classes are inherited. Use three classes
named alpha, beta, gamma such that alpha, beta are base class and gamma
is derived class inheriting alpha &beta. Pass four variable to gamma class
object which will further send one integer variable to alpha(),one float type
variable to beta().Show the order of execution by invoking constructor of
derived class.
Program Code:-
#include<iostream>
#include<conio.h>
using namespace std;
class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"alpha initialized\n";
}
void show_x()
{
cout<<"x="<<x<<"\n";
}
};
class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"beta initialized\n";
}
void show_y()
{
cout<<"y="<<y<<"\n";
}
};
class gamma : public beta,public alpha
{
int m,n;
public:
gamma(int a,float b,int c,int d): alpha(a),beta(b)
{
m=c,n=d;
cout<<"gamma initialized\n";
}
void show_mn()
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
int main()
{
gamma g(5,10.75,20,30);
g.show_x();
g.show_y();
g.show_mn();
return 0;
}