C ++ OOP类和构造函数

时间:2016-09-28 03:01:08

标签: c++ class oop constructor

这里对C ++很陌生并且整体编程所以请耐心等待,并理解我的解释可能不是标记。我的OOP类的赋值需要以下内容:

设计一个库存类,可以保存零售商店库存中的商品信息。 必需的私有成员变量:  - 项目编号   - 数量   - 费用 必需的公共成员函数

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

他们这样做的方式略有不同。我尝试初始化一个数组并存储用户在那里输入的所有值,而不是一个值。但是看起来,一旦用户退出成员/类函数,就会从数组中删除该值。

我的智慧结束了,所以任何信息或建议都会有很大的帮助。

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

}

再次感谢您对此的任何帮助。

2 个答案:

答案 0 :(得分:1)

您似乎错误地处理了您的课程。这条线

inventory(a, b, c);

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

inventory(a, b, c);

inventory inv1(a, b, c);

答案 1 :(得分:0)

价值不会被删除。代码中有一个小缺陷。

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

inventory inv1;

....
inv1.Show();

inv1的声明更改为inventory inv1(a, b, c);,然后调用show show。

同样inventory(a, b, c);会创建新对象,但不会影响inv1

2) - 您始终将值存储在成员数组的索引0处。 正如您在调用任何方法/构造函数时声明int counter = 0一样。

int counter ;成为班级成员。

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

    ....
};

它会计算您已在库存中推送的商品。 虽然您必须注意已经推入了多少项目。 替代方法是您可以使用std::vector代替int array