Virtual Base Classes
Virtual Base Classes
class B : public A {}
class C : public A {}
The above class hierarchy results in the "dreaded diamond" which looks like this:
/\
B C
\/
An instance of D will be made up of B, which includes A, and C which also includes A. So you
have two "instances" (for want of a better expression) of A.
When you have this scenario, you have the possibility of ambiguity. What happens when you do
this:
D d;
Virtual inheritance is there to solve this problem. When you specify virtual when inheriting your
classes, you're telling the compiler that you only want a single instance.
This means that there is only one "instance" of A included in the hierarchy. Hence
D d;
d.Foo(); // no longer ambiguous
Hope that helps as a mini summary. For more information, have a read of this and this.
Cheers!
Polymorphic means many forms. The object of the polymorphic class pointer can store any
derived class objects and called respective member functions when its object member
functions is called automatically instead of calling base class member function.
By using "Virtual" key word in front of Base class member function, the base class become
polymorphic class. when this member function assigns = 0 then it becomes pure virtual
function and Base class become Abstract class.