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