object oriented programming

I have problem in the pointer aggregation, if we aggregate an array in a class to another class, how should we initialise them using a constructor? and also I have a switch case, but when I insert the option, it run but does not break even I had included the word break.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class Item
{
	public:
		string code;
		string name;
		int quantity;
		double totPrice;
		double itemPrice;
		Date date;
		Time time;
		
	public:
		Item(string code="", int quantity=0)
		{
			this -> code = code;
			this -> quantity = quantity;
		}
		
		void setPrice(double price)
		{
			itemPrice = price;
		}
		
		double getPrice()
		{
			return itemPrice;
		}
		
		void read()
		{
			cout << "Item's code': ";
			getline(cin, code); 
			cout << "Quantity : ";
			cin >> quantity; 
			date.readDate(); 
			time.readTime(); 
		}		
		
		void getDateTime()
		{
			date.dispDate();
			cout << ", ";
			time.dispTime();
		}
		
		void displayDetails()
		{
			cout << left
				 << setw(25) << code 
				 << setw(7)  << name
				 << setw(25) << itemPrice
				 << setw(9) << quantity; 
		}
		
		int getQuantity()
		{
			return quantity;
		}
		
		void setQuantity(int quantity)
		{
			this->quantity = quantity;
		}
};

class Customer
{
	private:
		Status status;
		string name, phone, IC;
		Item *itemList[];
		int numItem;
		char paymentMethod;
		Payment payment;
		string payType;
	
	public:
		Customer(string name="", string phone="", string IC="")
		{
			this->name = name;
			this->phone = phone;
			this->IC = IC; 
			numItem = 0;
		}
		
		string getName()
		{
			return name;
		}
		
		string getPhone()
		{
			return phone;
		}
		
		string getIC()
		{
			return IC;
		}
		
		void setName(string name)
		{
			this->name = name;
		}
		
		void setPhone(string phone)
		{
			this->phone = phone;
		}
		
		void setIC(string IC)
		{
			this->IC = IC;
		}
		
		bool getStatus(Status s)
		{
			bool status;
			
			if ((s.getAge()<=12) || (s.getAge()>=65) || (s.getMask() == false) || (s.getTemp()>=37.5))
			{
				status = false;
			}
			else
			{
				status = true;
			}
			
			return status;
		}
		
		void addItem(Item *i)
		{
			itemList[numItem] = i; 
		}
		
		void displayTotal(Item data[])
		{
			cout << "CODE\tITEM NAME\t\tCOST(RM)\tQUATITY\t\tTOTAL(RM)\n";
			
			for(int i=0; i<15; i++)
			{
				cout << data[i].code << "\t" << data[i].name << "\t\t" << fixed << setprecision(2) << data[i].itemPrice
					 << "\t\t";
				cout << "\t\t" << data[i].quantity
				 	 << "\t\t" << fixed << setprecision(2) << data[i].totPrice << endl;;
			}
		}
		
		void displayInfoCust()
		{
			cout << left;
			cout << "Name	:" << name  << endl 
				 << "Phone	:" << phone << endl 
				 << "IC		:" << IC << endl;
		}
		
		void displayInfoItem()
		{
			double price, totalPrice = 0;

			if (numItem == 0)
				cout << "\nNo item!" << endl;
			else
			{
				cout << "\nNumber of Item: " << numItem << endl;

				cout << left;
				cout << setw(4)  << "No"
					 << setw(25) << "Code"
					 << setw(7)  << "Name"
					 << setw(25) << "Price(RM)"
					 << setw(9)  << "Quantity" << endl;

				for (int i = 0; i < numItem; i++) 
				{
					cout << (i+1) << ".  "; 
					itemList[i]->displayDetails();
					itemList[i]->getQuantity(); 
					totalPrice += itemList[i]->getPrice(); 
					cout << itemList[i]->getPrice() << endl; 
				}

				cout << "\nTotal price = RM" << totalPrice
					 << endl << endl;
			}
		}
		
		char getPaymentMethod()
		{
			return paymentMethod;
		}
		
		void displayPaymentMethod()
		{
			if(paymentMethod == 'A')
			{
				payType = "cash";
			}
			else if(paymentMethod == 'B')
			{
				payType = "credit card";
			}
			else
			{
				payType = "debit card";
			}
			cout << "You will use " << payType << " to pay your bill." << endl;
		}
};

int main()
{
	Customer custObj; //Customer object
	Customer custList[SIZE]; 
	Item data[15];
	Status S;
	Item *itemPtr; //Item pointer

for(int i=0; i<numItem; i++)
					{
						//array aggregation use constructor how to do?
						data[i].Item(choice3, quantity);
						itemPtr = &data[i];
					}
return 0;
}
You can't use a constructor at that point since the object has already been constructed. Objects in arrays are default constructed when the array is created. So if you use an array then you need to modify the value of the object; you can't "construct" it a second time.

Alternatively, you could use a vector instead of an array.

1
2
3
vector<Item> data;
...
data.emplace_back(choice3, quantity);

Your "array" isn't quote what you think. It's a "pointer to" a "pointer to Item". The first "pointer" is just that, but thru the convenience of pointer arithmetic, you can store and access and array with it. You haven't stored an array, so there's no array to access.

If you were to implement item correctly, your Customer implementation would lots of "non-Customer" things, like memory allocations/releases, linked list management of a "node" ...

In C++, those data structures exist in the Standard Template Library, and we can just use them, as dutch did above.

std::vector is a resizeable array, with optional bounds checking. You should just that.
Last edited on
thank you! I havent learnt vector yet, but it works!
but it works!
I doubt it. It doesn't crash because you forget to increment numItem in:
1
2
3
4
void addItem(Item *i)
{
	itemList[numItem] = i; 
}

But you don't initialize itemList. That code overwrites whatever itemList happens to be pointing to. You never allocate space for the Item.

So you probably what to fix that stuff.
Topic archived. No new replies allowed.