C++ program to illustrate Virtual Base Class


//Base class Employee with name and id as data members
//ReadData() and DisplayData() to read and display its data members
//two derived classes: BasicSalary and ExtraAllowance from class Employee
//class BasicSalary includes basic_scale as data member
//class ExtraAllowance includes bonus, rent and transport as data members
//another derived class Renumeration from both BasicSalary and ExtraAllowance to calculate total salary

#include <iostream>
using namespace std;
class Employee
{
    char name[20];
    int id;
    public:
        void ReadData()
        {
            cout<<"Enter name: ";
            cin>>name;
            cout<<"Enter id: ";
            cin>>id;
        }
        void DisplayData()
        {
            cout<<endl<<"Name: "<<name<<"\tID: "<<id<<endl;
        }
};
class BasicSalary: virtual public Employee
//shares copy of Employee
{
    protected:
        float basic_scale;
    public:
        void ReadBasicScale()
        {
            cout<<"Enter basic scale: ";
            cin>>basic_scale;
        }
        void DisplayBasicScale()
        {
            cout<<endl<<"Basic Scale: "<<basic_scale<<endl;
        }
};
class ExtraAllowance: virtual public Employee //shares copy of Employee
{
    protected:
        float bonus,rent,transport;
    public:
        void ReadExtraAllowance()
        {
            cout<<"Enter bonus allowance: ";
            cin>>bonus;
            cout<<"Enter rent allowance: ";
            cin>>rent;
            cout<<"Enter transport allowance: ";
            cin>>transport;
        }
        void DisplayExtraAllowance()
        {
            cout<<endl<<"Bonus: "<<bonus<<" Rent: "<<rent<<" Transport: "<<transport<<endl;

        }
};
class Renumeration:public BasicSalary,public ExtraAllowance
{
    int total;
    public:
        void ReadRenumeration()
        {
            ReadData();
            ReadBasicScale();
            ReadExtraAllowance();
            total=basic_scale+bonus+rent+transport;
        }
        void DisplayRenumeration()
        {
            DisplayData();
            DisplayBasicScale();
            DisplayExtraAllowance();
            cout<<endl<<"Total: "<<total<<endl;
        }
};
int main()
{
    Renumeration r;
    r.ReadRenumeration();
    r.DisplayRenumeration();
    return 0;
}

No comments:

Post a Comment