我的代码有效,但是setter不会创建新对象,所以我得到了错误的结果

时间:2017-10-30 02:27:41

标签: c++

所以,每当我发布以下代码时,我得到错误的回复, 类成员变量确实用我的setter更新,但由于某种原因,最终的成本是不正确的。我不允许更改main.cpp代码

我在想它可能与setter有关但我无法弄清楚如何强制setter调用构造函数以便调用setTotalCost()

的main.cpp

#include <iostream>
#include <iomanip>
#include "Inventory.h"

using namespace std;

int main()
{
    cout << fixed
         << showpoint
         << setprecision(2);

    // Demonstrate the default constructor
    Inventory stockItem1;
    cout << "\nDemonstrating the default constructor...\n";
    cout << "Item number: " << stockItem1.getItemNumber() << endl;
    cout << "Quantity   : " << stockItem1.getQuantity() << endl;
    cout << "Cost       : " << stockItem1.getCost() << endl;
    cout << "Total Cost : " << stockItem1.getTotalCost() << endl;

    // Now demonstrate the overloaded constructor
    Inventory stockItem2(124, 12, 84.95);
    cout << "\nDemonstrating the overloaded constructor...\n";
    cout << "Item number: " << stockItem2.getItemNumber() << endl;
    cout << "Quantity   : " << stockItem2.getQuantity() << endl;
    cout << "Cost       : " << stockItem2.getCost() << endl;
    cout << "Total Cost : " << stockItem2.getTotalCost() << endl;

    // Now demonstrate the member "set" functions
    stockItem2.setItemNumber(243);
    stockItem2.setQuantity(50);
    stockItem2.setCost(9.50);
    cout << "\nDemonstrating the \"set\" functions...\n";
    cout << "Item number: " << stockItem2.getItemNumber() << endl;
    cout << "Quantity   : " << stockItem2.getQuantity() << endl;
    cout << "Cost       : " << stockItem2.getCost() << endl;
    cout << "Total Cost : " << stockItem2.getTotalCost() << endl;

    // Now demonstrate the input validation functions
    cout << "\nDemonstrating the input validation functions...\n";
    stockItem2.setItemNumber(-1);
    stockItem2.setQuantity(-1);
    stockItem2.setCost(-1);

    cout << "\nItem number: " << stockItem2.getItemNumber() << endl;
    cout << "Quantity   : " << stockItem2.getQuantity() << endl;
    cout << "Cost       : " << stockItem2.getCost() << endl;
    cout << "Total Cost : " << stockItem2.getTotalCost() << endl;

    return 0;
}

Inventory.h

//Header File

#include<iostream>
#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory
{
private:
    int itemNumber;
    int quantity;
    double cost;
    double totalCost;

public:
    // default constructor, setting all values to 0
    Inventory();
    Inventory(int, int, double);
    void setItemNumber(int );
    void setQuantity(int );
    void setCost(double );
    void setTotalCost();
    int getItemNumber();
    int getQuantity();
    double getCost();
    double getTotalCost();




};

#endif //PROGRAM6_INVENTORY_H

Inventory.cpp

#include <iostream>
#include "Inventory.h"
using namespace std;

Inventory :: Inventory()
{
    itemNumber = 0;
    quantity = 0;
    cost = 0;
    totalCost = 0;
}

Inventory ::Inventory(int itemNumber, int quantity, double cost)
{
    setItemNumber(itemNumber);
    setQuantity(quantity);
    setCost(cost);
    setTotalCost();
}

void Inventory ::setItemNumber(int theItemNumber)
{
    if (theItemNumber > 0)
    {
        itemNumber = theItemNumber;
    }
    else
    {
        cout << "You entered " << theItemNumber << " as your item number. Only positive numbers are accepted"<<endl;
    }
}

void Inventory ::setQuantity(int quantityOfItems)
{
    cout << "Setting quantity to " << quantityOfItems << endl;
    if (quantityOfItems > 0)
    {
        quantity = quantityOfItems;
    }
    else
    {
        cout << "You entered " << quantityOfItems << " as your item quantity. Only positive numbers are accepted"
             << endl;
    }
}

void Inventory ::setCost(double costOfItems)
{
    if (costOfItems > 0)
    {
        cost = costOfItems;
    }
    else
    {
        cout << "You entered " << costOfItems << " item cost. Only positive numbers are accepted" << endl;
        exit(0);
    }
}

void Inventory ::setTotalCost()
{
    int itemCount = getQuantity();
    double itemCost = getCost();
    totalCost = itemCost * itemCount;

}

int Inventory ::getItemNumber()
{
    return itemNumber;
}
int Inventory ::getQuantity()
{
    return quantity;
}
double Inventory ::getCost()
{
    return cost;
}
double Inventory ::getTotalCost()
{
    return totalCost;
}

我得到以下输出:

Demonstrating the default constructor...

Item number: 0

Quantity   : 0

Cost       : 0.00

Total Cost : 0.00

Demonstrating the overloaded constructor...

Item number: 124

Quantity   : 12

Cost       : 84.95

Total Cost : 1019.40


Demonstrating the "set" functions...

Item number: 243

Quantity   : 50

Cost       : 9.50

Total Cost : 1019.40


Demonstrating the input validation functions...

You entered -1 as your item number. Only positive numbers are accepted

You entered -1 as your item quantity. Only positive numbers are accepted

You entered -1.00 item cost. Only positive numbers are accepted

处理完成,退出代码为0

2 个答案:

答案 0 :(得分:0)

对于我所看到的,在更改其他变量后,不要调用setTotalCost()。它不会自动发生。 希望它有所帮助。

答案 1 :(得分:0)

因此,您需要这样做,以便getTotalCost()返回正确的值,而不更新setTotalCost()后,将更新该值的输入。有两种方法可以执行此操作 - 您可以使用输入的setter调用setter,或者根本不能存储值totalCost并在每次调用getTotalCost()时重新计算。