In this article virtual base classes we give the information about the duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual base class.

Virtual Base Classes:

Virtual Base Classes

In the above figure the ‘Child’ has two direct base classes ‘Parent1’ and ‘Parent2’ which themselves have a common base class ‘Grandparent’. The ‘child’ inherits the traits of ‘Grandparent’ via two separate paths.  It can also inherit directly as shown by the broken line. The ‘Grandparent’ is sometimes referred to as indirect base class.

Inheritance by the ‘Child’ might pose some problems. All the public and protected members of ‘Grandparent’ are inherited into ‘Child’ twice, first via ‘Parent1’ and ‘Parent2’. This means, ‘Child’ would have duplicate sets of the members inherited from ‘Grandparent’. This introduced ambiguity and should be avoided.

The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual base class while declaring the direct or intermediate base classes as shown below:

class P                                          //  Grandparent

{

            ……………..

            ……………..

};

class Q1 : virtual public P                // Parent 1

{

            ……………..

            ……………..

};

class Q2 : public virtual P               // Parent 2

{

            ……………..

            ……………..

};

class R : public Q1, Public Q2        // Child

{

            ……………..                  // Only one copy of P will be inherited

            ……………..

};

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 paths exits between the virtual base class and a derived class.

// Virtual Base Class

#include<iostream.h>

#include<conio.h>

class stud     // base class (Grandparent)

{

            protected:

                        int rollno;

            public:

                        void getno()

                        {

                                    cout<<“\n Enter the roll number: “;

                                    cin>>rollno;

                        }

};

class test: virtual public stud   // 1st derived class(Parent 1)

{

            protected:

                        int c,cpp;

            public:

                        void getmarks()

                        {

                                    cout<<“\n Enter the marks of Subject C & CPP : “;

                                    cin>>c>>cpp;

                        }

};

class sports: public virtual stud        // 2nd derived class (Parent 2)

{

            protected:

                        int score;

            public:

                        void getscore()

                        {

                                    cout<<“\n Enter the Sports Score: ” ;

                                    cin>>score;

                        }

};

class result: public test, public sports  // 3rd derived class (Child)

{

            int tot;

            public:

                        void display()

                        {

                                    tot=c+cpp+score;

                                    cout<<“\n Roll Number: “<<rollno;

                                    cout<<“\n Subject Marks: \n C:”<<c;

                                    cout<<“\n C++: “<<cpp;

                                    cout<<“\n Sports Score: “<<score;

                                    cout<<“\n Total Marks: “<<tot;

                        }

};

void main()

{

            result p;

            clrscr();

            p.getno();

            p.getmarks();

            p.getscore();

            p.display();

            getch();

}

OUTPUT:

Enter the roll number: 1

Enter the marks of Subject C & CPP : 70

80

Enter the Sports Score: 10

Roll Number:1

Subject Marks:

C:70

C++:80

Sports Score:10

Total Marks:160

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *