C++ 37: Virtual Base Class
Ambiguity in Hybrid Inheritance
#include <iostream>using namespace std;class A{protected:char a[20];};class B : public A{protected:char b[20];};class C : public A{protected:char c[20];};class D : public B, public C{protected:char d[20];};int main(){A obj1;B obj2;C obj3;D obj4;cout << "Size of A obj1: " << sizeof(obj1) << endl;cout << "Size of B obj2: " << sizeof(obj2) << endl;cout << "Size of C obj3: " << sizeof(obj3) << endl;cout << "Size of D obj4: " << sizeof(obj4) << endl;return 0;}
Output:
Size of A obj1: 20
Size of B obj2: 40
Size of C obj3: 40
Size of D obj4: 100 Virtual Base Class
- If the hybrid inheritance involves multiple inheritance in it, then there is poassiblity that the base class is inherited in derived class multiple times, and if this happened then it will create multiple copies of base class data members in derived class.
- This multiple copies of the base class data members introduce ambiguity(i.e. confusing results)
- These multiple copies of the base class data-members can be avoided by inheriting the base class virtually in the derived class.
- If the base class is inherited virtually in the derived class then C++ takes special care to see only one copy of the base class data-member to be inherited in derived class.
Example:
As Virtual Base class creates only one copy of repeted members when it asks for the same values again then ther asre overrited.
#include <iostream>using namespace std;class Person{protected:char name[20];int age;public:void readData(void){cout << "Enter Name: ";cin.sync();cin.getline(name, 20);cout << "Age: ";cin >> age;}void showData(void){cout << "Name : " << name << endl;cout << "Age: " << age << endl;}};class TaxPayer : public virtual Person{protected:int PAN_NO;public:void readData(void){Person::readData();cout << "Enter PAN_NO: ";cin >> PAN_NO;}void showData(void){Person::showData();cout << "Pan_NO: " << PAN_NO << endl;}};class Citizen : public virtual Person{protected:char Nat[20];public:void readData(void){Person::readData();cout << "Enter Your Nationality : ";cin.sync();cin.getline(Nat, 19);}void showData(void){Person::showData();cout << "Nationality" << Nat << endl;}};class Employee : public TaxPayer, public Citizen{protected:int EmpNo;char job[10];public:void readData(void){TaxPayer::readData();Citizen::readData();cout << "Enter EmpNo: ";cin.sync();cin >> EmpNo;cout << "Enter job: ";cin >> job;}void showData(void){TaxPayer::showData();Citizen::showData();cout << "Employee: " << EmpNo << endl;cout << "Job: " << job << endl;}};int main(){// Citizen a;// a.readData();// a.showData();cout << "Employee Data: \n\n"<< endl;Employee b;b.readData();cout << "\n\n Displaying entered Data: " << endl;b.showData();return 0;}
Output:
Employee Data:
Enter Name: Jack
Age: 23
Enter PAN_NO: 1234
Enter Name: James
Age: 23
Enter Your Nationality : Ind
Enter EmpNo: 258
Enter job: SDM
Displaying entered Data:
Name : James
Age: 23
Pan_NO: 1234
Name : James
Age: 23
NationalityInd
Employee: 258
Job: SDM
Comments
Post a Comment