What is Virtual Base Class in C++?

What is Virtual Base Class in C++?

Take a circumstance where all the three kinds

of legacy, numerous, staggered and various levelled legacy

are utilized together. This circumstance is shown in the above picture.

The youngster has two base classes parent1

what’s more, parent2 and these two have a

regular base class grandparent. All

people in general and secured individuals from grandparent

are acquired into youngster twice,

first from parent1 and again from parent2. This implies youngster have copy sets of individuals

acquired from a grandparent. It

causes equivocalness in the program.

This sort of issue can

be settled by making the regular base class as a virtual base class. It very well may be

done in the following manner:

Program

                                                                                                                                                                          
class grandparent


{


                            . . . . . .


                            . . . . . .


};




class parent1: virtual
public grandparent


{


                            . . . . . .


                            . . . . . .


};




class parent2: public
virtual grandparent


{


                            . . . . . .


                            . . . . . .


};




class child: public
parent1, public parent2


{


                            . . . . . .


                            . . . . . .


};

In the event that a class is made a virtual

base class, just one duplicate of that class is acquired in determined class. Recall

one thing that the catchphrase virtual and open might be utilized in either request.

Underneath I have composed a program

that actualizes the concept of the virtual base class.

What is Virtual Base Class in C++?

Program

#include<iostream>
using namespace std;
class student


{


    protected:


    int roll_no;




    public:


    void get_no(int x)


    {


        roll_no=x;


    }




    void put_no()


    {


        cout<<“Roll
Number:”<<roll_no;


    }


};




class test: virtual public
student


{


    protected:


    float sub_marks;




    public:


    void get_submarks(float y)


    {


        sub_marks=y;


    }




    void put_submarks()


    {


        cout<<“nSubject
Marks:”<<sub_marks;


    }


};




class sports: public
virtual student


{


    protected:


    float sp_marks;




    public:


    void get_spmarks(float z)


    {


        sp_marks=z;


    }




    void put_spmarks()


    {


        cout<<“nSports
Marks:”<<sp_marks;


    }


};




class result: public test,
public sports


{


    float total_marks;




    public:


    void put_result()


    {


        total_marks=sub_marks+sp_marks;


        put_no();


        put_submarks();


        put_spmarks();


        cout<<“nTotal
Marks:”<<total_marks;


    }


};




int main()


{


    result R;


    R.get_no(20);


    R.get_submarks(75.6);


    R.get_spmarks(81.2);


    R.put_result();




    return 0;


}

In the above model understudy class is a conceptual class, as it isn’t utilized to make any object.

It is utilized distinctly for inferring different classes.

In the event that you loved this article

at that point remember to remark and share.

Leave a Comment

error: Alert: Content is protected!!