Need help on where i should call a virtual function

Sorry for the long ass code, let me explain briefly. I made a main class - Worker and two derived classes - hourlyworker, salaried worker. I made a virtual function that calculates a salary, no need to explain it, that's not my question.

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
97
98
99
  #include <iostream>
  #include <fstream>
  #include <string>
  #include <vector>
  using namespace std;

  class Worker {
  public:
    void print();
  protected:
    string name;
    string type;
  };

  class HourlyWorker : public  Worker {
  public:
    HourlyWorker(string n) {
        name = n;
    }
    virtual double compute_pay(int hours) {
        double payment = 10;
        if (hours <= 40) {
            return hours * payment;
        }
        else {
            return (40 * payment) + ((hours - 40) * payment * 1.5);
        }
    }
    void print(int a) {
        cout << name << "  " << a << "\n";
    }
   private:
    string name;
    string type;
   }

   class SalariedWorker : public Worker{
   public:
    SalariedWorker(string n) {
        name = n;
    }
    virtual double compute_pay(int hours) {
        hours = 40;
        int payment = 10;
        return payment * hours;
    }
    void print(int a) {
        cout << name << "  " << a << "\n";
    }
    private:
    string name;
    string type;
    };

    void print(vector<SalariedWorker*> sal, int a)
    {
    for (int i = 0; i < sal.size(); i++)
        sal[i]->print(a);
    }
    void print(vector<HourlyWorker*> sal, int a)
    {
    for (int i = 0; i < sal.size(); i++)
        sal[i]->print(a);
    }

   int main()
   {
    vector<SalariedWorker*> sal;
    vector<HourlyWorker*> hour;
    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;
    for (int i = 0; i < first+1; i++) {
        if (i < first) {
            cin >> name >> type;
            if (type == "sal") {
                SalariedWorker* sall = new SalariedWorker(name);
                sal.push_back(sall); //If a worker is "sal" i write into the HourlyWorker vector
            }
            else if (type == "hou") {
                HourlyWorker* hourr = new HourlyWorker(name);
                hour.push_back(hourr);
            } //If a worker is "hou" i write into the HourlyWorker vector
            else {
                cout << "Wrong input" << "\n";
                return 1;
            }
        }
        else { //This triggers when we reach the last line. It triggers only 
       once
            for (int z = 0; z < first; z++) {
                cin >> hours; //reading the last line
            }
        }
       }
        }


Here's an example of how it should work: 1 - It reads a name of a person. 2 - It reads sal/hou (payment method) and depending on that it puts the person in one of the two vectors - sal, hour. 3 - After it reads all the names it reads one more line - the work hours for every worker. First digit is for first worker, second for second, etc. After doing so, depending on the type of payment - sal/hou it calls the function compute_pay and that way it calculates the payment.

Again, sorry for the long code, you don't have to go trough all of it, just the main class, and more importantly - the loop. Here's my question: where exactly should i call my compute_pay function so that it goes thorugh all the lines, assuming the int variable it requiers is the int hours variable i read at the last line? Also, where should i call my print function afterwards so i can see my output? I know it's a stupid or complicated question, but i honestly tried my best and I cannot figure it out :/.


where exactly should i call my compute_pay function so that it goes thorugh all the lines, assuming the int variable it requiers is the int hours variable i read at the last line? Also, where should i call my print function afterwards so i can see my output?
At a loop after line 98. Within the for-loop, after line 95.

Be aware that your program design is pretty terrible, especially the derivation of your subclasses!
Last edited on
Be aware that your program design is pretty terrible, especially the derivation of your subclasses!

That's not a helpful comment. If you have specific advice as to what's wrong with the OP's design, and how to improve it, then by all means give it. But simply making sneering comments about other people's code is rude and obnoxious, and contributes nothing of value.
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';
    }
}
Topic archived. No new replies allowed.