Currently there is no need for compute_pay() to be virtual because you never deal with the Worker class directly.
print() should be virtual also.
SalariedWorker and HourlyWorker should NOT have
name
and
type
members. They use the ones inherited from Worker.
Think of it this way. When you say:
1 2 3
|
class D : public B {
...
};
|
You're saying that an entire instance of class B is inside every instance of class D. So it's a lot like this:
1 2 3 4 5
|
class D {
public:
B base;
...
};
|
Worker should have a constructor to set it by name and type.
Most importantly, you to define virtual method compute_pay inside Worker.
HourlyWorker and SalariedWorker should call the Worker() constructor and pass it the name and the appropriate type.
main() is a bit of a mess.
Since you were pretty close, I'll give you the code that I came up with. You should study this (and or whatever others provide) to be sure you understand it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::vector;
using std::string;
class Worker {
public:
// If you've learned about references then you can do this more efficiently
Worker(string n, string t) {
name = n;
type = t;
}
void print() {
cout << name << " (" << type << ')';
}
// hours is double since workers can work fractions of an hour
// compute_pay() is abstract because you can't call it on
// a Worker instance - only on a derived class
virtual double compute_pay(double hours) = 0;
protected:
string name;
string type;
};
class HourlyWorker : public Worker {
public:
HourlyWorker(string n) :
Worker(n, "Hourly")
{}
virtual double compute_pay(double hours) {
double payment = 10;
if (hours <= 40) {
return hours * payment;
}
else {
return (40 * payment) + ((hours - 40) * payment * 1.5);
}
}
};
class SalariedWorker : public Worker{
public:
SalariedWorker(string n) :
Worker(n, "Salaried")
{}
virtual double compute_pay(double hours) {
hours = 40;
int payment = 10;
return payment * hours;
}
void print(int a) {
cout << name << " " << a << "\n";
}
};
int main()
{
// Here's an array of pointers to the base class. Each item will actually point to one
// instance of a derived class.
vector<Worker*> workers;
int first; //first digit
int hours; //hours of each worker
string name; //name or worker
string type; //sal or hou
cout << "go" << "\n";
cin >> first;
// This loop only reads the names and types.
for (int i = 0; i < first; i++) {
Worker *w;
cin >> name >> type;
// Now point w to a new SalariedWorker or a new HourlyWorker
if (type == "sal") {
w = new SalariedWorker(name);
} else if (type == "hou") {
w = new HourlyWorker(name);
} else {
cout << "Wrong input" << "\n";
return 1;
}
workers.push_back(w); // Add the (salaried or hourly) worker to the vector
}
// Now this loop reads the hours and prints the pay info
for (int z = 0; z < first; z++) {
cin >> hours; //reading the last line
workers[z]->print(); // prints their name and type
cout << ": " << hours << " hours. Pay = $"
<< workers[z]->compute_pay(hours) << '\n';
}
}
|