Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 (<code>HourlyWorker</code> and <code>SalariedWorker</code>) have their own private <code>name</code>, <code>salary</code> and <code>payout</code> members. These members are <em>not</em> the same as the <code>name</code>, <code>salary</code> and <code>payout</code> members in the base class, because derived classes do not have direct access to private members.</p> <p>So, why the odd numbers? It's because you only ever set <code>salary</code> by calling the base class constructor. That sets the base class' private <code>salary</code> member, but <em>not</em> 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.</p> <p>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 <code>salary</code>, you'll always be accessing the base class member, which will have the correct value.</p> <p>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.</p> <p>In addition to that, you were asked to write a <em>virtual</em> function, but your <code>compute_pay</code> function is not actually virtual. A good overview of virtual functions in C++ is available <a href="http://www.parashift.com/c++-faq-lite/virtual-functions.html">in the C++ FAQ</a>, 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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