Menu
Coddy logo textTech

Recap - Employee Hierarchy

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 55 of 104.

challenge icon

Challenge

Easy

Let's build a complete employee hierarchy system that brings together all the inheritance concepts you've learned in this chapter. You'll create a company structure with different employee types that share common attributes but have specialized behaviors.

You'll organize your code across four files:

  • Employee.h: Define the base Employee class that all employee types will inherit from. Every employee has a name, an ID, and a base salary. Include:
    • Protected members for name (std::string), id (int), and baseSalary (double)
    • A constructor that initializes all three values
    • A virtual displayInfo() method that prints: Employee: <name> (ID: <id>)
    • A virtual calculateSalary() method that returns the base salary
    • A virtual destructor
  • Manager.h: Define a Manager class that publicly inherits from Employee. Managers receive a bonus based on their team size:
    • A private int teamSize member
    • A constructor that takes name, id, base salary, and team size — chain to the base constructor appropriately
    • Override displayInfo() to print: Manager: <name> (ID: <id>) - Team of <teamSize>
    • Override calculateSalary() to return base salary plus 500 for each team member
  • Developer.h: Define a Developer class that publicly inherits from Employee. Developers have a specialty language and get a skill bonus:
    • A private std::string language member
    • A constructor that takes name, id, base salary, and programming language
    • Override displayInfo() to print: Developer: <name> (ID: <id>) - <language>
    • Override calculateSalary() to return base salary plus a 1000 skill bonus
  • main.cpp: Read six inputs (each on a separate line):
    1. Manager name
    2. Manager ID
    3. Manager base salary
    4. Developer name
    5. Developer ID
    6. Developer base salary

    Create a Manager with team size 4 and a Developer who specializes in "C++". Store pointers to both in an array of Employee* pointers. Loop through the array to call displayInfo() and print the calculated salary for each employee in this format:

    <displayInfo output>
    Salary: <displayInfo output>
    Salary: $<calculated salary>lt;calculated salary>

    Print a blank line between employees. After displaying all employees, properly delete the dynamically allocated objects.

For example, with inputs Sarah, 101, 5000, Mike, 102, and 4500:

Manager: Sarah (ID: 101) - Team of 4
Salary: $7000

Developer: Mike (ID: 102) - C++
Salary: $5500

This challenge demonstrates polymorphism in action — calling virtual methods through base class pointers invokes the correct derived class implementations. Remember to use the override keyword and include a virtual destructor in your base class for proper cleanup.

Try it yourself

#include <iostream>
#include <string>
#include "Employee.h"
#include "Manager.h"
#include "Developer.h"

using namespace std;

int main() {
    // Read Manager inputs
    string managerName;
    int managerId;
    double managerSalary;
    
    getline(cin, managerName);
    cin >> managerId;
    cin >> managerSalary;
    cin.ignore();
    
    // Read Developer inputs
    string developerName;
    int developerId;
    double developerSalary;
    
    getline(cin, developerName);
    cin >> developerId;
    cin >> developerSalary;
    
    // TODO: Create a Manager with team size 4
    // TODO: Create a Developer who specializes in "C++"
    
    // TODO: Create an array of Employee* pointers to store both employees
    
    // TODO: Loop through the array and for each employee:
    //   - Call displayInfo()
    //   - Print: Salary: $<calculated salary>
    //   - Print a blank line between employees (not after the last one)
    
    // TODO: Delete the dynamically allocated objects
    
    return 0;
}

All lessons in Object Oriented Programming