如何允许用户在对象和类中输入?

How to allow user input in objects and classes?

本文关键字:输入 对象 何允许 用户      更新时间:2023-10-16

请考虑以下代码:

#include <iostream>
using namespace std;
class inventory
{
public:
~inventory()
{
cout << "This Object is being destroyed" << endl;
}
inventory()
{
itemNumber = 0;
quantity= 0;
cost= 0;
}
inventory(int itemNumber1, int quantity1, double cost1)
{
setItemNumber(itemNumber1);
setQuantity(quantity1);
setCost(cost1);

}
void setItemNumber(int itemNumber2)
{
itemNumber=itemNumber2;
}
bool setQuantity(int quantity2)
{
bool userTrue = true;
bool userFalse = false;
if (quantity2 < 0)
{
quantity = 0;
return userFalse;
}
else
{
quantity= quantity2;
return userTrue;
}
}
bool setCost(double cost2)
{
bool userTrue = true;
bool userFalse = false;
if (cost2 < 0.0)
{
cost = 0.0;
return userFalse;
}
else
{
cost= cost2;
return userTrue;
}
}
double getTotalCost(int quantity, double cost)
{
int total;
total = (quantity * cost);
return total;
}
private:
int itemNumber;
int quantity;
double cost;
};
int main()
{
int itemNumberInput;
int quantityInput;
double costInput;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput;
cout << "Enter the Cost : " << endl;
cin >> costInput;

inventory *pointerA, *pointerB;
pointerA = new inventory;
pointerB = new inventory(inventory(itemNumberInput , quantityInput , costInput));
inventory firstObject(itemNumberInput,quantityInput,costInput);


int itemNumberInput1;
int quantityInput1;
double costInput1;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput1;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput1;
cout << "Enter the Cost : " << endl;
cin >> costInput1;
inventory secondObject(itemNumberInput1,quantityInput1,costInput1); // not sure if thats correct
cout << secondObject.setItemNumber(); // not working
cout << secondObject.setQuantity(); // not working
cout << secondObject.setCost(); // not working

return 0;
}

上面的代码应该接受三个用户输入,并将它们发送到类,类将完成它们的工作。

我目前被困在最后,它给了我一个错误。

在向用户请求值的第二个对象中,它应将这些值发送到类。

相反,我收到错误。

如何解决此问题?

这是固定代码:-

#include <iostream>
using namespace std;
class inventory
{
public:
~inventory()
{
cout << "This Object is being destroyed" << endl;
}
inventory()
{
itemNumber = 0;
quantity= 0;
cost= 0;
}
inventory(int itemNumber, int quantity, double cost)
{
this->itemNumber = itemNumber;
this->quantity = quantity;
this->cost = cost;

}
void setItemNumber(int itemNumber)
{
this->itemNumber=itemNumber;
}
bool setQuantity(int quantity)
{
bool userTrue = true;
bool userFalse = false;
if (quantity < 0)
{
this->quantity = 0;
return userFalse;
}
else
{
this->quantity= quantity;
return userTrue;
}
}
bool setCost(double cost)
{
bool userTrue = true;
bool userFalse = false;
if (cost < 0.0)
{
this->cost = 0.0;
return userFalse;
}
else
{
this->cost= cost;
return userTrue;
}
}
double getTotalCost(int quantity, double cost)
{
return quantity * cost;
}
private:
int itemNumber;
int quantity;
double cost;
};
int main()
{
int itemNumberInput;
int quantityInput;
double costInput;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput;
cout << "Enter the Cost : " << endl;
cin >> costInput;
inventory *pointerA, *pointerB;
pointerA = new inventory;
pointerB = new inventory(inventory(itemNumberInput , quantityInput , costInput));
inventory firstObject(itemNumberInput,quantityInput,costInput);
int itemNumberInput1;
int quantityInput1;
double costInput1;
cout << "Enter the Item Number: " << endl;
cin >> itemNumberInput1;
cout << "Enter the Quantity : " << endl;
cin >> quantityInput1;
cout << "Enter the Cost : " << endl;
cin >> costInput1;
// The below line is correct
// inventory secondObject(itemNumberInput1,quantityInput1,costInput1); 
//Alternatively
inventory secondObject;
secondObject.setItemNumber(itemNumberInput1); 
secondObject.setQuantity(quantityInput1); 
secondObject.setCost(costInput1); 
delete pointerA; // delete dynamically allocated memory to avoid memory leak
delete pointerB;
return 0;
}

好吧,你已经使用 3-arg 构造函数构造了 'secondObject' 对象,使用用户输入的值作为参数。因此,此对象的成员变量是通过构造函数设置的,并且实际上不需要使用"set"方法。在您的情况下,如果您想稍后更改值,则 set 方法将很有用。例如,假设用户输入 10、10 和 2.5 作为值。然后,使用构造函数使用这些值构造对象。唯一的区别是首先将这些值放入变量中。但它的工作方式相同。如果以后想更改数量值,可以secondObject.setQuantity(2);该对象的数量现在设置为 2。对 .set 的调用不起作用的原因是您需要将参数传递给这些方法,即要将其设置为的值。

对于正在打印的析构函数方法,当对象超出范围时,将销毁对象,以便释放内存。通常,在输出方面不会发生任何事情 - 对象将超出范围,编译器将释放内存并继续其业务。但是,您已经编写了一个自定义析构函数,该析构函数打印出"对象正在销毁",它位于主条目的末尾。很可能您的构造函数工作正常,我只是不确定您希望发生什么。我还建议您阅读C++中的内存泄漏,尤其是关于"新"关键字。