2016-09-28 6 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

나는 이것이 어떻게 진행되고 있는지 생각했다. 나는 그것이 생성자의 의도에 대한 나의 이해라는 것입니다. 하나는 배열과 모든 값을 0으로 초기화 한 다음 다른 배열을 생성자로 호출하여 데이터가있는 동일한 배열을 채우려는 것입니다.이 배열은 분명히 그들이하는 일이 아닙니다. 이 시나리오에서는 사용자에게 2 가지 옵션을 제공해야합니다. 빈 배열 객체를 만들거나 배열을 만들고 채우시겠습니까? 그게 더 중요한 시점인가? – Zardoz

0

값을 변경해야합니다. 코드에 작은 결함이 있습니다.

1) - 개체를 만드는 중 실수를하고 있습니다. 기본 생성자로 생성 된 개체로 Show 메서드를 호출하고 있습니다.

inventory inv1; 

.... 
inv1.Show(); 

변경 inv1inventory inv1(a, b, c);에 다음 쇼 메서드를 호출 호출의 선언.

또한 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을 사용할 수도 있습니다.