Simple Inheritance Private Base Class Example Program in C++
On this page (5sections)
Example Program on Simple with Private Base Class
// Header Files
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class Employee {
int eno;
char name[20], des[20];
// Private members cannot call from outside class.
public:
void getEmpDetails() {
cout << "\nEnter the Employee number:";
cin>>eno;
cout << "Enter the Employee name:";
cin>>name;
cout << "Enter the Employee designation:";
cin>>des;
}
void employee_display() {
cout <<"\nEmployee number:"<<eno;
cout <<"\nEmployee name:"<<name;
cout <<"\nEmployee designation:"<<des;
}
};
class Salary : private Employee {
//Private Base Class, We cannot access outside the dervied Class
float bp, hra, da, pf, np;
public:
void getPayDetails() {
getEmpDetails();
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;
}
void calculate() {
np = bp + hra + da - pf;
}
void display() {
employee_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<<endl;
}
};
int main() {
int i, n;
char ch;
Salary s[10];
cout << "Simple Inheritance Private Base Class Example Program : Payroll System \n";
cout << "Enter the number of employee:";
cin>>n;
for (i = 0; i < n; i++) {
cout << "\nEmployee Details # "<<(i+1)<<" : ";
//getEmpDetails(); inaccessible from outside derived class, Which is private Base Class.
s[i].getPayDetails();
s[i].calculate();
}
for (i = 0; i < n; i++) {
s[i].display();
}
getch();
return 0;
}
Output
Simple Inheritance Private Base Class Example Program : Payroll System
Enter the number of employee:2
Employee Details # 1 :
Enter the Employee number:1
Enter the Employee name:BOSE
Enter the Employee designation:DOC
Enter the Basic pay:20000
Enter the Humen Resource Allowance:2000
Enter the Dearness Allowance :1000
Enter the Profitablity Fund:800
Employee Details # 2 :
Enter the Employee number:2
Enter the Employee name:AMZE
Enter the Employee designation:FIN
Enter the Basic pay:15000
Enter the Humen Resource Allowance:1200
Enter the Dearness Allowance :1200
Enter the Profitablity Fund:900
Employee number:1
Employee name:BOSE
Employee designation:DOC
Employee Basic pay:20000
Employee Humen Resource Allowance:2000
Employee Dearness Allowance:1000
Employee Profitablity Fund:800
Employee Net Pay:22200
Employee number:2
Employee name:AMZE
Employee designation:FIN
Employee Basic pay:15000
Employee Humen Resource Allowance:1200
Employee Dearness Allowance:1200
Employee Profitablity Fund:900
Employee Net Pay:16500
--------------------------------
Process exited after 57.27 seconds with return value 0
Press any key to continue . . .
How It Works
This C++ program demonstrates Simple Inheritance Private Base Class. It first reads the required values as input, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Read the input values that the program will work with.
- Iterate over the data using a loop to apply the logic.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Related Pages
Continue learning with these related tutorials and programs:
- OOP Programs — Browse all OOP Programs.
- Understanding Inheritance in C++ — Tutorial — single, multilevel and hierarchical inheritance.
- Simple Inheritance Base Class Private Member Example Program — More in inheritance in c programs.
- Simple Multi Level Inheritance Example Program — More in inheritance in c programs.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Simple Inheritance Private Base Class, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and reading user input, illustrating a common pattern in C++ programming.