Here’s what I’m supposed to do:
-
Write a base class Worker and 2 derived classes
HourlyWorkerandSalariedWorker. Every worker has a name and salary rate. -
Write a virtual function
compute_pay(int hours)that computes the weekly pay for every worker. Hourly worker gets pay in full for the first 40 hours and haft the rate for any hours over that. The salaried worker gets paid the hourly wage for 40 hours, no matter what the actual number of hours is.
And below is my code, which compiles without error. But the calculations are all wrong. I hope someone can tell me what’s wrong with my code.
#include <iostream>
#include <string>
using namespace std;
//////////// Worker /////////////////////////////
class Worker
{
public:
Worker();
Worker(string name, int salary);
void print();
int compute_pay(int hours);
string get_name();
int get_salary();
private:
string name;
int salary;
int payout;
};
Worker::Worker() {salary = 0;}
Worker::Worker(string name, int salary)
{
this->name = name;
this->salary = salary;
}
void Worker::print()
{
cout << "Worker name: " << this->name;
cout << "Salary: " << this->salary;
}
int Worker::compute_pay(int hours)
{
this->payout = this->salary * hours;
return payout;
}
string Worker::get_name() {return this->name;}
int Worker::get_salary() { return this->salary;}
//////////// HourlyWorker ///////////////////////
class HourlyWorker : public Worker
{
public:
HourlyWorker(string name, int salary);
int compute_pay(int hours);
private:
string name;
int salary;
int payout;
};
HourlyWorker::HourlyWorker(string name, int salary)
:Worker(name, salary) {}
int HourlyWorker::compute_pay(int hours)
{
int temp = 0;
if (hours >= 40)
{
temp = (this->salary * (hours - 40)) / 2;
this->payout = (this->salary * 40) + temp;
}
else
{
this->payout = this->salary * hours;
}
return payout;
}
//////////// SalariedWorker /////////////////////
class SalariedWorker : public Worker
{
public:
SalariedWorker(string name, int salary);
int compute_pay(int hours);
private:
string name;
int salary;
int payout;
};
SalariedWorker::SalariedWorker(string name, int salary)
:Worker(name, salary) {}
int SalariedWorker::compute_pay(int hours)
{
this->payout = this->salary * 40;
return payout;
}
/////////
int main()
{
HourlyWorker a("Sam", 20);
HourlyWorker b("Mary", 15);
SalariedWorker c("Tom", 30);
SalariedWorker d("Pat", 40);
cout << "Hourly worker " << a.get_name() << " earns $"
<< a.get_salary();
cout << " and worked 20 hours for a pay of $" << a.compute_pay(20)
<< "\n";
cout << "Hourly worker " << b.get_name() << " earns $"
<< b.get_salary();
cout << " and worked 50 hours for a pay of $" << b.compute_pay(50)
<< "\n";
cout << "Salaried worker " << c.get_name() << " earns $"
<< c.get_salary();
cout << " and worked 20 hours for a pay of $" << c.compute_pay(20)
<< "\n";
cout << "Salaried worker " << d.get_name() << " earns $"
<< d.get_salary();
cout << " and worked 50 hours for a pay of $" << d.compute_pay(50)
<< "\n";
return 0;
}
This is the result when i run it:
Hourly worker Sam earns $20 and worked 20 hours for a pay of $85081400 Hourly worker Mary earns $15 and worked 50 hours for a pay of $-1915569464 Salaried worker Tom earns $30 and worked 20 hours for a pay of $91731520 Salaried worker Pat earns $40 and worked 50 hours for a pay of $162529280
and this is what i suppose to get
Hourly worker Sam earns $20 and worked 20 hours for a pay of $400 Hourly worker Mary earns $15 and worked 50 hours for a pay of $825 Salaried worker Tom earns $30 and worked 20 hours for a pay of $1200 Salaried worker Pat earns $40 and worked 50 hours for a pay of $2000
There are a number of problems in your code. The reason you’re seeing such odd numbers in your calculations is because each of your derived classes (
HourlyWorkerandSalariedWorker) have their own privatename,salaryandpayoutmembers. These members are not the same as thename,salaryandpayoutmembers in the base class, because derived classes do not have direct access to private members.So, why the odd numbers? It’s because you only ever set
salaryby calling the base class constructor. That sets the base class’ privatesalarymember, but not the derived class member. Thus, the derived class member is just whatever happened to be at that location in memory, so when you multiply it by the number of hours worked, you get a very odd result.You can fix that by making the data members protected in the base class, and removing them from the derived class definitions. (Name can still be private since you don’t directly access it in the derived classes.) That way, when you access
salary, you’ll always be accessing the base class member, which will have the correct value.Once you fix that, you should see reasonable numbers. However, your expected values don’t square with the assignment definition that you gave, as far as I can tell. You’ll have to sort that out on your own.
In addition to that, you were asked to write a virtual function, but your
compute_payfunction is not actually virtual. A good overview of virtual functions in C++ is available in the C++ FAQ, so I strongly suggest that you take a look at that. In brief, though, the difference is that if you call a virtual function on a base class pointer or reference which is actually of a derived type, the appropriate derived class function will be called. If the function is non-virtual, the base class function will always be called, even if the derived class overrides it.