Confused with Virtual inheritance
Hello guys,
I am IT student and currently studying OOPs concept of java. I am little bit aware about the inheritance concept, and specially about the multiple inheritance. But I am not getting what is 'Virtual inheritance' and it's use. I have go through many books for this but nothing was helpful. I wonder if you provide information something about the 'Virtual inheritance'. It will be be great if you explain it with suitable example.
Re: Confused with Virtual inheritance
Hi,
Virtual Inheritance is a mechanism to compensate the disadvantages repeated inheritance. The legacy is in virtual mode if the keyword virtual is present in the modifier inheritance. The following figure shows a case of repeated inheritance:
- The attributes of class A are present in double copy in class D, once because of the legacy from B, again because of the legacy from C
- Manufacturers B and C are supposed to call all both the manufacturer of A which does not fail to pose a major problems.
Re: Confused with Virtual inheritance
Following is the example of implementation of Virtual inheritance:
Code:
class Has
{
Public:
A ()
{
court << "Manufacturer A" <<endl;
}
private:
int a;
};
class B: virtual Public A?
{
Public:
B (): A ()
{
court << "B constructor" <<endl;
};
private:
int b;
);
class C: virtual Public A?
{
Public:
C (): A ()
{
court << "constructor C" <<endl;
};
private:
int c;
};
class D: virtual Public Has, Public B Public C?
{
Public:
D () A ()
B ()
C ()
{
court << "constructor D" <<endl;
};
private:
int d;
};
Re: Confused with Virtual inheritance
Suppose you provide a library including all classes derive from a common ancestor. It is necessary use of virtual inheritance so that your client can of safe multiple inheritance. First, you must decide if your classes have a chance to be derived by your client and if so, can he desire to associate with another class of your library? if the answer is yes, then you must provision of virtual inheritance in all other cases, use the legacy of "normal".
Re: Confused with Virtual inheritance
HI friend,
The virtual inheritance adds considerably table management of virtual methods and attributes. The order of modifiers and virtual audience in a Virtual heritage is irrelevant. For example you have class 'A', and if constructor of A is explicitly named in the D ensuring the proper initialization of inherited attributes of A. Consequently, calls the constructor of A from those of B and C will not be executed.
Re: Confused with Virtual inheritance
Hi,
Virtual inheritance can be used within multiple inheritance. Suppose if a class 'V' has a direct parent class 'T', then it can inherit it indirectly using several inheritance lines. In short the Virtual Inheritance is nothing but the process for to compensate the demerits of the repeated inheritance. The idea of the Virtual inheritance is always hold he virtual content , but the similar order as they have stored.