Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#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;

};

// First derived 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;

};
// Second derived class (inherits from Person)

class Student : public Person {

private:

std::string studentId;

public:

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

Student(const std::string& n, int a, const std::string& id) : Person(n, a), studentId(id) {}

void study() {

std::cout << "Student with ID " << studentId << " is studying." << std::endl;

};

// Derived class with multiple inheritance (inherits from Employee and Student)

class WorkingStudent : public Employee, public Student {

public:

// Constructor for WorkingStudent class, calling the constructors of both base classes

WorkingStudent(const std::string& n, int a, const std::string& empId, const std::string& stuId)

: Employee(n, a, empId), Student(n, a, stuId) {}

void doBoth() {

std::cout << "Working student is multitasking." << std::endl;

};

int main() {

// Creating an instance of the WorkingStudent class

WorkingStudent myWorkingStudent("Alice", 22, "EMP456", "STU789");

// Accessing methods from the base class (Person)


myWorkingStudent.displayInfo();

// Accessing methods from the first derived class (Employee)

myWorkingStudent.work();

// Accessing methods from the second derived class (Student)

myWorkingStudent.study();

// Accessing methods from the class with multiple inheritance (WorkingStudent)

myWorkingStudent.doBoth();

return 0;

You might also like