Menu
Coddy logo textTech

Recap - Employee Hierarchy

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 24 of 70.

challenge icon

Challenge

Easy

Let'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 base Employee class in the Company namespace. Every employee has a Name (string) and Salary (decimal). Create a constructor that accepts both values. Add a virtual method called CalculateBonus() that returns 10% of the salary by default. Override ToString() to return "{Name} - ${Salary}".
  • Manager.cs: Define a Manager class in the Company namespace that inherits from Employee. Managers have an additional TeamSize property (int). The constructor should accept name, salary, and team size, using base to initialize the parent. Override CalculateBonus() to return 15% of salary plus $50 for each team member. Override ToString() to return "{Name} - ${Salary} (Manager, Team: {TeamSize})".
  • Developer.cs: Define a Developer class in the Company namespace that inherits from Employee. Developers have a ProgrammingLanguage property (string). The constructor accepts name, salary, and programming language. Override CalculateBonus() to return 12% of salary. Override ToString() 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 of Employee references to demonstrate polymorphism. Loop through the array and print each employee's ToString() 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: $8400

Notice 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