Recap - Employee Hierarchy
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 55 of 104.
Challenge
EasyLet'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 baseEmployeeclass 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
- Protected members for name (
Manager.h: Define aManagerclass that publicly inherits fromEmployee. Managers receive a bonus based on their team size:- A private
int teamSizemember - 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
- A private
Developer.h: Define aDeveloperclass that publicly inherits fromEmployee. Developers have a specialty language and get a skill bonus:- A private
std::string languagemember - 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
- A private
main.cpp: Read six inputs (each on a separate line):- Manager name
- Manager ID
- Manager base salary
- Developer name
- Developer ID
- 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 calldisplayInfo()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: $5500This 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
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container