Double Inheritance

Double inheritance

Create two base classes Person and Worker.

The Person class should have: std::string name, and int age (public) The Worker class should have: int worker_id, and std::string position (public)

Create a derived class Employee that inherits publicly from both Person and Worker. Then, create an Employee object and set values for name, age, worker_id, and position. Finally, display the values of all these members.

Answer

#include <iostream>
#include <string>

struct Person {
    std::string name;
    int age;
};

struct Worker {
    int worker_id;
    std::string position;
};

struct Employee : public Person, public Worker {
};

int main() {
    Employee emp;
    emp.name = "Alice";
    emp.age = 30;
    emp.worker_id = 1234;
    emp.position = "Software Developer";

    std::cout << "Name: " << emp.name << "\n";
    std::cout << "Age: " << emp.age << "\n";
    std::cout << "Worker ID: " << emp.worker_id << "\n";
    std::cout << "Position: " << emp.position << "\n";

    return 0;
}
Questions