45. Single inheritance

By | September 27, 2021

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.

C++ Inheritance

Where ‘A’ is the base class, and ‘B’ is the derived class.

Single Level Inheritance Example: Inheriting Fields

When one class inherits another class, it is known as single level inheritance. Let’s see the example of single level inheritance which inherits the fields only.

#include <iostream>  
using namespace std;  
 class Account {  
   public:  
   float salary = 60000;   
 };  
   class Programmer: public Account {  
   public:  
   float bonus = 5000;    
   };       
int main(void) {  
     Programmer p1;  
     cout<<“Salary: “<<p1.salary<<endl;    
     cout<<“Bonus: “<<p1.bonus<<endl;    
    return 0;  
}  

Output:

Salary: 60000
Bonus: 5000

In the above example, Employee is the base class and Programmer is the derived class.

Leave a Reply

Your email address will not be published. Required fields are marked *