Question: // Workshop 8: Virtual Functions // Date: 05.07.2020 // Author: Cornel // Description: // Tests the Employee and its derived classes via virtual functions /////////////////////////////////////////////

// Workshop 8: Virtual Functions

// Date: 05.07.2020

// Author: Cornel

// Description:

// Tests the Employee and its derived classes via virtual functions

/////////////////////////////////////////////

#include

#include "Doctor.h"

#include "Doctor.h"

#include "Engineer.h"

#include "Engineer.h"

#include "Employee.h"

using namespace std;

using namespace sdds;

void printHeader(const char* title)

{

char oldFill = cout.fill('-');

cout.width(40);

cout << "" << endl;

cout << "|> " << title << endl;

cout.fill('-');

cout.width(40);

cout << "" << endl;

cout.fill(oldFill);

}

int main()

{

Employee* employees[]{

new Doctor("Family doctor", 90, 30),

new Doctor("Internal medicine", 150, 25, false),

new Doctor("Neuro-surgeon", 325, 12, true),

new Engineer(75, 12),

new Engineer(60, 25)

};

{

printHeader("T1: Checking display()");

for (auto i = 0; i < 5; ++i)

employees[i]->display();

cout << endl;

}

{

printHeader("T2: Doctor's weekly income");

for (auto i = 0; i < 3; ++i)

{

cout << "Employee " << i + 1 << endl;

cout << "Under-time: " << employees[i]->getSalary(10) << endl;

cout << "Over-time: " << employees[i]->getSalary(40) << endl;

}

cout << endl;

}

{

printHeader("T2: Engineer's weekly income");

for (auto i = 3; i < 5; ++i)

{

cout << "Employee " << i + 1 << endl;

cout << "Under-time: " << employees[i]->getSalary(10) << endl;

cout << "Overtime-time: " << employees[i]->getSalary(40) << endl;

}

cout << endl;

}

// Cleaning up the memory

for (auto i = 0; i < 5; ++i)

delete employees[i];

return 0;

}

//output

/*

----------------------------------------

|> T1: Checking display()

----------------------------------------

Doctor

Type: Family doctor

Pay Rate: 90

Min Hours: 30

Doctor

Type: Internal medicine

Pay Rate: 150

Min Hours: 25

Doctor

Type: Neuro-surgeon (specialist)

Pay Rate: 325

Min Hours: 12

Engineer

Level: 12 //fix mee, i get hot garb values

Pay Rate: 75

Min Hours: 30 //fix me, i get gaaarb

Engineer

Level: 25

Pay Rate: 60

Min Hours: 30

----------------------------------------

|> T2: Doctor's weekly income

----------------------------------------

Employee 1

Under-time: 900

Over-time: 4050

Employee 2

Under-time: 1500

Over-time: 7125

Employee 3

Under-time: 5250

Over-time: 19550

----------------------------------------

|> T2: Engineer's weekly income

----------------------------------------

Employee 4

Under-time: 984 //fix math

Overtime-time: 3480 //fix math

Employee 5

Under-time: 1280

Overtime-time: 3400

*/

Part 1 (100%)

EmployeeModule

Design and code a class namedEmployeethat holds information about an employee. Place your class definition in a header file namedEmployee.hand your function definitions in an implementation file namedEmployee.cpp.

Include in your solution all of the statements necessary for your code to compile under a standard C++ compiler and within thesddsnamespace.

EmployeeClass

Design and code a class namedEmployeethat holds information about an employee.

Assume all parameters are valid.

EmployeePrivate Members

The class should be able to store the following data:

  • aconstantinteger representing the minimum number of hours an employee must work in a week.Very Important:this must be a member constant.
  • a number representing the hourly salary of the employee, as a floating point number in double precision.

Important:Constant and referene attributes cannot be initialized in the body of a constructor like regular attributes (explain in the reflection why is that). You must use aninitializer list. For more information readhttps://en.cppreference.com/w/cpp/language/constructor

You can add any otherprivatemembers in the class, as required by your design.

EmployeeProtected Members

  • two queries to retrieve the values of the two attributes (minimum working hours and hourly salary).

EmployeePublic Members

This class willNOThave a default constructor!

  • a custom constructor that receives as parameters the hourly salary and the minimum number of hours the employee must work in a week.
  • double getSalary(int workedHours): apure virtual querythat calculates the salary earned by the employee during a week in which she worked the number of hours specified in the parameter. Each type of employee will use a different formula to calculate salary.
  • ostream& display(ostream& out): apure virtual querythat inserts into the stream the representation of the current instance. Each type of employee has different data to print. The parameter will havestd::coutas a default value.
  • avirtual, empty-body, destructor. Explain in reflection why is it necessary to add the destructor to the class, considering that the implementation contains no statements.

Free Helper Functions

  • overload the insertion operators to insert anEmployeeinto a stream. This operator should call thedisplaymember function of the classEmployee. In the previous workshop you had to overload this operator for each class on the hierarchy, but in this workshop you only overload it here. Why is working properly in this workshop but not in the previous one? Note that the functiondisplayhas no implementation in this class.Explain in the reflection.

DoctorModule

Design and code a class namedDoctorthat holds information about a doctor. Place your class definition in a header file namedDoctor.hand your function definitions in an implementation file namedDoctor.cpp.

Include in your solution all of the statements necessary for your code to compile under a standard C++ compiler and within thesddsnamespace.

DoctorClass

Design and code a class namedDoctorthat holds information about a doctor.This class should inherit fromEmployeeclass.

Assume all parameters are valid.

DoctorPrivate Members

The class should be able to store the following data (on top of data coming from the parent class):

  • the type of doctor (surgeon, general medicine, family medicine, etc.) as an array of characters of size 32.
  • a boolean value that istrueif the doctor is a specialist.

You can add any other private members in the class, as required by your design.Do not duplicate members from the base class!

DoctorPublic Members

  • a custom constructor that receives as parameters the type of doctor, the hourly salary, minimum numbers of hours the doctor must work in a week, and a value signaling if the doctor is a specialist. The last parameter should have a default value offalse.
  • double getSalary(int workedHours): overrides the base class function. Calculates the salary according to the following rules:
  • if the doctor worked minimum number of hours or less, multiply the parameter by the hourly rate
  • if the doctor worked overtime, the minimum number of hours are paid at hourly rate, and anything extra is paid at 150% hourly rate.
  • if the doctor is a specialist, she receives extra 2000 regardless of the numbers of hours worked.
  • ostream& display(ostream& out): overrides the base class function. Inserts into the stream the content of the object in the format
Doctor Type: [TYPE] Pay Rate: [HOURLY_RATE] Min Hours: [MIN_HOURS] 
  • If the doctor is a specialist, use the format:
Doctor Type: [TYPE] (specialist) Pay Rate: [HOURLY_RATE] Min Hours: [MIN_HOURS] 
  • The labels should be printed on a field of size 16, aligned to the right.

EngineerModule

Design and code a class namedEngineerthat holds information about an engineer. Place your class definition in a header file namedEngineer.hand your function definitions in an implementation file namedEngineer.cpp.

Include in your solution all of the statements necessary for your code to compile under a standard C++ compiler and within thesddsnamespace.

EngineerClass

Design and code a class namedEngineerthat holds information about an engineer.This class should inherit fromEmployeeclass.

Assume all parameters are valid.

EngineerPrivate Members

The class should be able to store the following data (on top of data coming from the parent class):

  • an integer representing the level of an engineer. This value is used in calculating the salary.

You can add any other private members in the class, as required by your design.Do not duplicate members from the base class!

EngineerPublic Members

  • a custom constructor that receives as parameters the hourly salary and the level. The engineer must work minimum 30 hours per week.
  • double getSalary(int workedHours): overrides the base class function. Calculates the salary according to the following rules:
  • multiply the parameter by the hourly rate (the engineer is paid the same amount for all worked hours)
  • add an extra 40 dollars for each level the engineer has
  • if the engineer worked less than the minimum number of hours, cut 20% of her salary.
  • ostream& display(ostream& out): overrides the base class function. Inserts into the stream the content of the object in the format
Engineer Level: [LEVEL] Pay Rate: [HOURLY_RATE] Min Hours: [MIN_HOURS] 
  • The labels should be printed on a field of size 20, aligned to the right.

mainModule (supplied)

Do not modify this module!Look at the code and make sure you understand it.

Sample Output

The output should look like the one from theexpected_output.txtfile.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!