简体   繁体   English

C++ OOP 类和构造函数

[英]C++ OOP Classes and Constructors

Pretty new to C++ here and programming as a whole so please be patient and understanding that my explanations may not be on the mark.在这里对 C++ 和整个编程都很陌生,所以请耐心等待并理解我的解释可能不正确。 The assignment for my OOP class calls for the following:我的 OOP 课程的作业要求如下:

Design an Inventory class that can hold information for an item in a retail store's inventory.设计一个 Inventory 类,该类可以保存零售店库存中的某个项目的信息。 Required private member variables: - item number - quantity - cost Required public member functions所需的私有成员变量: - 项目编号 - 数量 - 成本 所需的公共成员函数

Default constructor - Sets all the member variables to 0 Constructor #2 - Accepts an item's number, quantity and cost as arguments.默认构造函数 - 将所有成员变量设置为 0 构造函数 #2 - 接受项目的编号、数量和成本作为参数。 Calls other class functions to copy these values into the appropriate member variables.调用其他类函数将这些值复制到适当的成员变量中。

They way I'm going about this is slightly different.他们处理此事的方式略有不同。 Rather than 1 value I'm trying to initialize an array and store all the values enter by the user there.我试图初始化一个数组并将用户输入的所有值存储在那里,而不是 1 个值。 However it seems, once the user exits the member/class function the value is removed from the array.然而,一旦用户退出成员/类函数,该值就会从数组中删除。

Kind of at my wits end here so any info or recommendations would greatly help.我的智慧在这里结束,所以任何信息或建议都会有很大帮助。

#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;

}

Thanks again for any help with this.再次感谢您对此的任何帮助。

You appear to be handling your class incorrectly.您似乎在错误地处理您的课程。 The line线

inventory(a, b, c);

only creates a temporary instance of inventory that is gone essentially after the line finishes execution.仅创建一个临时inventory实例,该实例在生产线完成执行后基本消失。 So when you call inv1.Show() it's still using the values that are assigned in your default constructor when inv1 was declared.所以,当你调用inv1.Show()它仍然使用在你的默认构造函数时分配的值inv1被宣布。 You should remove the current declaration of inv and change您应该删除inv的当前声明并更改

inventory(a, b, c);

to

inventory inv1(a, b, c);

Value are not getting removed.价值没有被移除。 There is small flaw in the code.代码中有一个小缺陷。

1)- You are making mistake while creating an object. 1)- 您在创建对象时犯了错误。 You are calling Show method with object created by default constructor.您正在使用默认构造函数创建的对象调用Show方法。

inventory inv1;

....
inv1.Show();

Change the declaration of inv1 to inventory inv1(a, b, c);inv1的声明改为inventory inv1(a, b, c); and call then call show method.并调用然后调用 show 方法。

Also inventory(a, b, c);还有inventory(a, b, c); will create new object and will not effect inv1 .将创建新对象并且不会影响inv1

2)- You are always storing values at index 0 of the member array. 2)- 您始终将值存储在成员数组的索引0处。 As you are declaring int counter = 0 whenever you are calling any method/constructor.当您在调用任何方法/构造函数时声明int counter = 0时。

Make int counter ;制作int counter ; a class member.班级成员。

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

    ....
};

It will count the items you already push in your inventory.它将计算您已经放入库存的物品。 Though you will have to take care how many item you already pushed in. Alternative approach can be you can use std::vector instead of int array .虽然你必须注意你已经推入了多少项目。替代方法可以是你可以使用std::vector而不是int array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM