如何允许用户在对象和类中输入?
How to allow user input in objects and classes?
请考虑以下代码:
#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++中的内存泄漏,尤其是关于"新"关键字。
相关文章:
- 在构造函数中输入对象时C++类成员作用域
- 如何使用函数将一个对象的输入复制到另一个对象中
- 如何允许用户在对象和类中输入?
- 为什么复制构造函数不需要检查输入对象是否指向自身?
- C++ 通过输入创建对象
- 如何定义此"if block"中其他无效输入的值,以便在c ++中将字符串转换为对象?
- C++ 对象数组字符串输入在控制台上不起作用
- 删除函数环境中C++输入 R 对象
- 将私有对象变量与用户输入的变量进行比较
- 如何将用户输入从控制台传递到不同线程中的对象 -> QTcpsocket
- 流对象来表示文件输入,然后是标准输入?
- 使用用户的输入直接初始化对象的变量
- 有没有办法根据用户的输入访问类对象的信息?
- 是否可以保证输入<Type>实数时复杂对象的虚部设置为零?
- 使用非成员函数从输入流中提取类对象
- 执行输入检查并在C++中初始化对象
- 如何使用ISTream对象输入多个字符到对象的数组中?(and 0作为哨兵值.下面的我的代码将无效)
- 通过用户输入到对象的指针的C 向量
- 链接到动态文件和作为输入对象有什么区别?
- 创建一个复制构造函数,该构造函数对输入对象进行深度复制(使用数组)