Skip to main content

Manager derived from person and employee

Short answer

Manager inherits Person (name, age) and Employee (empId, salary) via multiple inheritance, adding department and staffCount. Input and display functions are defined to read and print all data, with no ambiguity due to distinct member names.

Create a derived class manager from two base classes person and employee. Assume suitable data members in each class and display the information.

Class Manager is derived from two base classes, Person and Employee (multiple inheritance). Assumed data: Person has name and age; Employee has employee id and salary; Manager adds department and number of staff managed.

#include <iostream>
#include <string>
using namespace std;

class Person {
protected:
    string name;
    int age;
public:
    void getPerson() {
        cout << "Name: "; cin >> name;
        cout << "Age: ";  cin >> age;
    }
    void showPerson() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

class Employee {
protected:
    int empId;
    float salary;
public:
    void getEmployee() {
        cout << "Employee id: "; cin >> empId;
        cout << "Salary: ";      cin >> salary;
    }
    void showEmployee() {
        cout << "Id: " << empId << ", Salary: " << salary << endl;
    }
};

class Manager : public Person, public Employee {
    string department;
    int staffCount;
public:
    void getdata() {
        getPerson();
        getEmployee();
        cout << "Department: ";        cin >> department;
        cout << "Number of staff: ";   cin >> staffCount;
    }
    void display() {
        showPerson();
        showEmployee();
        cout << "Department: " << department
             << ", Staff managed: " << staffCount << endl;
    }
};

int main() {
    Manager m;
    m.getdata();
    cout << "\n--- Manager information ---" << endl;
    m.display();
    return 0;
}

Sample run:

Name: Ram
Age: 40
Employee id: 101
Salary: 85000
Department: Sales
Number of staff: 12

--- Manager information ---
Name: Ram, Age: 40
Id: 101, Salary: 85000
Department: Sales, Staff managed: 12

Manager inherits all public members of both bases, so it can call getPerson() and getEmployee() directly. Since the two bases have no members with the same name, there is no ambiguity.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 6 questions →