1. Interface Design

Write the header file for Grocery ADT bag (GroceryBag with the minimum 4 core methods), allowing for the node items to be added, removed, and the bag to be searched etc. You do not need to implement these methods. Assume each item of the bag is a type Node as displayed below.

class Node {
public:
explicit Node(string name, double price) : name{name}, price{price}, next{nullptr} {}

private:
string name;
double price;
Node *next;
};

2.

Given the above definition for Node, a Node object should be displayed as < N:name, $ price >. Write the necessary operator to display a Node object.

// Node n("banana",1.25);
// cout << n << endl;
// Output: < N:Banana, $1.25 >

3. operator +=

Write operator+= for the GroceryBag class such that when two grocery bags are added together, all objects are added into the first bag. You may assume class Node has required getters (getNext(), getItemName(), getItemPrice()).

Example:
GroceryBag gb1; GroceryBag gb2;
gb1+=gb2;

4. Copy Constructor

Write a copy constructor for your grocery bag.

4.1.

Write in words what is the difference between your copy constructor and one that the compiler would generate by default.

5. Recursion

Write a recursive implementation for method getSum() of class GroceryBag, which returns the sum of the total value (price) of all the items in a grocery bag. Your method can take all the required arguments that is necessary.

6.

write in words the four criteria of designing the recursion function and how your function satisfies them.

7. Templates

Rewrite the class definition of Node in question 1 such that it could store any data types.

8.

What would be the printed output when the following code is called with n=5.

int collatz(int n) {
cout << "N: " << n << endl;
int next;
int (n == 1) {
cout << "done!" << endl;
return n;
}
if(n % 2 == 0) {
next = collatz(n / 2);
} else {
next = collatz(3 * n + 1);
}
cout << "N:" << n << " --> Next: " << next << endl;
return next;
}

8.1

Draw the box trace for the above call (n=5).

9.

Given the NodeTest1 Class below:

class NodeTest1 {
public:
static int NodeCount;
int value;
NodeTest1 *Next;

explicit NodeTest1(int Value = 0, NodeTest1 *Next = nullptr):
Value{Value}, Next{Next} {
++NodeCount;
}
~NodeTest1() {
--NodeCount;
}
};

Indicate which lines will generate a compile-time error with a "C" and which will generate a run-time error with an "R" for the method test1. You do not need to fix the code. Mark the line that will generate an error and write "C" or "R" next to it.

NodeTest1 test1(NodeTest1*& aNode) {
NodeTest1 N1(1);
NodeTest1 *N1Ptr;
N1Ptr = N1;
aNode=&N1Ptr;
assert(N1Ptr == nullptr);
auto *N2Ptr = new NodeTest1(3);
auto *N3Arr = new NodeTest1[10];
for(int I = 0; I < 5; ++I) {
NodeTest1 Tmp(I * I);
N3Arr[I] = Tmp;
}
delete N1;
return aNode;
}

10.

How many times the copy constructor is called when test1 method is executed?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.