Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculations are wrong in my homework code. How to fix?
    text
    copied!<p>Here's what I'm supposed to do:</p> <ul> <li><p>Write a base class Worker and 2 derived classes <code>HourlyWorker</code> and <code>SalariedWorker</code>. Every worker has a name and salary rate.</p></li> <li><p>Write a virtual function <code>compute_pay(int hours)</code> 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.</p></li> </ul> <p>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.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; 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-&gt;name = name; this-&gt;salary = salary; } void Worker::print() { cout &lt;&lt; "Worker name: " &lt;&lt; this-&gt;name; cout &lt;&lt; "Salary: " &lt;&lt; this-&gt;salary; } int Worker::compute_pay(int hours) { this-&gt;payout = this-&gt;salary * hours; return payout; } string Worker::get_name() {return this-&gt;name;} int Worker::get_salary() { return this-&gt;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 &gt;= 40) { temp = (this-&gt;salary * (hours - 40)) / 2; this-&gt;payout = (this-&gt;salary * 40) + temp; } else { this-&gt;payout = this-&gt;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-&gt;payout = this-&gt;salary * 40; return payout; } ///////// int main() { HourlyWorker a("Sam", 20); HourlyWorker b("Mary", 15); SalariedWorker c("Tom", 30); SalariedWorker d("Pat", 40); cout &lt;&lt; "Hourly worker " &lt;&lt; a.get_name() &lt;&lt; " earns $" &lt;&lt; a.get_salary(); cout &lt;&lt; " and worked 20 hours for a pay of $" &lt;&lt; a.compute_pay(20) &lt;&lt; "\n"; cout &lt;&lt; "Hourly worker " &lt;&lt; b.get_name() &lt;&lt; " earns $" &lt;&lt; b.get_salary(); cout &lt;&lt; " and worked 50 hours for a pay of $" &lt;&lt; b.compute_pay(50) &lt;&lt; "\n"; cout &lt;&lt; "Salaried worker " &lt;&lt; c.get_name() &lt;&lt; " earns $" &lt;&lt; c.get_salary(); cout &lt;&lt; " and worked 20 hours for a pay of $" &lt;&lt; c.compute_pay(20) &lt;&lt; "\n"; cout &lt;&lt; "Salaried worker " &lt;&lt; d.get_name() &lt;&lt; " earns $" &lt;&lt; d.get_salary(); cout &lt;&lt; " and worked 50 hours for a pay of $" &lt;&lt; d.compute_pay(50) &lt;&lt; "\n"; return 0; } </code></pre> <p>This is the result when i run it:</p> <blockquote> <pre><code>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 </code></pre> </blockquote> <p>and this is what i suppose to get</p> <blockquote> <pre><code>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 </code></pre> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload