You must demonstrate Polymorphism for this problem. You want to develop an HR system for any company to carry information about employees who work there. Every employee has an id and name. Permanent employees are paid a fixed salary per month while those on contract are paid on hourly basis (information that needs to be kept is hours worked and hourly rate and the salary is the product of the two). i. Identify the classes needed and the relationship between them ii. Give the interface of these classes in (.h) files and implement the member functions in a separate .cpp file iii. Give a menu based program that allows a user to add new employees and print their data. It could be as follows:
http://prnt.sc/12u5jxt
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Employee
{
public:
Employee(string name, string id) { this->name = name; this->id = id; }
virtual void Display() = 0;
string GetID() { return id; }
string GetName() { return name; }
private:
string name;
string id;
};
class Permanent : public Employee
{
public:
Permanent(string name, string id, double salary);
void Display();
private:
double salary;
};
Permanent::Permanent(string name, string id, double salary) : Employee(name, id)
{
this->salary = salary;
}
void Permanent::Display()
{
cout << "Employee id: " << GetID() << ", Employee Name: " << GetName() << ", Salary: " << salary << endl;
}
class Contract : public Employee
{
public:
Contract(string name, string id, int hours, double rate);
void Display();
double GetSalary() { return hours * rate; }
private:
int hours;
double rate;
};
Contract::Contract(string name, string id, int hours, double rate) : Employee(name, id)
{
this->hours = hours;
this->rate = rate;
}
void Contract::Display()
{
cout << "Employee id: " << GetID() << ", Employee Name: " << GetName() << ", Salary: " << GetSalary() << endl;
}
void Display(vector<Employee*>& list);
int main()
{
cout << "Welcome to the HR system of NewTel." << endl;
vector<Employee*> list;
int choice = 0;
while (true)
{
cout << "Press 1 to see existing employees and press 2 to add a new employee: ";
cin >> choice;
if (choice == 2)
{
cout << "Enter the employee's id: ";
string id;
cin >> id;
cin.ignore();
cout << "Enter the employee's name: ";
string name;
getline(cin, name);
char ch;
cout << "Enter employee type (C for contract and P for permanent): ";
cin >> ch;
if (ch == 'P')
{
cout << "Enter employee's monthly salary: ";
double salary;
cin >> salary;
Employee* temp = new Permanent(name, id, salary);
list.push_back(temp);
}
else
{
cout << "Enter the employee's hourly rate: ";
double rate;
cin >> rate;
cout << "Enter the employee's hours worked: ";
int hours;
cin >> hours;
Employee* temp = new Contract(name, id, hours, rate);
list.push_back(temp);
}
}
if (choice == 1) Display(list);
cout << "Do you want to continue? Press Y for yes and any other character to exit." << endl;
char ch;
cin >> ch;
if (ch == 'Y') continue;
else break;
}
system("pause");
return 0;
}
void Display(vector<Employee*>& list)
{
cout << "Employees' data at NewTel" << endl;
for (int i = 0; i < list.size(); i++)
{
list[i]->Display();
}
}
Comments
Leave a comment