Program to read and print employee information with department using Hierarchical inheritance
In this, we are going to see how to Read and Print Employee Information using Hierarchical Inheritance in C++ Programming Language.
 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include<conio.h>
#include <stdio.h>

// Base Class - basicInfo
class basicInfo
{
    protected:
    char name[30];
    int empId;
    char gender[10];

    public:
    void getBasicInfo(void)
    {
        cout << "Enter Name: ";
        cin >> name;
        cout << "Enter Emp. Id: ";
        cin >> empId;
        cout << "Enter Gender: ";
        cin >> gender;
    }
};

// Base Class - deptInfo
class deptInfo : private basicInfo
{
    protected:
    char deptName[30];
    char assignedWork[30];
    int time2complete;

    public:
    void getDeptInfo(void)
    {
        getBasicInfo(); // to get basic info of an employee
        cout << "Enter Department Name: ";
        cin.ignore(1);
        cin.getline(deptName, 30);
        cout << "Enter assigned work: ";
        fflush(stdin);
        cin.getline(assignedWork, 30);
        cout << "Enter time in hours to complete work: ";
        cin >> time2complete;
    }
    void printDeptInfo(void)
    {
        cout << "Employee's Information is: " << endl;
        cout << "Basic Information...:" << endl;
        cout << "Name: " << name << endl;         // accessing protected data
        cout << "Employee ID: " << empId << endl; // accessing protected data
        cout << "Gender: " << gender << endl << endl; // accessing protected data

        cout << "Department Information...:" << endl;
        cout << "Department Name: " << deptName << endl;            // accessing protected data
        cout << "Assigned Work: " << assignedWork << endl;          // accessing protected data
        cout << "Time to complete work: " << time2complete << endl; // accessing protected data
    }
};

// another Base Class : loanInfo
class loanInfo : private basicInfo
{
    protected:
    char loanDetails[30];
    int loanAmount;

    public:
    void getLoanInfo(void)
    {
        getBasicInfo(); // to get basic info of an employee
        cout << "Enter Loan Details (Type of Loan): ";
        cin.ignore(1);
        cin.getline(loanDetails, 30);
        cout << "Enter loan amount: ";
        cin >> loanAmount;
    }
    void printLoanInfo(void)
    {
        cout << "Employee's Information is: " << endl;
        cout << "Basic Information...:" << endl;
        cout << "Name: " << name << endl;         // accessing protected data
        cout << "Employee ID: " << empId << endl; // accessing protected data
        cout << "Gender: " << gender << endl << endl; // accessing protected data

        cout << "Loan Information...:" << endl;
        cout << "Loan Details: " << loanDetails << endl; // accessing protected data
        cout << "Loan Amount : " << loanAmount << endl;  // accessing protected data
    }
};

void main()
{
    clrscr();
    // read and print department information
    deptInfo objD;

    objD.getDeptInfo();
    objD.printDeptInfo();

    cout << endl << endl;
    // read and print loan information
    loanInfo objL;

    objL.getLoanInfo();
    objL.printLoanInfo();

    getch();
}

//.............Coded by RISHAB NAIR

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <stdio.h>
using namespace std;

// Base Class - basicInfo
class basicInfo
{
    protected:
    char name[30];
    int empId;
    char gender[10];

    public:
    void getBasicInfo(void)
    {
        cout << "Enter Name: ";
        cin >> name;
        cout << "Enter Emp. Id: ";
        cin >> empId;
        cout << "Enter Gender: ";
        cin >> gender;
    }
};

// Base Class - deptInfo
class deptInfo : private basicInfo
{
    protected:
    char deptName[30];
    char assignedWork[30];
    int time2complete;

    public:
    void getDeptInfo(void)
    {
        getBasicInfo(); // to get basic info of an employee
        cout << "Enter Department Name: ";
        cin.ignore(1);
        cin.getline(deptName, 30);
        cout << "Enter assigned work: ";
        fflush(stdin);
        cin.getline(assignedWork, 30);
        cout << "Enter time in hours to complete work: ";
        cin >> time2complete;
    }
    void printDeptInfo(void)
    {
        cout << "Employee's Information is: " << endl;
        cout << "Basic Information...:" << endl;
        cout << "Name: " << name << endl;         // accessing protected data
        cout << "Employee ID: " << empId << endl; // accessing protected data
        cout << "Gender: " << gender << endl << endl; // accessing protected data

        cout << "Department Information...:" << endl;
        cout<< << "Department Name: " << deptName << endl;            // accessing protected data
        cout << "Assigned Work: " << assignedWork << endl;          // accessing protected data
        cout << "Time to complete work: " << time2complete << endl; // accessing protected data
    }
};

// another Base Class : loanInfo
class loanInfo : private basicInfo
{
    protected:
    char loanDetails[30];
    int loanAmount;

    public:
    void getLoanInfo(void)
    {
        getBasicInfo(); // to get basic info of an employee
        cout << "Enter Loan Details (Type of Loan): ";
        cin.ignore(1);
        cin.getline(loanDetails, 30);
        cout << "Enter loan amount: ";
        cin >> loanAmount;
    }
    void printLoanInfo(void)
    {
        cout << "Employee's Information is: " << endl;
        cout << "Basic Information...:" << endl;
        cout << "Name: " << name << endl;         // accessing protected data
        cout << "Employee ID: " << empId << endl; // accessing protected data
        cout <<"Gender: " << gender << endl << endl; // accessing protected data

        cout << "Loan Information...:" << endl;
        cout << "Loan Details: " << loanDetails << endl; // accessing protected data
        cout << "Loan Amount : " << loanAmount << endl;  // accessing protected data
    }
};

int main()
{
    // read and print department information
    deptInfo objD;

    objD.getDeptInfo();
    objD.printDeptInfo();

    cout << endl << endl;
    // read and print loan information
    loanInfo objL;

    objL.getLoanInfo();
    objL.printLoanInfo();

    return 0;
}

//.............Coded by RISHAB NAIR

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post