Simple Inheritance Private Base Class Example Program


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 . . .