Simple Inheritance Base Class Private Member Example Program in C++
On this page (5sections)
Simple Example Program With Inheritance Base Class Private Member
// 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 : public Employee {
//We cannot use Employee Base Class Private Members
float bp, hra, da, pf, np;
public:
void getPayDetails() {
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 Base Class Private Member : Payroll System \n";
cout << "Enter the number of employee:";
cin>>n;
for (i = 0; i < n; i++) {
cout << "\nEmployee Details # "<<(i+1)<<" : ";
s[i].getEmpDetails();
s[i].getPayDetails();
s[i].calculate();
}
for (i = 0; i < n; i++) {
s[i].display();
}
getch();
return 0;
}
Sample Output
Simple Inheritance Base Class Private Member : Payroll System
Enter the number of employee:2
Employee Details # 1 :
Enter the Employee number:1
Enter the Employee name:BALU
Enter the Employee designation:SE
Enter the Basic pay:10000
Enter the Humen Resource Allowance:1000
Enter the Dearness Allowance :500
Enter the Profitablity Fund:600
Employee Details # 2 :
Enter the Employee number:2
Enter the Employee name:SUMESH
Enter the Employee designation:SX
Enter the Basic pay:12000
Enter the Humen Resource Allowance:1100
Enter the Dearness Allowance :600
Enter the Profitablity Fund:600
Employee number:1
Employee name:BALU
Employee designation:SE
Employee Basic pay:10000
Employee Humen Resource Allowance:1000
Employee Dearness Allowance:500
Employee Profitablity Fund:600
Employee Net Pay:10900
Employee number:2
Employee name:SUMESH
Employee designation:SX
Employee Basic pay:12000
Employee Humen Resource Allowance:1100
Employee Dearness Allowance:600
Employee Profitablity Fund:600
Employee Net Pay:13100
--------------------------------
Process exited after 105.4 seconds with return value 0
Press any key to continue . . .
How It Works
This C++ program demonstrates Simple Inheritance Base Class Private Member. 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 Private Base Class 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 Base Class Private Member, 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.