aggregation relation

Why the case 3 cannot display?

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
class Item
{
	public:
		string itemName;
		double totPrice;
		double itemPrice;
		int quantity;
		
		//Item(){}
		Item (string itemName = "", double itemPrice = 0, int quantity = 0)
		{
			this->itemName = itemName;
			this->itemPrice = itemPrice;
			this->totPrice = 0;
			this->quantity = 0;
		}
		
		string getName()
		{
			return itemName;
		}
		
		void setName(string name)
		{
			itemName = name;
		}
		
		void setPrice(double price)
		{
			itemPrice = price;
		}
		
		//below are added function
		double getPrice()
		{
			return itemPrice;
		}
		
		void displayDetails()
		{
			cout << left
				 << setw(7)  << itemName
				 << setw(25) << itemPrice
				 << setw(9) << quantity; 
		}
		
		int getQuantity()
		{
			return quantity;
		}
		
		void setQuantity(int quantity)
		{
			this->quantity = quantity;
		}
		
};

class Customer
{
	private:
		Date date; //composition
		Payment P; //composition
		Status status;//composition
		string name, phone, IC, payType;
		int numItem;
		string paymentMethod;
		bool item[SIZE];
		Item *itemList[SIZE]; //aggregation
		double totPrice, overallPrice;

	public:
		Customer()
		{
			numItem = 0;
			for (int i = 0; i < SIZE; i++)
				item[i] = true;
		}
		
		void set(string name="", string phone="", string IC="")
		{
			this->name = name;
			this->phone = phone;
			this->IC = IC; 
			date.readDate();
			cout << "\n";
		}
			
		void setNum(int ind)
		{
			numItem = ind;
		}
		
		string getName()
		{
			return name;
		}
		
		string getPhone()
		{
			return phone;
		}
		
		string getIC()
		{
			return IC;
		}

		void addItem(Item *i)
		{
			itemList[numItem] = i;
			numItem++; 
		}
		
		//add
		bool getStatus(int age, bool mask, double temp)
		{
			bool status;
			
			if ((age<=12) || (age>=65) || (mask == false) || (temp>=37.5))
			{
				status = false;
			}
			else
			{
				status = true;
			}
			
			return status;
		}
		
		void setStatus(Status S)
		{
			status = S;
		}
	
		//add
		void displayTotal(Item data[])
		{
			cout << "ITEM NAME\tCOST(RM)\tQUATITY\t\tTOTAL(RM)\n";
			
			for(int i=0; i<15; i++)
			{
				totPrice = data[i].itemPrice * data[i].quantity;
				overallPrice += totPrice;
				cout << data[i].itemName << "\t\t" << fixed << setprecision(2) << data[i].itemPrice
					 << "\t\t";
				cout << data[i].quantity
				 	 << "\t\t" << fixed << setprecision(2) << totPrice << endl;
			}
			cout << "Overall Price: " << overallPrice << endl;
		}
		
		//add
		void displayInfoCust()
		{
			cout << left;
			cout << "\nName\t:" << name 
				 << "\nPhone\t:" << phone
				 << "\nIC\t:" << IC << endl;
		}
		
		//add
		void displayInfoItem(Item data[])
		{
			double totalPrice = 0;
			cout << "ITEM NAME\tPRICE(RM)\tQUATITY\t\tTOTAL(RM)\n";
			if (numItem == 0)
				cout << "\nNo item!" << endl;
			else
			{
				
				cout << "\nNumber of Item: " << numItem << endl;

				cout << left;
				cout << setw(4)  << "No"
					 << 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(); 
					itemList[i]->getPrice(); 
				 	totalPrice+=itemList[i]->getPrice();
					cout << endl; 
				}

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

int main()
{
	//Local variables and instances
	char ch, next;
	double temp;
	int choice1; //Task chosen
	int choice2; //Movie chosen
	bool mask, status, qstatus;
	string name, phone, IC, payType; 
	int m, quantity, age, numItem=0, N;
	time_t now = time(0);
	char* dt = ctime(&now); //for automatic read time , can replace in class time,class date can do also
	double itemPrice =0;
	string itemName;

	Status S;
	Payment *P;
	Customer custObj; //Cust object
	Item data[15], *itemPtr; 
	
	ifstream infile("input.txt");
	
 	if (infile.fail())
   		cout << "Sorry, input file not exist!" << endl;
//  	exit(1);
   	else
 	{
   		while (!infile.eof( ))      //if not at end of file, continue reading input data
		{
			for(int i=0; i<15; i++)
			{
				infile >> data[i].itemName >> data[i].itemPrice;

			}
		}
 	}
	infile.close();
	
	while(next == 'Y' || 'y')// next customer loop
	{	
		cout << "<<< Add Health Info >>>\n";
		cout << "Enter your age: ";
		cin >> age;
		cout << "Enter your body temperature: ";
		cin >> temp;
		try{
	 	      	if( temp >= 36.10 && temp <= 37.40)
		   		{
	            	cout << "Normal temperature." << endl;
	            }
		        else
				{
	            	throw(temp);
				}
          	}
	    catch(...)
		{
        	cout << "Invalid entry for temperature." << endl;
        	exit(1);
        }
		cout << "Do you wear your mask (1 for yes or 0 for no): ";
		cin >> m;
				
		if (m==1)
		{
			mask = true;
		}
		else
		{
			mask = false;
		}
				
		S.setAge(age), S.setMask(mask), S.setTemp(temp);
			
		status = custObj.getStatus(age, mask, temp);
				
		if(status == true)
		{
			cout << "You are allowed to go in the market.\n";
		}
				else
				{
					cout << "You are not allowed to go in the market!\n";
					exit(0);
				}
				cout << "\nEnter your name: ";
					cin >> name;
					cout << "Enter your IC: ";
					cin >> IC;
					cout << "Enter your phone number: ";
					cin >> phone;
					cout << "\n";
					custObj.set(name, phone, IC);
	//Enter the task chosen 
	choice1 = menu(); 

	//The process will be repeated if user enter the value in range 1 to 5

	while (choice1 > 0 && choice1 < 6)
	{
		switch (choice1)
		{		
			case 1:
				
				{
				cout << "\n<<< Display Item's List >>>\n";
				cout << "NO\tITEM NAME\tCOST(RM)\t\n";
	
				for(int i=0; i<15; i++)
				{
					cout << i+1 << "\t";
					cout << data[i].getName() << "\t\t" << fixed << setprecision(2) << data[i].getPrice() << "\n";
				}
				cout << "\n";
				}break;
				
			case 2:
			
			do
			{
				cout << "\n<<< Add Item into Cart >>>" << endl;
				cout << "\nEnter item's number: ";
				cin >> choice2; //To enter the item chosen
				numItem++;
				cin.ignore();
				
				while((choice2 >= 16) || (choice2 <= 0))
				{
					cout << "Invalid choice! Please try again!\n";
					cin.clear();
					cin >> choice2;
					data[choice2-1].setName(itemName);
					data[choice2-1].setPrice(itemPrice);
				}
				
				//To enter the details of item
				cout << "Quantity: ";
				cin >> quantity;
				
				cout << "How many number of this item in shopping cart? (Include quantity enter just now): ";
				cin >> N;
				
				quantity = N;
				
				if (quantity < 6)
				{
					qstatus = true;
					data[choice2-1].setQuantity(quantity);
					custObj.addItem(data);
				}
				else
				{
					qstatus = false;
					cout << "You have reached the limit num of items, please enter again!" << endl;
					cin.clear();
					cin >> quantity;
					while (quantity < 6)
					{
						qstatus = true;
						data[choice2-1].setQuantity(quantity);
						custObj.addItem(data);
					}
				}
					
	
				cin.ignore();
				
				for(int i=0; i<numItem; i++)
				{
					data[choice2-1].setQuantity(quantity);
					custObj.addItem(data);	
					custObj.setNum(numItem);	
					itemPtr = &data[choice2-1];
				}
				
				cout << "\nPress 'Y' to continue >> ";
				cin >> ch; //To enter a value to continue to add an audience
				cin.ignore();
				ch = toupper(ch);
			} while (ch == 'Y'); //The process will be continued if user enter the valid input
			break;

			case 3:
				{
					custObj.displayInfoItem(data);
				}break;
}
Why the case 3 cannot display?
It depends on the content of custObj. The data you pass to displayInfoItem(...) is not used in that function.
You've still not fixed Customer::itemList.

I've explained in other threads that what you have is wrong and why and how to fix it.

You code that isn't working is using it.
my lecturer does not allow me to use the vector, is it have any other way to solve this? as I had replaced the customer array to a customer object.
I had tried out case 5 also, the payment display has a problem, but I cannot find out why it keeps displaying the statement for cash although I input credit card option.
my lecturer does not allow me to use the vector


What - have they removed std::vector from the stl? How else have they not allowed you to use vector?

If they only don't want you to use std::vector, then you're being short changed on your course....
Last edited on
because we haven't learn vector yet...
Well, you'll have to implement your own container. Are you allowed to use templates?
no, but I had solved this code. thank you so much!
This:
1
2
3
4
5
6
class Customer
{
	private:
		int numItem;
		bool item[SIZE];
		Item *itemList[SIZE]; //aggregation 

Should be:
1
2
3
4
5
6
class Customer
{
	private:
		int numItem;
		bool item[SIZE];
		Item itemList[SIZE]; // remove the pointer 
I have question on the payment, I cant compile my code because there is an vtable error, I never seen this error before, how can I solve this?
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//----------------------------------------------------
class Payment
{
	protected: 
		double amount;
		string paymentMethod;
	
	public:
		virtual void display();	//polymorphism
		Payment(){}	
		
		string getPaymentMethod()
		{
			return paymentMethod;
		}
		
		void setPaymentMethod(string paymentMethod)
		{
			this-> paymentMethod = paymentMethod; 
		}
};

//----------------------------------------------------
class Cash : public Payment	//inheritance
{
	public: 
		void display()
		{
			cout << "";
		}
};

//----------------------------------------------------
class CreditCard : public Payment	//inheritance
{
	protected:
		double cardNumber, dateExpiry;
		
	public:
		void display()
		{
			//check validity
			cout << "Enter your card number: ";
			cin >> cardNumber;
			
			cout << "Enter card's date expiry: ";
			cin >> dateExpiry;
		}
};

//----------------------------------------------------
class BankIn : public Payment 	//inheritance
{
	protected:
		string bankName;
		string username;
		double accountNo;
	
	public: 
		void display()
		{
			//check validity
			cout << "Enter your bank account name: ";
			cin >> bankName;
			
			cout << "Enter your username: " ;
			cin >> username;
			
			cout << "Enter your account number: ";
			cin >> accountNo;
		}
};

class Customer
{
	private:
		Date date; //composition
		Payment payment; //composition
		Status status;//composition
		Item *itemList[SIZE]; //aggregation
		string name, phone, IC, payType;
		int numItem;
		bool item[SIZE];
		double price, totalPrice, totPrice, overallPrice;

	public:
		Customer()
		{
			numItem = 0;
			for (int i = 0; i < SIZE; i++)
				item[i] = true;
		}
		
		void set(string name="", string phone="", string IC="")
		{
			this->name = name;
			this->phone = phone;
			this->IC = IC; 
			date.readDate();
			cout << "\n";
		}

		bool getItem(int ind)
		{
			return item[ind];
		}

		void setItem(int ind)
		{
			if (ind>6)
			{
				item[ind] = false;
			};
		}
			
		string getName()
		{
			return name;
		}
		
		string getPhone()
		{
			return phone;
		}
		
		string getIC()
		{
			return IC;
		}

		void addItem(Item *i)
		{
			itemList[numItem] = i;
			numItem++; 
		}
		
		bool getStatus(int age, bool mask, double temp)
		{
			bool status;
			
			if ((age<=12) || (age>=65) || (mask == false) || (temp>=37.5))
			{
				status = false;
			}
			else
			{
				status = true;
			}
			
			return status;
		}
		
		void setStatus(Status S)
		{
			status = S;
		}
	
		void displayTotal(Item data[])
		{
			cout << "ITEM NAME\tCOST(RM)\tQUANTITY\tTOTAL(RM)\n";
			
			for(int i=0; i<15; i++)
			{
				totPrice = data[i].itemPrice * data[i].quantity;
				overallPrice += totPrice;
				cout << data[i].itemName << "\t\t" << fixed << setprecision(2) << data[i].itemPrice
					 << "\t\t";
				cout << data[i].quantity
				 	 << "\t\t" << fixed << setprecision(2) << totPrice << endl;
			}
			cout << "Overall Price: " << overallPrice << endl;
		}
		
		void displayInfoCust()
		{
			cout << left;
			cout << "\nName\t:" << name 
				 << "\nPhone\t:" << phone
				 << "\nIC\t:" << IC << endl;
		}
		
		void displayInfoItem(Item d[])
		{
			totalPrice = 0; 
			cout << "ITEM NAME\tCOST(RM)\tQUANTITY\tTOTAL\n";
			for(int i=0; i<15; i++)
			{
				if(!(d[i].quantity == 0))
				{
					price = d[i].itemPrice * d[i].quantity;
					totalPrice += price;
					cout << d[i].itemName << "\t\t" << fixed << setprecision(2) << d[i].itemPrice << "\t\t";
					cout << d[i].quantity << "\t\t" << fixed << setprecision(2) << price << endl;
				}
			}
			cout << "\nTotal price = RM" << totalPrice
				 << endl << endl;
		}
		
		void displayPaymentMethod()
		{
			string paymentMethod;
			paymentMethod = payment.getPaymentMethod();
			if (paymentMethod == "A" || paymentMethod=="a")
			{
				payType = "cash";
			}
			else if (paymentMethod == "B" || paymentMethod=="b")
			{
				payType = "credit card";
			}
			else if(paymentMethod=="C"||paymentMethod=="c")
			{
				payType = "debit card";
			}
			cout << "You will use " << payType << " to pay your bill." << endl;
		}
};

int main()
{
	Payment *P, pay;
case 5:
				{
					cout << "\nYour info: \nName\t:" << custObj.getName()
				   		 << "\nIC\t:" << custObj.getIC()
						 << "\nPhone\t:" << custObj.getPhone() << "\n";
					cout << "\n" << dt;
					custObj.addItem(data);
					custObj.displayTotal(data);
					
					cout << "\nSelect the alphabet of payment method that you wish to proceed. (A. Cash, B. Credit Card, C. Online Transfer): " ;
					cin >> payType;
					pay.setPaymentMethod(payType);
					if (payType == "A" || payType =="a") //To identify the payment category
					{
						P = new Cash();
					}
					else if (payType == "B" || payType =="b")
					{
						P = new CreditCard();
					}
					else if(payType=="C"||payType=="c")
					{
						P = new BankIn();
					}
					else
					{
					    cout << "Invalid Payment Method.";
					}
					
					custObj.displayPaymentMethod();
                	P->display();
					cout << "\nThank you, looking forward to your next visit!" << endl;
					exit(0);
				}
		} //End switch
		cout << endl;
		//Enter the task chosen 
		choice1 = menu(); 
	} //End while loop

	return 0;
}
If you have a vtable error, it usually means you didn't implement (define) a virtual function.

Payment::display() I assume is the function at fault.
If you want a pure virtual function, you need = 0 at the end.
virtual void display() = 0;

Also, if a base class is being used polymorphically, it should have a virtual destructor
virtual ~Payment() = default;

Third, you should get in the habit of using C++11 keywords like override to improve readability for others and provide some compile-time safety checks.
1
2
3
4
5
6
7
8
9
10
11
12
		void display() override
		{
			//check validity
			cout << "Enter your bank account name: ";
			cin >> bankName;
			
			cout << "Enter your username: " ;
			cin >> username;
			
			cout << "Enter your account number: ";
			cin >> accountNo;
		}


Fourth, having a "display" function that asks for user data is misleading. I would not expect a display function to mutate any data.
Last edited on
virtual ~Payment() = default;

I tried but still cannot...
Your message does not help me help you.
It shows 2 error
[code]
[Error] cannot declare field 'Customer::py' to be of abstract type 'Payment'
[Error] expected class-name before 'virtual'
[\code]
Well if you made Payment have a pure virtual function, you can't declare it by value within another class. (I didn't notice you were doing this before.) You would declare it as a pointer and point a non-abstract subclass to it.

If you want to be able to instantiate objects of type Payment directly, you need to implement the Payment::display function, instead of making it pure virtual.
Last edited on
Topic archived. No new replies allowed.