ここではC++のプログラミングやプログラミングにかなり興味がありますので、私の解説がうまくいかないかもしれないことをご理解ください。 OOPクラスの割り当てでは、次のものが必要です。C++ OOPクラスとコンストラクタ
小売店の在庫内のアイテムの情報を保持できるインベントリクラスを設計します。 必要なプライベートメンバ変数: - アイテム数 - 数量 - 必要なパブリックメンバ関数を要し
デフォルトのコンストラクタは - 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;
}
もう一度お手数をおかけします。
私はそれが何が起こっているのだと思った。私は、コンストラクターの意図を理解していると思います。私は1つの配列を初期化し、すべての値を0にしてから、別のものをコンストラクタに呼び出して同じ配列にデータを埋め込むようにしています。このシナリオでは、ユーザーに2つのオプションを与えると見なされます。空の配列オブジェクトを作成するか、配列を作成して塗りつぶしますか?それはもっと重要なことですか? – Zardoz