0% found this document useful (0 votes)
20 views2 pages

Multi Level

The document shows an example of inheritance in C++ with a Person base class and Employee and Manager derived classes. The Manager class inherits from Employee which inherits from Person, and constructor calls ensure proper initialization of base class members. An instance of the Manager class is created and methods from each class in the hierarchy are accessed.

Uploaded by

Ankur Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Multi Level

The document shows an example of inheritance in C++ with a Person base class and Employee and Manager derived classes. The Manager class inherits from Employee which inherits from Person, and constructor calls ensure proper initialization of base class members. An instance of the Manager class is created and methods from each class in the hierarchy are accessed.

Uploaded by

Ankur Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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