Friday 17 April 2015

Multipath Inheritance and Virtual Base Classes


The form of inheritance which derives a new class by multiple inheritance of bas clsses, which an derived earlier from the same base class, is known as multipath inheritance. It involves more than one form of inheritance namely multilevel, multiple and hiearchical as shown in figure.




The base classes parent1 and parent2 shows that the multiple inheritance , then the grandparent class with parent1 and parent2 shows that the hierarchical inheritance , then the dotted line with grandparent and child shows that the multilevel inheritance , whereas the grandparent is referred to as the indirect base class.

Multipath inheritance can pose some problems in compilation. The public and protected members of grandparent are inherited into the child class twice, first, via parent1 class and the via parent2 class. Therefore, the child class would have duplicate sets of members of the grandparent which leads to ambiguity during compilation and it should be avoided.

C++ supports another important concept called virtual base classes to handle ambiguity caused due to the multipath inheritance. It is achieved by making the common base class as a virtual base class while declaring the direct or intermediate classes as shown below.


Class A
{
public:
void func(void)
{
// body of function
}
};
Class B1 : public virtual A
{
body of class B1
};
Class B2 : public virtual A
{
//body of class B2
};
Class D: public B1, public B2
{
//body of class D
};
consider the statement


obj.func();

where obj is the object of class D and invokes the func() defined in the class A. if the keyword virtual does not exist in the declaration of classes B1 and B2, a call to func() leads to the following compilation error:

Error : Member is ambiguous : ‘A::func’ and ‘B::func’

C++ takes necessary care to see that only one copy of the base class is inherited, when a class is inherited as virtual irrespective of the number of paths that exist between the virtual base class and the derived class. The keyword virtual and public or protected may be used in any order.

Consider the following diagram and the program for that.


Sample Program

#include <iostream.h>
class person
{
protected:
int rno;
char branch[30];
public:
void readstudentdata(void)
{
cout<<”Roll Number ?”;cin>>rno;
cout<<”Branch ?”;cin>>branch;
}
void displaystudentdata(void)
{
cout<<”Roll Number”<<rno;
cout<<”Branch”<<branch;
}
};
class internalexam:virtual public student
{
protected:
int sm1, sm2;
public:
void readdata(void)
{
cout<<”Enter two subject marks”;
cin>>sm1>>sm2;
}
void displaydata(void)
{
cout<<”Marks in two subjects”;
cout<<sm1<<sm2;
cout<<”Internal total “<<internaltotal()<<endl;l
}
int internaltotal(void)
{
return sm1 + sm2;
}
};
class externalexam : virtual public student
{
protected:
int sm1, sm2;
public:
void readdata(void)
{
cout<<”Enter two subject marks”;
cin>>sm1>>sm2;
}
void displaydata(void)
{
cout<<”Marks in two subjects”;
cout<<sm1<<sm2;
cout<<”External total “<<externaltotal()<<endl;l
}
int externaltotal(void)
{
return sm1 + sm2;
}
};
class result : public internalexam, public externalexam
{
int total;
public:
int totalmarks(void)
{
return internaltotal() + externaltotal();
}
};
void main()
{
result stud1;
cout<<”Enter data for student”<<endl;
stud1.readdata();
cout<<”Enter the Internal Marks….”<<endl;
stud1.interna lexam::readdata();
cout<<”Enter the External Marks….”<<endl;
stud1.externalexam::readdata();
cout<<”Student Details”<<endl;
stud1.displaystudentdata();
stud1.internalexam::displaydata();
stud1.externalexam::displaydata();
}







No comments:

Post a Comment