Looking for NEET / JEE Coaching? Enquire Now
You must Sign In to post a response.
  • Category: Programming

    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

    2 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

    --Time is money--
    Benjamin Franklin

  • 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();
    }


  • Sign In to post your comments