2016-09-28 5 views
-2

ここでは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

あなたのクラスを正しく処理していないようです。ライン

inventory(a, b, c); 

は線のみが実行を終了した後、実質的になくなってinventoryの一時的なインスタンスを作成します。だから、inv1.Show()と呼ぶときには、inv1が宣言されたときにデフォルトのコンストラクタで割り当てられた値を使用しています。あなたはinvの現在の宣言を削除し、削除取得されていません

inventory(a, b, c); 

inventory inv1(a, b, c); 
+0

私はそれが何が起こっているのだと思った。私は、コンストラクターの意図を理解していると思います。私は1つの配列を初期化し、すべての値を0にしてから、別のものをコンストラクタに呼び出して同じ配列にデータを埋め込むようにしています。このシナリオでは、ユーザーに2つのオプションを与えると見なされます。空の配列オブジェクトを作成するか、配列を作成して塗りつぶしますか?それはもっと重要なことですか? – Zardoz

0

値を変更する必要があります。コードに小さな欠陥があります。

1) - オブジェクトの作成中に間違っている。デフォルトのコンストラクタによって作成されたオブジェクトでShowメソッドを呼び出しています。

inventory inv1; 

.... 
inv1.Show(); 

変更inv1inventory inv1(a, b, c);への宣言およびshowメソッドを呼び出して呼び出します。

inventory(a, b, c);も新しいオブジェクトを作成し、inv1は無効になります。

2) - メンバー配列のインデックス0に常に値を格納しています。 メソッド/コンストラクタを呼び出すたびにint counter = 0と宣言しているので、

int counter ;をクラスメンバにします。

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

    .... 
}; 

あなたの在庫に既に押し込んでいるアイテムを数えます。 あなたが既に押し込んだアイテムの数に注意する必要がありますが、 int arrayの代わりにstd::vectorを使用することもできます。