VIRTUAL BASE CLASSES in C++
We have already learned that on “Single and Multiple Inheritance” that inheritance is a process of creating a new class which is derived from more than one base classes. Multiple inheritance hierarchies can be complex, which may lead to a situation in which a derived class inherits multiple times from the same indirect base class.
Example 1.
A program to define multiple derived classes which are accessing the same base class data members through indirect base references.
//using nonvirtual base classes
#include <iostream>
using namespace std;
class baseA {
protected:
int x;
public:
void getdata();
void display();
};
class derivedB : public baseA { //path 1, through derivedB
protected:
fl oat y;
public:
void getdata();
void display();
};
class derivedD : public baseA { //path 2, through derivedD
protected:
char name[20];
public:
void getdata();
void display();
};
class abc : public derivedB,public derivedD
{
public:
void getdata();
void display();
};
void baseA :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
}
void baseA :: display()
{
cout << “ integer : ” << x << endl;
}
void derivedB :: getdata()
{
baseA::getdata();
cout << “ enter a fl oating point value \n”;
cin >> y;
}
void derivedB :: display()
{
baseA :: display();
cout << “ real number :” << y << endl;
}
void derivedD :: getdata()
{
baseA :: getdata();
cout << “ enter a string \n”;
cin >> name;
}
void derivedD :: display()
{
baseA:: display();
cout << “ string : ” << name << endl;
}
void abc:: getdata()
{
derivedB :: getdata();
derivedD :: getdata();
}
void abc:: display()
{
derivedB :: display();
derivedD :: display();
}
int main()
{
abc obj;
obj.getdata();
obj.display();
return 0;
}
Output of the above program
enter an integer
10
enter a floating point value
-2.2
enter an integer
20
enter a string
C++, world
integer : 10
real number :-2.2
integer : 20
string : C++,world
Note:- The protected data member x is accessed twice by the two derived classes through derivedB and derivedD. The content of the data variable is copied into two memory variables in the heap. It is certainly a repetition with different data and confusing if it is intended to process the same item of the base class.
Example 2.
A program to define multiple derived classes which is accessing the same base class data members through indirect base references using virtual base class.
//using virtual base classes
#include <iostream>
using namespace std;
class baseA {
protected:
int x;
public:
void getdata();
void display();
};
class derivedB : public virtual baseA { //path 1, through derivedB
protected:
fl oat y;
public:
void getdata();
void display();
};
class derivedD : public virtual baseA { //path 2, through derivedD
protected:
char name[20];
public:
void getdata();
void display();
};
class abc : public derivedB,public derivedD
{
public:
void getdata();
void display();
};
void baseA :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
}
void baseA :: display()
{
cout << “ integer : ” << x << endl;
}
void derivedB :: getdata()
{
baseA::getdata();
cout << “ enter a fl oating point value \n”;
cin >> y;
}
void derivedB :: display()
{
baseA :: display();
cout << “ real number :” << y << endl;
}
void derivedD :: getdata()
{
baseA :: getdata();
cout << “ enter a string \n”;
cin >> name;
}
void derivedD :: display()
{
baseA:: display();
cout << “ string : ” << name << endl;
}
void abc:: getdata()
{
derivedB :: getdata();
derivedD :: getdata();
}
void abc:: display()
{
derivedB :: display();
derivedD :: display();
}
int main()
{
abc obj;
obj.getdata();
obj.display();
return 0;
}
Output of the above program
enter an integer
20
enter a floating point value
3.3
enter an integer
55
enter a string
Hello, C++
integer: 55
real number: 3.3
integer: 55
string: Hello,C++
Note:- Even though the protected data member x is accessed by two derived classes through derivedB and derivedD, the content of the variable x is the same.

0 comments
Post a Comment