You are on page 1of 2

#include <iostream>

#include <string>

// Base class

class Person {

private:

std::string name;

int age;

public:

Person(const std::string& n, int a) : name(n), age(a) {}

void displayInfo() {

std::cout << "Name: " << name << ", Age: " << age << std::endl;

};

// Intermediate class (inherits from Person)

class Employee : public Person {

private:

std::string employeeId;

public:

// Constructor for Employee class, calling the base class constructor explicitly

Employee(const std::string& n, int a, const std::string& id) : Person(n, a), employeeId(id) {}

void work() {

std::cout << "Employee with ID " << employeeId << " is working." << std::endl;

};
// Derived class (inherits from Employee)

class Manager : public Employee {

private:

std::string department;

public:

// Constructor for Manager class, calling the base class constructor explicitly

Manager(const std::string& n, int a, const std::string& id, const std::string& dept)

: Employee(n, a, id), department(dept) {}

void manageTeam() {

std::cout << "Manager of department " << department << " is managing the team." << std::endl;

};

int main() {

// Creating an instance of the Manager class

Manager myManager("John Doe", 35, "EMP123", "IT");

// Accessing methods from the base class (Person)

myManager.displayInfo();

// Accessing methods from the intermediate class (Employee)

myManager.work();

// Accessing methods from the derived class (Manager)

myManager.manageTeam();

return 0;

You might also like