Make sure to include: #include <cassert> Create two classes: Item class private
ID: 3538586 • Letter: M
Question
Make sure to include:
#include <cassert>
Create two classes:
- Item class
-
- private member m_name - string
- private member m_price - float
- public function Setup
-
- Does not return any values
- Takes two parameters
-
- const string-reference "name"
- float "price"
- Setup function assigns values to m_name and m_price
- public function GetPrice() - returns a float (m_price)
- public function GetName() - returns a string (m_name)
- Menu class
-
- private member m_menuItems - array of 5 Items (the class you created)
- public constructor Menu() - initializes menu items
-
- 0: Pizza, 9.99
- 1: Hamburger, 4.99
- 2: Chicken Sandwich, 5.99
- 3: French Fries, 0.99
- 4: Salad, 2.99
- public function PrintMenu()
-
- Returns nothing
- No parameters
- Outputs each item on the menu, including the index of that element, the name, and the price.
- public function GetFood(...)
-
- Returns a pointer to an Item
- One parameter: index (integer)
- use an assert to validate that the index is O.K.:
-
- assert( index >= 0 && index < 5 );
- return the address to the menu item at that index
Explanation / Answer
#include <iostream>
#include <cassert>
#include <string>
#include <assert.h>
using namespace std;
class item
{
string m_name;
float m_price;
public:
void Setup(const string& name,float price)
{
m_name=name;
m_price=price;
}
string getName()
{
return m_name;
}
float getPrice()
{
return m_price;
}
};
class Menu {
item m_menuItems[5];
public:
Menu()
{
m_menuItems[0].Setup("Pizza", 9.99);
m_menuItems[1].Setup("Hamburger", 4.99);
m_menuItems[2].Setup("Chicken Sandwich", 5.99);
m_menuItems[3].Setup("French Fries", 0.99);
m_menuItems[4].Setup("Salad", 2.99);
}
void PrintMenu()
{
for (int i=0; i<5; i++) {
cout << "Index " <<i+1 << " name " <<m_menuItems[1].getName() << " price " << m_menuItems[i].getPrice()<<endl;
}
}
item& getFood(int index)
{
assert( index >= 0 && index < 5 );
return m_menuItems[index];
}
};
if you have any doubt , you can ask me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.