Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Any comments are greatly appreciated Create a base class call Item and a

ID: 3713494 • Letter: I

Question

In C++

Any comments are greatly appreciated

Create a base class call Item and a derived class called Consumable. Create two subclasses of Consumable called Juice and Chili. Provide the Item class with 3 private variables called name, weight and value. Provide getter and setter methods for each of these variables. Provide constructors and destructors for each class. Each should display a message notifying the user it has been executed.

In main() Create an array of 10 Item pointers. Use a for loop to assign the first five elements an object from the Juice class. Do the same for the last 5 elements but from the Chili class. Make sure your objects are stored on the heap.

Use the setter methods to set the name of the first and last objects. Don’t worry about setting the values for the other objects. Create a global function called crazy() that receives 2 item pointers as parameters. Program this function to swap the names of the two items.

Call the function in main() and pass it the two named objects using pass by pointer. Display the names of the two objects after the swap.

Free up all of the memory that was used by the objects before ending your program.

Explanation / Answer

#include <iostream>
using namespace std;
class Item {
public:
void setName(string n) {
name = n;
}
void setValue(string v) {
value = v;
}
void setWeight(string w) {
weight = w;
}
Item();
~Item();
  
private:
string name;
string value;
string weight;
};

// Derived class
class Consumable: public Item {
public:
Consumable();
~Consumable();
  
};

class Juice: public Consumable {
public:
Juice();
~Juice();

};
class Chili: public Consumable {
public:
Chili();
~Chili();

};
Item::Item(void) {
cout << "Item Object is being created" << endl;
}
Item::~Item(void) {
cout << "Item Object is being deleted" << endl;
}
Chili::Chili(void) {
cout << "Chili Object is being created" << endl;
}
Chili::~Chili(void) {
cout << "Chili Object is being deleted" << endl;
}
Juice::Juice(void) {
cout << "Juice Object is being created" << endl;
}
Juice::~Juice(void) {
cout << "Juice Object is being deleted" << endl;
}

Consumable::Consumable(void) {
cout << "Consumable Object is being created" << endl;
}
Consumable::~Consumable(void) {
cout << "Consumable Object is being deleted" << endl;
}
int main(void) {
char *ptr[10];
Juice juice;
Chili chili;
  
for (int i = 0; i < 5; i++) {
ptr[i] = &juice[i];
}
for (int i = 5; i < 10; i++) {
ptr[i] = &chili[i];
}
ptr[0].setName("first");
ptr[9].setName("last");

crazy(&ptr[0],&ptr[9]);

delete ptr;   
ptr = NULL;

return 0;
}
void crazy(char *arr1, char *arr2) {
string temp;

temp=arr1;
arr1=arr2;
arr2=temp;
cout << "Arr1: " << arr1<< endl;
cout << "Arr2: " << arr2<< endl;

}