Question: a) b) c) d) Use the following program to answer the questions: 1. //--------------------------------------------------------------------------- 2. // CSC160 Test Three - TestThree.cpp 3. //--------------------------------------------------------------------------- 4. #include
a)
b)
c)
d)![{ 9. cout 10. ShoppingCart testCarts[3]; 11. string test1("Golf Clubs"), test2("Golf Bag");](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f52c4e7e676_69466f52c4e43494.jpg)
Use the following program to answer the questions:
1. //---------------------------------------------------------------------------
2. // CSC160 Test Three - TestThree.cpp
3. //---------------------------------------------------------------------------
4. #include
5. #include "ShoppingCart.h"
6. using namespace std;
7. int main()
8. {
9. cout
10. ShoppingCart testCarts[3];
11. string test1("Golf Clubs"), test2("Golf Bag");
12. try
13. {
14. testCarts[0].addToCart(test1);
15. testCarts[0].addToCart(test2);
16. testCarts[0].addToCart("Golfballs (12)");
17. testCarts[1].addToCart("CD-R (50)");
18. testCarts[1].addToCart("Printer Toner");
19. testCarts[2].addToCart(testCarts[0].getItem("Golf Clubs"));
20. testCarts[2].addToCart(testCarts[1].getItem(0));
21. testCarts[2].addToCart(testCarts[1].getItem(1));
22. cout
23. cout
24. cout
25. cout
26. }
27. catch(ShoppingCartException err)
28. {
29. cout
30. }
31. catch(...)
32. {
33. cout
34. }
35. return 0;
36. }
37. //---------------------------------------------------------------------------
38. // CSC160 Test Three - ShoppingCart.h
39. //---------------------------------------------------------------------------
40. using namespace std;
41. #ifndef CART_H
42. #define CART_H
43. #include
44. #include "ApplicationException.h"
45. class ShoppingCartException : public ApplicationException
46. {
47. public:
48. ShoppingCartException();
49. ShoppingCartException(string errorMsg);
50. ShoppingCartException(char errorMsg[]);
51. };
52. class ShoppingCart
53. {
54. public:
55. ShoppingCart();
56. void addToCart(string itemDesc) throw(ShoppingCartException);
57. void addToCart(char itemDesc[]) throw(ShoppingCartException);
58. string getItem(string itemDesc) throw(ShoppingCartException);
59. string getItem(char itemDesc[]) throw(ShoppingCartException);
60. string getItem(int itemNumber) throw(ShoppingCartException);
61. private:
62. string *cartItems;
63. int numItems;
64. int allocatedItems;
65. void increaseCartAllocation(int count);
66. };
67. #endif
68. //---------------------------------------------------------------------------
69. // CSC160 Test Three - ShoppingCart.cpp
70. //---------------------------------------------------------------------------
71. #include
72. #include "ShoppingCart.h"
73. using namespace std;
74. const int ALLOCATE_SIZE = 10;
75. // Implementation of the ShoppingCartException class
76. ShoppingCartException::ShoppingCartException(): ApplicationException()
77. { message = "Error Detected in Shopping Cart Class"; }
78. ShoppingCartException::ShoppingCartException(string errorMsg): ApplicationException()
79. { message = errorMsg; }
80. ShoppingCartException::ShoppingCartException(char errorMsg[]): ApplicationException()
81. { message = errorMsg; }
82. // Implementation of the ShoppingCart class
83. ShoppingCart::ShoppingCart()
84. {
85. cartItems = new string[ALLOCATE_SIZE];
86. numItems = 0;
87. allocatedItems = ALLOCATE_SIZE;
88. }
89. void ShoppingCart::addToCart(string item) throw(ShoppingCartException)
90. {
91. if (numItems == allocatedItems)
92. increaseCartAllocation(ALLOCATE_SIZE);
93. numItems++;
94. cartItems[numItems - 1] = item;
95. }
96. void ShoppingCart::addToCart(char item[]) throw(ShoppingCartException)
97. {
98. if (numItems == allocatedItems)
99. increaseCartAllocation(ALLOCATE_SIZE);
100. numItems++;
101. cartItems[numItems - 1] = item;
102. }
103. string ShoppingCart::getItem(string itemDesc) throw(ShoppingCartException)
104. {
105. for (int i = 0; i
106. if (cartItems[i] == itemDesc)
107. return cartItems[i];
108. throw ShoppingCartException("Item " + itemDesc + " is not in Shopping Cart");
109. }
110. string ShoppingCart::getItem(char itemDesc[]) throw(ShoppingCartException)
111. {
112. string temp = itemDesc;
113. for (int i = 0; i
114. if (cartItems[i] == temp)
115. return cartItems[i];
116. throw ShoppingCartException("Item is not in Shopping Cart");
117. }
118. string ShoppingCart::getItem(int itemNumber) throw(ShoppingCartException)
119. {
120. if (itemNumber > numItems - 1)
121. {
122. ShoppingCartException temp("Item number is too large");
123. throw temp;
124. }
125. else
126. return cartItems[itemNumber];
127. }
128. void ShoppingCart::increaseCartAllocation(int count)
129. {
130. allocatedItems += count;
131. string *temp = new string[allocatedItems];
132. for (int i = 0; i
133. temp[i] = cartItems[i];
134. cartItems = temp;
135. }
Suppose we want to add a destructor member function to the ShoppingCart class: What must the name of that function be? What line of code should be included in that function to return dynamically allocated memory to the memory heap
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
