Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 3 Classes (2.0) Overview The purpose of this assignment is give you some experience writing classes in C++, the various special functions they make

Lab 3 Classes (2.0) Overview The purpose of this assignment is give you some experience writing classes in C++, the various special functions they make use of (such as copy constructors, assignment operators, and destructors), as well as and introduction dynamically allocating memory within those classes. Description This program will represent a stereotypical college pantry, which consists of shelves that contai

//Include header files for the three classes

/Main should not be edited

Lab 3 Classes (2.0)

Overview

The purpose of this assignment is give you some experience writing classes in C++, the various special functions they make use of (such as copy constructors, assignment operators, and destructors), as well as and introduction dynamically allocating memory within those classes.

Description

This program will represent a stereotypical college pantry, which consists of shelves that contain various food items. To that end, there are three classes you will be writing:

  • Food
  • Shelf
  • Cabinet

For this assignment, main.cpp will be provided for you, so you dont have to worry about the structure of the program. Instead, you can focus solely on the structure of the classes and their interactions.

Food

The Food class is the basic container of this assignment. You will need to store the following data:

  • The type of food (such as cereal, fruit, vegetable, frozen etc)
  • The name of the food product (such as Fruity Pebbles, Ramen, Tomato, etc)
  • The expiration year
  • The price
  • How many calories does the food have?

In addition to a constructor which takes in the necessary information, you will also need the following functions defined. If you are using dynamic memory in your class, you will also want to define a destructor, in which you will clean up (delete) any memory which was allocated by the class.

Shelf

The Shelf class is a bit more sophisticated. Its purpose is to store an array of Food objects. Each Shelf that you create could have a different number of Food objects, so you will have to use dynamic memory allocation in this case. Your Shelf should contain variables for the following:

  • The name of the Shelf
  • A pointer to the array of Food objects, and because pointers dont have any addition info on their own
  • A maximum capacity of the array
  • A count of how many Food objects you currently have

In addition, you should create the following functions (plus the special functionsa copy constructor, assignment operator, and destructor):

Cabinet

The Cabinet class in some ways is very similar to the Shelf. It will store a dynamic array, this time of Shelf objects. It will also need a name and ways to keep track of how many Shelves it can store, versus how many it is currently storing.

Based on the layout of main.cpp (and the test output), you will also need a way to print everything in its inventory (which consists of Shelves which consist of Food), but also a way to get the average price of each food item.

Be sure to define the big three as well.

Tips

A few tips about this assignment:

  • Remember the "Big Three" or the Rule of Three o If you define one of the three special functions (copy constructor, assignment operator, or destructor), you should define the other two
  • You can print out a tab character (the escape sequence '\t' ) to help line up the output.
  • Don't try to tackle everything all at once. Work on one class at a time. Can't have a Shelf without Food...
  • You can customize the way numbers are displayed in C++ (particularly floating-point numbers).

The header file contains this functionality. Look into std::fixed and std::setprecision()

  • Refer back to the recommended chapters in your textbook as well as lecture videos for an explanation of the details of dynamic memory allocation o There are a lot of things to remember when memory allocation

n various food items. To that end, there are three classes you will be writing: Food Shelf Cabinet For this assignment, main.cpp will be provided for you, so you dont have to worry about the structure of the program. Instead, you can focus solely on the structure of the classes and their interactions. Food The Food class is the basic container of this assignment. You will need to store the following data: The type of food (such as cereal, fruit, vegetable, frozen etc) The name of the food product (such as Fruity Pebbles, Ramen, Tomato, etc) The expiration year The price How many calories does the food have? In addition to a constructor which takes in the necessary information, you will also need the following functions defined. If you are using dynamic memory in your class, you will also want to define a destructor, in which you will clean up (delete) any memory which was allocated by the class. Shelf The Shelf class is a bit more sophisticated. Its purpose is to store an array of Food objects. Each Shelf that you create could have a different number of Food objects, so you will have to use dynamic memory allocation in this case. Your Shelf should contain variables for the following: The name of the Shelf A pointer to the array of Food objects, and because pointers dont have any addition info on their own A maximum capacity of the array A count of how many Food objects you currently have In addition, you should create the following functions (plus the special functionsa copy constructor, assignment operator, and destructor): Cabinet The Cabinet class in some ways is very similar to the Shelf. It will store a dynamic array, this time of Shelf objects. It will also need a name and ways to keep track of how many Shelves it can store, versus how many it is currently storing. Based on the layout of main.cpp (and the test output), you will also need a way to print everything in its inventory (which consists of Shelves which consist of Food), but also a way to get the average price of each food item. Be sure to define the big three as well. Tips A few tips about this assignment: Remember the "Big Three" or the Rule of Three o If you define one of the three special functions (copy constructor, assignment operator, or destructor), you should define the other two You can print out a tab character (the escape sequence '\t' ) to help line up the output. Don't try to tackle everything all at once. Work on one class at a time. Can't have a Shelf without Food... You can customize the way numbers are displayed in C++ (particularly floating-point numbers). The header file contains this functionality. Look into std::fixed and std::setprecision() Refer back to the recommended chapters in your textbook as well as lecture videos for an explanation of the details of dynamic memory allocation o There are a lot of things to remember when memory allocation

main

#include "Food.h" #include "Cabinet.h" #include "Shelf.h" #include #include #include #include #include

using namespace std;

void TestOne(Food *v); void TestTwo(Food *v);

int main() { // Initialize some data. It's hard-coded here, but this data could come from a file, database, etc Food foods[] = { Food("Vegetable", "Avocado", 2019, 15, 5), Food("Cheese", "Moldy Cheddar", 1999, 7, 400), Food("Breakfast", "Pop-Tart", 2025, 5, 754), Food("Cereal", "Lucky Charms", 2001, 4, 600), Food("Frozen", "Cheese Pizza", 2021, 10, 1001), Food("Very-Processed", "Ramen", 3005, 1, 9999), Food("Expensive", "Caviar", 2017, 180, 150), };

int testNum; cin >> testNum;

if (testNum == 1) TestOne(foods); else if (testNum == 2) TestTwo(foods);

return 0; }

void TestOne(Food *foods) { // Shelfs to store the foods Shelf shelf("\"Healthy-Food Shelf\"", 3); //calls the constructor with parameters shelf.AddFood(&foods[0]); shelf.AddFood(&foods[1]); shelf.AddFood(&foods[2]);

Shelf secondary("\"Treat Your Shelf\"", 4); secondary.AddFood(&foods[3]); secondary.AddFood(&foods[4]); secondary.AddFood(&foods[5]); secondary.AddFood(&foods[6]);

// A "parent" object to store the Shelfs Cabinet cabinet("COP3503 Dormroom Cabinet", 2); cabinet.AddShelf(&shelf); //adding show rooms is the issue cabinet.AddShelf(&secondary);

cabinet.ShowInventory(); }

void TestTwo(Food *foods) { // Shelfs to store the foods Shelf shelf("\"Healthy-Food Shelf\"", 3); shelf.AddFood(&foods[0]); shelf.AddFood(&foods[1]);

Shelf secondary("\"Treat Your Shelf\"", 4);

secondary.AddFood(&foods[4]); secondary.AddFood(&foods[5]);

Shelf third("\"Treat Your Shelf\"", 4); third.AddFood(&foods[3]); // A "parent" object to store the Shelfs Cabinet cabinet("COP3503 Dormroom Cabinet", 3); cabinet.AddShelf(&shelf); cabinet.AddShelf(&secondary); cabinet.AddShelf(&third);

cout << "Using just the GetAveragePrice() function ";

cout << "Average price of the food in the cabinet: $" << std::fixed << std::setprecision(2); cout << cabinet.GetAveragePrice(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How would you describe the work atmosphere?

Answered: 1 week ago

Question

4. Will technology eliminate the need for HR managers?

Answered: 1 week ago