Recap - Employee Hierarchy
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 24 of 70.
Challenge
EasyLet's build an employee hierarchy system that brings together all the inheritance concepts from this chapter. You'll create a company structure where different employee types share common attributes but calculate their bonuses differently.
You'll organize your code across four files:
Employee.cs: Define the baseEmployeeclass in theCompanynamespace. Every employee has aName(string) andSalary(decimal). Create a constructor that accepts both values. Add avirtualmethod calledCalculateBonus()that returns 10% of the salary by default. OverrideToString()to return"{Name} - ${Salary}".Manager.cs: Define aManagerclass in theCompanynamespace that inherits fromEmployee. Managers have an additionalTeamSizeproperty (int). The constructor should accept name, salary, and team size, usingbaseto initialize the parent. OverrideCalculateBonus()to return 15% of salary plus $50 for each team member. OverrideToString()to return"{Name} - ${Salary} (Manager, Team: {TeamSize})".Developer.cs: Define aDeveloperclass in theCompanynamespace that inherits fromEmployee. Developers have aProgrammingLanguageproperty (string). The constructor accepts name, salary, and programming language. OverrideCalculateBonus()to return 12% of salary. OverrideToString()to return"{Name} - ${Salary} (Developer, {ProgrammingLanguage})".Program.cs: In your main file, create one of each employee type using input values. Store all three in an array ofEmployeereferences to demonstrate polymorphism. Loop through the array and print each employee'sToString()result followed by their bonus.
You will receive six inputs:
- Base employee name
- Base employee salary
- Manager name
- Manager salary
- Manager team size
- Developer name
- Developer salary
- Developer programming language
Print each employee's information and bonus in this format:
{ToString result}
Bonus: ${CalculateBonus result}For example, if the inputs are John, 50000, Sarah, 80000, 5, Mike, 70000, and C#, the output should be:
John - $50000
Bonus: $5000
Sarah - $80000 (Manager, Team: 5)
Bonus: $12250
Mike - $70000 (Developer, C#)
Bonus: $8400Notice how each employee type calculates bonuses differently, yet you can process them all through a single Employee array. This is the power of combining inheritance with polymorphism!
Try it yourself
using System;
using Company;
class Program
{
public static void Main(string[] args)
{
// Read base employee inputs
string empName = Console.ReadLine();
decimal empSalary = Convert.ToDecimal(Console.ReadLine());
// Read manager inputs
string mgrName = Console.ReadLine();
decimal mgrSalary = Convert.ToDecimal(Console.ReadLine());
int teamSize = Convert.ToInt32(Console.ReadLine());
// Read developer inputs
string devName = Console.ReadLine();
decimal devSalary = Convert.ToDecimal(Console.ReadLine());
string progLang = Console.ReadLine();
// TODO: Create one Employee, one Manager, and one Developer
// TODO: Store all three in an Employee array to demonstrate polymorphism
// TODO: Loop through the array and print each employee's ToString() and bonus
// Format:
// {ToString result}
// Bonus: ${CalculateBonus result}
}
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator