CodesJava

Easy learning with example program codes

CPP Inheritance


The property of acquiring all the properties and behaviors of the parent object by an object is termed as inheritance in OOPs. This is a unique feature in object oriented programming languages which facilitates reusability of the code of the parent class by the derived class.

CPP Multilevel Inheritance:

CPP facilitates inheritance of a derived class from its base class as well as inheritance of a derived class from another derived class. The inheritance of a derived class from another derived class is called as multilevel inheritance in CPP, which is possible to any level.

Example 1: Example of single level inheritance

#include <iostream.h>  
using namespace std;  
class Employee
{  
public:  
string name = "Ved";   
};  
class Details: public Employee
{  
public:  
float salary = 50000;    
};  	 
int main()
{  
Details e1;  
cout<<"Name: "<<e1.name<<endl;    
cout<<"Salary: "<<e1.salary<<endl;    
return 0;  
}

Output

Name: Ved       
Salary: 50000

Example 2: Example of multilevel inheritance

#include <iostream.h>  
using namespace std;  
class Employee
{  
public:  
string name = "Ved";   
};  
class Salary: public Employee
{  
public:  
float salary = 50000;    
};	 
class Experience: public Salary
{  
public:  
string experience = "5 years";    
};  
int main()
{  
Experience e1;  
cout<<"Name: "<<e1.name<<endl;    
cout<<"Salary: "<<e1.salary<<endl;  
cout<<"Experience: "<<e1.experience<<endl;  
return 0;  
}

Output

Name: Ved                      
Salary: 50000    
Experience: 5 years
Posted in CPP   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki