我的作业代码中的计算错误。怎么修?

时间:2011-11-27 22:51:40

标签: c++ debugging

这是我应该做的:

  • 编写基类Worker和2个派生类HourlyWorkerSalariedWorker。每个工人都有姓名和工资率。

  • 编写一个虚拟函数compute_pay(int hours),计算每个工人的每周工资。每小时工人在前40个小时内获得全额工资,并且在任何时间内都可以获得工资。无论实际的小时数是多少,受薪工人都会按小时工资支付40小时。

以下是我的代码,编译时没有错误。但计算都错了。我希望有人可以告诉我我的代码有什么问题。

#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;
}

这是我运行时的结果:

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

这就是我想要的

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

2 个答案:

答案 0 :(得分:8)

您的代码中存在许多问题。您在计算中看到这些奇数的原因是因为您的每个派生类(HourlyWorkerSalariedWorker)都有自己的私有namesalary和{{ 1}}成员。这些成员与基类中的payoutnamesalary成员相同,因为派生类不能直接访问私有成员。

那么,为什么奇数?这是因为您只通过调用基类构造函数来设置payout。这将基类设置为私有salary成员,但不是派生类成员。因此,派生类成员就是发生在内存中该位置的任何内容,所以当你将它乘以工作小时数时,会得到一个非常奇怪的结果。

您可以通过在基类中保护数据成员并从派生类定义中删除它们来解决此问题。 (名称仍然可以是私有的,因为您不在派生类中直接访问它。)这样,当您访问salary时,您将始终访问具有正确值的基类成员。

一旦你解决了这个问题,你应该看到合理的数字。但是,据我所知,您的预期值与您给出的赋值定义不一致。你必须自己解决这个问题。

除此之外,还要求您编写虚拟功能,但您的salary功能实际上并不是虚拟的。可以使用in the C++ FAQ对C ++中的虚函数进行很好的概述,因此我强烈建议您查看一下。简而言之,不同之处在于,如果在基类指针或实际上是派生类型的引用上调用虚函数,则将调用相应的派生类函数。如果函数是非虚函数,则始终会调用基类函数,即使派生类会覆盖它。

答案 1 :(得分:-2)

实际上,员工的工资总是保存在工人阶级而不是每小时和有薪阶层。所以每当你调用“compute_pay”时它就会使用垃圾值。所以要删除它在每小时和受薪类的构造函数中使用它。

SalariedWorker::SalariedWorker(string name, int salary)
:Worker(name, salary) {this->salary=salary;}

HourlyWorker::HourlyWorker(string name, int salary)
:Worker(name, salary) {this->salary=salary;}

this->salary=salary 

也将薪水复制到这些班级工资中。