You must Sign In to post a response.
  • Program to explain virtual base class.


    Studying object oriented programming and facing some doubts? Wondering what is virtual base class? No worries, prepare for exams confidently by scrolling through the solution provided by our ISC experts here.

    Following is a question that was asked in the question paper of Diploma in Computer Technology(Summer 2019)conducted by Maharashtra State Board of Technical Examination, Mumbai for subject Object Oriented Programming(17432)(G Scheme), Semester-IV

    This question is asked for 4 marks.

    Q) Explain virtual base class with suitable example. [4 Marks]
  • Answers

    3 Answers found.
  • Virtual base class comes under the topic Virtual Inheritance. They are used to prevent multiple instances of a given class in an inheritance hierarchy when multiple inheritance is used.
    Example :

    #include
    using namespace std;

    class Ant {
    public:
    int a;
    Ant() // constructor
    {
    a = 10;
    }
    };

    class Bat : public virtual Ant {
    };

    class Cat: public virtual Ant {
    };

    class Dog : public Bat, public Cat {
    };

    int main()
    {
    Dog object; // object creation of class dog
    cout << "a = " << object.a << endl;

    return 0;
    }

    Output: a = 10

    “It takes a great deal of bravery to stand up to our enemies, but just as much to stand up to our friends."
    – Albus Dumbledore, Harry Potter and the Sorcerer's Stone, Chapter 17

  • While using inheritance, situation may arises when properties of a base class may inherit to indirect child class via two or more than two ways. This situation creates ambiguity because duplicate copies of direct base class gets created in indirect child class.
    To avoid this ambiguity if user made indirect base class (who's properties were available in duplicate form) as a virtual base class, then compiler takes care not to duplicate properties of direct base class, though multiple inheritance path exists.
    Below is the program to demonstrate use of virtual base class. Please include iostream.h and conio.h header files before declaration of class student.

    //PROGRAM TO IMPLEMENT CONCEPT OF VIRTUAL BASE CLASS.
    #include
    #include
    class student
    {
    protected:
    char name[20];
    int rn;
    public:
    void get(void)
    {
    cout<<"Enter student name and roll number\n";
    cin>>name>>rn;
    }
    void display(void)
    {
    cout<<"\nStudent name is "< cout<<"\nStudent roll number is "< }
    };
    class test:virtual public student
    {
    protected:
    int t1,t2;
    public:
    void in(void)
    {
    cout<<"\nEnter marks in two tests\n";
    cin>>t1>>t2;
    }
    void disp(void)
    {
    cout<<"\nMarks in first test "< cout<<"\nMarks in second test "< }
    };
    class sport:virtual public student
    {
    protected:
    int sm;
    public:
    void input(void)
    {
    cout<<"\nEnter marks in sports\n";
    cin>>sm;
    }
    void output(void)
    {
    cout<<"\nMarks in sport "< }
    };
    class result:public test,public sport
    {
    int total;
    float avg;
    public:
    void final(void)
    {
    total=t1+t2+sm;
    cout<<"\nTotal of test and sport marks "< avg=float(total)/3;
    cout<<"\nAvergare marks "< }
    };
    void main(void)
    {
    class result r;
    clrscr();
    r.get();
    r.in();
    r.input();
    clrscr();
    r.display();
    r.disp();
    r.output();
    r.final();
    getch();
    }

  • Virtual base class:- Virtual base class is used to resolve the ambiguity problem in that case when the derived class has multiple instances of the base class. For Ex .:- suppose you have a class P that has the member function display() further this class is inherited by two other classes that are Q and R, and these both classes Q and R further inherited by a single class S. Now the member functions from the class P are twice inherited to the class S, One is from class Q and another one is from class R. So when the member function of class P is accessed by the object of class S it introduces the problem of ambiguity meaning it confused whether to access the inherited member function from class Q or R.
    Below is a small example that will help you to understand the above theory:-
    class P
    {
    public:
    void display()
    {
    printf("Class P");
    }
    };
    class Q : public P
    {
    };
    class R : public P
    {
    };
    class S : public Q, public R
    {
    };
    int main()
    {
    S objectOfS;
    objectOfS.display();
    }
    Note:- this above code will show the ambguity problem.

    To solve this ambiguity problem, we need to use the virtual keyword. We can use the virtual keyword either before or after the public specifier. The below program demonstrate the use of a virtual keyword to resolve the problem of ambiguity.
    class P
    {
    public:
    void display()
    {
    printf("Class P");
    }
    };
    class Q : virtual public P
    {
    };
    class R : virtual public P
    {
    };
    class S : public Q, public R
    {
    };
    int main()
    {
    S objectOfS;
    objectOfS.display();
    return 0;
    }


  • Sign In to post your comments