Simple Hierarchical Inheritance Example Program
Definition
Inheritance is when an object or class is based on another object or class, using the same implementation specifying implementation to maintain the same behavior. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. In hierarchical inheritance a single class serves as a superclass (base class) for more than one sub class.
Syntax
Class A{
public void methodA(){
//Do Something
}
}
Class B : public A{
public void methodB(){
//Do Something
}
}
Class C : public A{
public void methodC(){
//Do Something
}
}
Hierarchical Inheritance Example Program
// Header Files
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class Person {
int eno;
char name[20], des[20];
// Private members cannot call from outside class.
public:
void getPersonDetails() {
cout << "\nEnter the Person number:";
cin>>eno;
cout << "Enter the Person name:";
cin>>name;
cout << "Enter the Person designation:";
cin>>des;
}
void person_display() {
cout <<"\nPerson number:"<<eno;
cout <<"\nPerson name:"<<name;
cout <<"\nPerson designation:"<<des;
}
};
class Employee : private Person {
//Private Base Class, We cannot access outside the dervied Class
float bp, hra, da, pf, np;
public:
void getEmployeeDetails() {
getPersonDetails();
cout << "Enter the Basic pay:";
cin>>bp;
cout << "Enter the Humen Resource Allowance:";
cin>>hra;
cout << "Enter the Dearness Allowance :";
cin>>da;
cout << "Enter the Profitablity Fund:";
cin>>pf;
calculate();
}
void calculate() {
np = bp + hra + da - pf;
}
void employee_display() {
person_display();
cout <<"\nEmployee Basic pay:"<<bp;
cout <<"\nEmployee Humen Resource Allowance:"<<hra;
cout <<"\nEmployee Dearness Allowance:"<<da;
cout <<"\nEmployee Profitablity Fund:"<<pf;
cout <<"\nEmployee Net Pay:"<<np;
}
};
class Student : private Person {
char college[20], course[20];
public:
void getStudentDetails() {
getPersonDetails();
cout << "Enter the Student college Name:";
cin>>college;
cout << "Enter the Student course Name:";
cin>>course;
}
void student_display() {
person_display();
cout <<"\nStudent college Name:"<<college;
cout <<"\nStudent IFSC:"<<course<<endl;
}
};
int main() {
int i, n;
char ch;
Student s[10];
Employee e[10];
cout << "Simple Hierarchical Inheritance Example Program : Payroll System \n";
cout << "Enter the number of Student:";
cin>>n;
for (i = 0; i < n; i++) {
cout << "\nStudent Details # "<<(i+1)<<" : ";
s[i].getStudentDetails();
}
for (i = 0; i < n; i++) {
s[i].student_display();
}
cout << "\n\nEnter the number of Employee:";
cin>>n;
for (i = 0; i < n; i++) {
cout << "\nEmployee Details # "<<(i+1)<<" : ";
e[i].getEmployeeDetails();
}
for (i = 0; i < n; i++) {
e[i].employee_display();
}
getch();
return 0;
}
Sample Output
Simple Hierarchical Inheritance Example Program : Payroll System
Enter the number of Student:2
Student Details # 1 :
Enter the Person number:101
Enter the Person name:Booke
Enter the Person designation:10th
Enter the Student college Name:CGR
Enter the Student course Name:BSC
Student Details # 2 :
Enter the Person number:102
Enter the Person name:Moste
Enter the Person designation:12th
Enter the Student college Name:IIT
Enter the Student course Name:BE
Person number:101
Person name:Booke
Person designation:10th
Student college Name:CGR
Student IFSC:BSC
Person number:102
Person name:Moste
Person designation:12th
Student college Name:IIT
Student IFSC:BE
Enter the number of Employee:2
Employee Details # 1 :
Enter the Person number:201
Enter the Person name:ASHK
Enter the Person designation:Er
Enter the Basic pay:12000
Enter the Humen Resource Allowance:1200
Enter the Dearness Allowance :1100
Enter the Profitablity Fund:900
Employee Details # 2 :
Enter the Person number:203
Enter the Person name:ROK
Enter the Person designation:Dr
Enter the Basic pay:13000
Enter the Humen Resource Allowance:1300
Enter the Dearness Allowance :800
Enter the Profitablity Fund:700
Person number:201
Person name:ASHK
Person designation:Er
Employee Basic pay:12000
Employee Humen Resource Allowance:1200
Employee Dearness Allowance:1100
Employee Profitablity Fund:900
Employee Net Pay:13400
Person number:203
Person name:ROK
Person designation:Dr
Employee Basic pay:13000
Employee Humen Resource Allowance:1300
Employee Dearness Allowance:800
Employee Profitablity Fund:700
Employee Net Pay:14400
--------------------------------
Process exited after 108.4 seconds with return value 0
Press any key to continue . . .
Inheritance In C++ Programs
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Inheritance Base Class Private Member Example Program
- Simple Inheritance Private Base Class Example Program
- Simple Multi Level Inheritance Example Program
- Simple Hierarchical Inheritance Example Program
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming