Virtual Base class
• Virtual base classes are used in virtual inheritance in a way of preventing
multiple “instances” of a given class appearing in an inheritance
hierarchy when using multiple inheritances.
• The duplication of inherited members due to these multiple can be
avoided by making the common base class as virtual base class while
declaring the direct or intermediate base classes.
• When a class is made a virtual base class, C++ takes necessary care to see
that only one copy of that class is inherited, regardless of how many
inheritance path exist between the virtual base class and a derived class.
• The keywords virtual and public may be used in either order.
Virtual base class
Virtual Base class
• #include <iostream>
• using namespace std;
• class A {
• public:
• A() {
• cout << "Constructor An";
• }
• void display() {
• cout << "Hello form Class A n";
• }
• };
• class B: public virtual A
{
• };
• class C: public virtual A
{
• };
• class D: public B, public
C {
• };
• int main() {
• D object;
• object.display();
• }

19.Virtual Base class.pptx kiujytfghjgfcghjkh

  • 1.
    Virtual Base class •Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritances. • The duplication of inherited members due to these multiple can be avoided by making the common base class as virtual base class while declaring the direct or intermediate base classes. • When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance path exist between the virtual base class and a derived class. • The keywords virtual and public may be used in either order.
  • 2.
  • 3.
  • 4.
    • #include <iostream> •using namespace std; • class A { • public: • A() { • cout << "Constructor An"; • } • void display() { • cout << "Hello form Class A n"; • } • }; • class B: public virtual A { • }; • class C: public virtual A { • }; • class D: public B, public C { • }; • int main() { • D object; • object.display(); • }