C++ program to read and print employee information using multiple inheritance.

Learn, how to implement multiple inheritance in C++? Here, we are implementing a C++ program that will read and the employee information using multiple inheritance.
[Last updated : March 02, 2023]

Reading/printing the employee information using multiple inheritance

This program will demonstrate example of multiple inheritance using employee details example in c++ programming language.

Multiple Inheritance Program in C++

// C++ program to read and print employee information
// using multiple inheritance.

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

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

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

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

public:
    void getDeptInfo(void)
    {
        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;
    }
};

// final class (Derived Class)- employee
class employee : private basicInfo, private deptInfo {
public:
    void getEmployeeInfo(void)
    {
        cout << "Enter employee's basic info: " << endl;
        //call getBasicInfo() of class basicInfo
        getBasicInfo(); //calling of public member function
        cout << "Enter employee's department info: " << endl;
        //call getDeptInfo() of class deptInfo
        getDeptInfo(); //calling of public member function
    }
    void printEmployeeInfo(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
    }
};

int main()
{
    //create object of class employee
    employee emp;

    emp.getEmployeeInfo();
    emp.printEmployeeInfo();

    return 0;
}

Output

    Enter employee's basic info: 
    Enter Name: Mickey
    Enter Emp. Id: 1121
    Enter Gender: F
    Enter employee's department info: 
    Enter Department Name: Testing
    Enter assigned work: Test Game OEM
    Enter time in hours to complete work: 70
    Employee's Information is: 
    Basic Information...:
    Name: Mickey
    Employee ID: 1121
    Gender: F

    Department Information...:
    Department Name: Testing
    Assigned Work: Test Game OEM
    Time to complete work: 70




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.