Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:16:41+00:00 2026-05-27T03:16:41+00:00

Here’s what I’m supposed to do: Write a base class Worker and 2 derived

  • 0

Here’s what I’m supposed to do:

  • Write a base class Worker and 2 derived classes HourlyWorker and SalariedWorker. 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:16:41+00:00Added an answer on May 27, 2026 at 3:16 am

    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 (HourlyWorker and SalariedWorker) have their own private name, salary and payout members. These members are not the same as the name, salary and payout members 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 salary by calling the base class constructor. That sets the base class’ private salary member, 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_pay function 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is what is supposed to happen: The moment the user chooses an option
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here is a snippet of my xaml: <ComboBox x:Name=cbo1 Width=100 SelectedValue=200> <ComboBoxItem Name=n1>100</ComboBoxItem> <ComboBoxItem
Here is an example: I write html code inside of textarea, then I swap
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here is an example: I have a file 1.js, which has some functions. I
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.