Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Virtual Base Class

C++ Object Oriented Programming

In order to prevent the compiler from giving an error due to ambiguity in multiple path or multiple inheritance, we use the keyword virtual.

Virtual Base Class

 

• In order to prevent the compiler from giving an error due to ambiguity in multiple path or multiple inheritance, we use the keyword virtual. That means when we inherit from the base class in both derived classes, the base class is made virtual. The code that illustrates the concept of virtual base class is as given below ‒

C++ Program

#include<iostream>

using namespace std;

class base {

    public:

      int i;

};

class derived1:virtual public base

{

    public:

      int j;

};

class derived2:virtual public base

{

     public:

       int k;

};

//derived3 is inherited from derived1 and derived2

//but only one copy of base class is inherited.

class derived3:public derived 1,public derived2

{

public:

int sum()

{

      return i+j+k;

}

};

void main()

{

derived3 obj;

Codec 100

obj.i=10;

obj.j=20;

obj.k=30;

cout<" The sum is = "<obj.sum();

}

Output

The sum is = 60

Program Explanation

In above program if we do not write the keyword virtual while deriving the classes derived1 and derived2 then compiler would have generated error messages stating the ambiguity in accessing the member of base class.

The sum is a function which can access the variables i, j and k of parent classes. In main function we have created an object obj of derived3 class and using this object i, j and k can be accessed.

 

Example:1

Develop an object oriented program in C++ to create a payroll system for an organization where one base class consist of employee name, code, designation and another base class consist of a account no and date of joining. The derived class consists of the data members such as basic pay, DA, HRA, CCA and deductions PF, LIC, IT. (Program must use the concept of virtual base class)

Solution :

/**************************************************************************

Program to Payroll System for organization

************************************************************************/

#include<iostream>

using namespace std;

class Employee

{

    public:

      char name[10];

      int code;

      char designation[15];

};

class Accounts:virtual public Employee

{

      int accno;

      char doj[10];

};

class Pay:virtual public Employee

{

    public:

      double BasicPay;

      double DA;

      double HRA;

      double CCA;

      double PF,LIC,IT;

};

class Derived:public Accounts,public Pay

{

public:

    void get_details()

    {

      cout<<"\n Enter the name of Employee: ";

      cin>>name;

      cout<<"\n Enter the Employee Code: ";

      cin>>code;

      cout<<"\n Enter the designation of Employee: ";

      cin>>designation;

      cout<<"\n Enter the Basic Payment: ";

      cin>>BasicPay;

      cout<<"\n Enter amount of DA : ";

      cin>>DA;

      cout<<"\n Enter the amount of HRA: ";

      cin>>HRA;

      cout<<"\n Enter the amount of CCA: ";

      cin>>CCA;

      cout<<"\n Enter the amount of PF:

      cin>>PF;

      cout<<"\n Enter the amount of LIC: ";

      cin>>LIC;

      cout<<"\n Enter the amount of IT:

      cin>>IT;

   }

   Apa double NetPayment()

  {

      double amount, deductions;

      deductions=PF+LIC+IT;

      amount=(BasicPay+DA+HRA+CCA)‒deductions;

      return amount;

   }

};

void main()

{

      Derived obj;

      obj.get_details();

      cout<<obj.NetPayment();

}

Output

Enter the name of Employee: ABC

Enter the Employee Code: 100

Enter the designation of Employee: Manager

Enter the Basic Payment: 12000

Enter amount of DA: 3000

Enter the amount of HRA: 2000

Enter the amount of CCA: 700

Enter the amount of PF: 1200

Enter the amount of LIC: 1000

Enter the amount of IT: 3000

12500

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Virtual Base Class


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation