Multipath Inheritance , virtual base class

In C++ ,If a class inherits two or more intermediate base classes (derived classes) which are derived from the same base class then this type of inheritance is called Multipath Inheritance. It includes other inheritances like single, multiple, multilevel, hierarchical etc. 
C++ में यदि एक क्लास द्वारा दो या अधिक इंटरमीडिएट बेस क्लासेस (डीराइवड क्लासेस) को इन्हेरिट किया जाता है जो एक ही बेस क्लास को इन्हेरिट करती है तब इस प्रकार के इनहेरिटेंस को मल्टीपाथ इनहेरिटेंस कहा जाता है। यह अन्य इनहेरिटेंस रखती है जैसे सिंगल, मल्टीप्ल, मल्टीलेवल एवं हिएरार्चिकल इत्यादि          




In above diagram class D indirectly inherits class A. Here base class A becomes the virtual base class and single copy of data/function of A (virtual) will be copied to class B and class C. It means a single copy of its data members/ member functions is shared by all the other base classes that use virtual base class.
उपरोक्त चित्र में क्लास D द्वारा क्लास A को अप्रत्यक्ष रूप से इन्हेरिट किया गया है। यहाँ बेस क्लास A वर्चुअल बेस क्लास कहलाती है इसकी केवल एक सिंगल कॉपी(वर्चुअल) क्लास B एवं क्लास C में राखी जाती है अर्थात इसकी एक सिंगल कॉपी, अन्य सभी बेस क्लासेस में शेयर की जाती है जो वर्चुअल बेस क्लास का उपयोग करती है।    

C++ program for multipath inheritance:-
#include<iostream.h>
#include<conio.h>
class m{
protected:
int m;
public:
m(){
m=10;
}
};
class n:virtual public m{
protected:
int n;
public:
n(){
n=20;
}
};
class o:virtual public m{
protected:
int o;
public:
o(){
o=30;
}
};
class p:public n,public o{
protected:
int p;
public:
p(){
p=40;
}
void display(){
cout<<"vlaue of m="<<m<<endl<<"vlaue of n="<<n<<endl<<"vlaue of o="<<o<<endl<<"vlaue of p="<<p<<endl;
}};
void main(){
clrscr();
p obj;
obj.display();
getch();
}

No comments:

Post a Comment