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;
};
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 >
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;
Write a copy constructor for your grocery bag.
Write in words what is the difference between your copy constructor and one that the compiler would generate by default.
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.
write in words the four criteria of designing the recursion function and how your function satisfies them.
Rewrite the class definition of Node in question 1 such that it could store any data types.
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;
}
Draw the box trace for the above call (n=5).
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;
}
How many times the copy constructor is called when test1 method is executed?