所以,我刚才有一个关于我的教授对我们所做的一个项目的反馈的问题。我不太理解她对第二个构造函数中的setter函数的意思。以下是她所说的:
下次确保在尝试实验室之前完成阅读和准备工作。仔细阅读作业。
问题是重载的构造函数(构造函数#2)应该调用setter函数。
未测试重载的构造函数(构造函数#2)。使用重载的构造函数创建一个对象,如下所示:Inventory itemTwo(666,3,.99);
漂亮的课程,漂亮的主程序和编码风格。
这就是问题:
最后,这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
class Inventory
{
private:
int itemNumber; //Private Class Members
int quanity;
double cost;
public:
Inventory(); //Public Member Functions
Inventory(int i, int q, double c);
void setItemNumber(int);
void setQuanity(int);
void setCost(double);
int getItemNumber() const;
int getQuanity() const;
double getCost() const;
double getTotalCost() const;
};
int main()
{
int itemNum;
int qty;
double price;
Inventory itemOne; //Declares itemOne and itemTwo being an Inventory class objects
Inventory itemTwo;
/****************************************************************************/
cout << "Please Enter Data For Item One\n"; //Data entry for Item One
cout << "Enter item number: ";
cin >> itemNum;
while (itemNum < 0) //Safegaurds for data
{
cout << "Please enter a non-negative item number: ";
cin >> itemNum;
}
cout << "Enter quanity: ";
cin >> qty;
while (qty < 0)
{
cout << "Please enter a non-negative quanity number: ";
cin >> qty;
}
cout << "Enter price: ";
cin >> price;
while (price < 0)
{
cout << "Please enter a non-negative price: ";
cin >> price;
}
itemOne.setItemNumber(itemNum); //Passes All Data To Correct Functions
itemOne.setQuanity(qty);
itemOne.setCost(price);
cout << endl; //Formatting
/****************************************************************************/
cout << "Please Enter Data For Item Two\n"; //Data Entry for Item Two
cout << "Enter item number: ";
cin >> itemNum;
while (itemNum < 0)
{
cout << "Please enter a non-negative item number: ";
cin >> itemNum;
}
cout << "Enter quanity: ";
cin >> qty;
while (qty < 0)
{
cout << "Please enter a non-negative quanity number: ";
cin >> qty;
}
cout << "Enter price: ";
cin >> price;
while (price < 0)
{
cout << "Please enter a non-negative price: ";
cin >> price;
}
itemTwo.setItemNumber(itemNum); //Passes All Data To Correct Functions
itemTwo.setQuanity(qty);
itemTwo.setCost(price);
cout << endl; //Formatting
/****************************************************************************/
cout << fixed << showpoint << setprecision(2);
//All input displayed here
cout << "Here's the data you entered for item one:\n";
cout << "Item Number: " << " " << itemOne.getItemNumber() << endl;
cout << "Item Quanity: " << " " << itemOne.getQuanity() << endl;
cout << "Item Cost: " << " " << "$" << itemOne.getCost() << endl;
cout << "Item Total Cost: " << " " <<"$" << itemOne.getTotalCost() << endl << endl;
cout << "Here's the data you entered for item two:\n";
cout << "Item Number: " << " " << itemTwo.getItemNumber() << endl;
cout << "Item Quanity: " << " " << itemTwo.getQuanity() << endl;
cout << "Item Cost: " << " " << "$" << itemTwo.getCost() << endl;
cout << "Item Total Cost: " << " " <<"$" << itemTwo.getTotalCost() << endl;
return 0;
}
Inventory::Inventory() //Default Constructor
{
itemNumber = 0;
quanity = 0;
cost = 0;
}
Inventory::Inventory(int i, int q, double c) //Constructor #2
{
itemNumber = i;
quanity = q;
cost = c;
}
void Inventory::setItemNumber(int i) //Re-Evaluates For a Negative Number and
{ //Passes User's Entered Data to the Private class
if (i >= 0)
itemNumber = i;
else
itemNumber = 0;
}
void Inventory::setQuanity(int q) //Re-Evaluates For a Negative Number and
{ //Passes User's Entered Data to the Private class
if (q >= 0)
quanity = q;
else
quanity = 0;
}
void Inventory::setCost(double c) ////Re-Evaluates For a Negative Number and
{ //Passes User's Entered Data to the Private class
if (c >= 0)
cost = c;
else
cost = 0;
}
int Inventory::getItemNumber() const //All 'Getter' Functions to Display Object's Data
{ //Whilst Keeping Data Integrity With 'const'
return itemNumber;
}
int Inventory::getQuanity() const
{
return quanity;
}
double Inventory::getCost() const
{
return cost;
}
double Inventory::getTotalCost() const //Calculates Total Cost and Returns
{
return cost * quanity;
}
/*
-----------------------------------------------------
Please Enter Data For Item One
Enter item number: 1303
Enter quanity: 10
Enter price: 2.50
Please Enter Data For Item Two
Enter item number: 5676
Enter quanity: 54
Enter price: 5.65
Here's the data you entered for item one:
Item Number: 1303
Item Quanity: 10
Item Cost: $2.50
Item Total Cost: $25.00
Here's the data you entered for item two:
Item Number: 5676
Item Quanity: 54
Item Cost: $5.65
Item Total Cost: $305.10
-----------------------------------------------------
Please Enter Data For Item One
Enter item number: 7456
Enter quanity: 10
Enter price: 10.9
Please Enter Data For Item Two
Enter item number: 0293
Enter quanity: 5
Enter price: 109.23
Here's the data you entered for item one:
Item Number: 7456
Item Quanity: 10
Item Cost: $10.90
Item Total Cost: $109.00
Here's the data you entered for item two:
Item Number: 293
Item Quanity: 5
Item Cost: $109.23
Item Total Cost: $546.15
-----------------------------------------------------
*/
答案 0 :(得分:0)
您的教授可能意味着您应该在构造函数中调用setter函数,而不是直接设置值:
Inventory::Inventory(int i, int q, double c) //Constructor #2
{
setItemNumber(i);
setQuantity(q);
setCost(c);
}
答案 1 :(得分:0)
她是对的。您正在接受用户输入,因此您必须使用您的setter进行验证,您出于某种原因创建了它们。
Inventory::Inventory(int i, int q, double c) //Constructor #2
{
setItemNumber(i);
setQuantity(q);
setCost(c);
}