C++ C++;OOP类和构造函数

C++ C++;OOP类和构造函数,c++,class,oop,constructor,C++,Class,Oop,Constructor,设计一个库存类,该类可以保存零售商店库存中某个商品的信息。 所需的私有成员变量: -项目编号 -数量 -成本 所需的公共成员职能 默认构造函数-将所有成员变量设置为0 构造函数#2-接受项目的编号、数量和成本作为参数。调用其他类函数将这些值复制到相应的成员变量中 他们认为我处理这件事的方式略有不同。我尝试初始化一个数组并存储用户输入的所有值,而不是1个值。然而,一旦用户退出成员/类函数,该值就会从数组中删除 我有点不知所措,所以任何信息或建议都会大有帮助 #include <iostrea

设计一个库存类,该类可以保存零售商店库存中某个商品的信息。 所需的私有成员变量: -项目编号 -数量 -成本 所需的公共成员职能

默认构造函数-将所有成员变量设置为0 构造函数#2-接受项目的编号、数量和成本作为参数。调用其他类函数将这些值复制到相应的成员变量中

他们认为我处理这件事的方式略有不同。我尝试初始化一个数组并存储用户输入的所有值,而不是1个值。然而,一旦用户退出成员/类函数,该值就会从数组中删除

我有点不知所措,所以任何信息或建议都会大有帮助

#include <iostream>
using namespace std;

class inventory
{
    private:
        int productNum[10];
        int productCount[10];
        double productPrice[10];
        int inventoryFillLevel;
        int userPNumber;
        int userQuantity;
        double userPrice;
    public:
        inventory()
        {
            int counter = 0;
            userPNumber = 0;
            userQuantity = 0;
            userPrice = 0;
            while (counter < 10)
            {
                productNum[counter] = 5;
                productCount[counter] = 6;
                productPrice[counter] = 7;
                counter++;
            }
        }
        inventory(int pNumber, int pCount, int pPrice)
        {
            cout << "Now we're in the 2nd constructor in the Class" << endl;
            cout << "The 1st number entered by the user is: " << pNumber << endl;
            cout << "The 2nd number entered by the user is: " << pCount << endl;
            cout << "The 3rd number entered by the user is: " << pPrice << endl;
            Input(pNumber);
        }
        void Input(int pNumber)
        {
            int counter = 0;
            cout << "\nNow we're in the function as called by the Constructor." << endl;
            cout << "The 1st number entered by the user is: " << pNumber << endl;
            cout << "In the function the counter is: " << counter << endl;
            cout << "The value in the array at " << counter << " is: " << productNum[counter] << endl;
            cout << "Now we set that to the value entered by the user" << endl;
            productNum[counter] = pNumber;
            cout << "And now the value in the array is: " << productNum[counter] << endl;
        }
         void Show()
        {
            int counter = 0;
            cout << "After entering the value, let's check what is stored in the array: ";
            cout << productNum[counter] << endl;
        }
};

 int main()
{

    int a=0;
    int b=0;
    int c=0;

    inventory inv1;

    cout << "1st User entered value" << endl;
    cin >> a;
    cout << "2nd User entered value" << endl;
    cin >> b;
    cout << "3rd User entered value" << endl;
    cin >> c;

    cout << "Now we call the 2nd constructor and pass the values to it" << endl;
    inventory(a, b, c);

    inv1.Show();    

    return 0;

}
#包括
使用名称空间std;
班级清单
{
私人:
int productNum[10];
int productCount[10];
双倍产品价格[10];
国际库存水平;
int userPNumber;
int用户数量;
双用户价格;
公众:
存货()
{
int计数器=0;
userPNumber=0;
用户数量=0;
userPrice=0;
while(计数器<10)
{
productNum[计数器]=5;
productCount[计数器]=6;
产品价格[柜台]=7;
计数器++;
}
}
库存(整数、整数、整数)
{

难道你似乎在错误地处理你的课程吗

inventory(a, b, c);
仅创建
inventory
的临时实例,该实例在行完成执行后基本消失。因此,当调用
inv1.Show()时
声明
inv1
时,它仍在使用默认构造函数中指定的值。您应该删除
inv
的当前声明并更改

inventory(a, b, c);


值未被删除。代码中存在小缺陷

1) -创建对象时出错。使用默认构造函数创建的对象调用
Show
方法

inventory inv1;

....
inv1.Show();
inv1
的声明更改为
库存inv1(a、b、c);
并调用然后调用show方法

另外,
inventory(a、b、c);
将创建新对象,并且不会影响
inv1

2) -您总是在成员数组的索引
0
处存储值。 当您在调用任何方法/构造函数时声明
int counter=0

inventory inv1;

....
inv1.Show();
使
int计数器;
成为类成员

class inventory
{
    private:
        int counter;
    public:
       inventory():counter(0){....}

    ....
};
它将计算您已经推到库存中的项目。 尽管你必须注意你已经投入了多少物品。
另一种方法是,你可以使用
std::vector
而不是
int-array

我认为这就是发生的事情。我认为归根结底,这是我对构造函数意图的理解。我希望让一个构造函数用0初始化一个数组和所有值,然后调用另一个构造函数来填充同一个数组ith数据,这显然不是他们所做的。在这种情况下,他们应该被视为给用户2个选项。要么创建一个空数组对象,要么创建并填充一个数组?这是重点吗?