The local health food restaurant has a new entree sampler, where you can get a p
ID: 3859025 • Letter: T
Question
The local health food restaurant has a new entree sampler, where you can get a platter with portions of four different entrees.
Write a class called Entree that has data members for the name of the entree (string) and for the number of calories (int). It should have a default constructor that sets the name to the empty string ("") and the number of calories to zero. It should have a constructor that takes two parameters and uses them to initialize the data members. It should have get methods for both data members.
Write a class called EntreeSampler that has four data members of type Entree. It should have a constructor that takes four parameters and uses them to initialize its data members. It should have a method called listEntrees that prints out the names of the four entrees in the EntreeSampler. It should have a method called totalCalories that returns the total number of calories in the EntreeSampler.
Your functions should have the following names:
getName
getNumCalories
listEntrees
totalCalories
The files must be named: Entree.hpp, Entree.cpp, Entreemain.cpp, EntreeSampler.hpp and EntreeSampler.cpp, EntreeSamplermain.cpp
Explanation / Answer
The needed files for the code with output given below. Please rate the answer if it helped. Thank you.
Entree.hpp
#ifndef Entree_hpp
#define Entree_hpp
#include <iostream>
using namespace std;
class Entree
{
private:
string name; //name of entree
int calories; // number of calories
public:
Entree(); //default constructor
Entree(string name1, int cal);//parameterized constructor
string getName(); //getter for name
int getNumCalories(); //getter for calories
};
#endif /* Entree_hpp */
Entree.cpp
#include "Entree.hpp"
Entree::Entree() //default constructor
{
name = "";
calories = 0;
}
Entree::Entree(string name1, int cal)//parameterized constructor
{
name = name1;
calories = cal;
}
string Entree::getName() //getter for name
{
return name;
}
int Entree::getNumCalories() //getter for calories
{
return calories;
}
Entreemain.cpp
#include <iostream>
#include "Entree.hpp"
using namespace std;
int main()
{
Entree emptyEntree; //default constructor
Entree chickenfry("chicken fry", 300); //parametrized constructor
Entree soup("crab soup", 200);
cout << "Displaying emptyEntry, Name: " << emptyEntree.getName() << ", Calories: " << emptyEntree.getNumCalories() << endl;
cout << "Displaying chickenfry, Name: " << chickenfry.getName() << ", Calories: " << chickenfry.getNumCalories() << endl;
cout << "Displaying soup, Name: " << soup.getName() << ", Calories: " << soup.getNumCalories() << endl;
return 0;
}
output
Displaying emptyEntry, Name: , Calories: 0
Displaying chickenfry, Name: chicken fry, Calories: 300
Displaying soup, Name: crab soup, Calories: 200
EntreeSampler.hpp
#ifndef EntreeSampler_hpp
#define EntreeSampler_hpp
#include "Entree.hpp"
class EntreeSampler
{
private:
Entree e1;
Entree e2;
Entree e3;
Entree e4;
public:
EntreeSampler(Entree E1, Entree E2, Entree E3, Entree E4); //parametrized constructor
void listEntrees(); //fucntion to print the names of 4 entrees in the sampler
int totalCalories(); //total calories the 4 entrees
};
#endif /* EntreeSampler_hpp */
EntreeSampler.cpp
#include "EntreeSampler.hpp"
#include <iostream>
using namespace std;
EntreeSampler::EntreeSampler(Entree E1, Entree E2, Entree E3, Entree E4) //parametrized constructor
{
e1 = E1;
e2 = E2;
e3 = E3;
e4 = E4;
}
void EntreeSampler::listEntrees() //fucntion to print the names of 4 entrees in the sampler
{
cout << "The entrees in the sampler are " << endl;
cout << " Name: " << e1.getName() << endl;
cout << " Name: " << e2.getName() << endl;
cout << " Name: " << e3.getName() << endl;
cout << " Name: " << e4.getName() << endl;
}
int EntreeSampler::totalCalories() //total calories the 4 entrees
{
return e1.getNumCalories() + e2.getNumCalories() + e3.getNumCalories() + e4.getNumCalories();
}
EntreeSamplermain.cpp
#include <iostream>
#include "EntreeSampler.hpp"
#include "Entree.hpp"
using namespace std;
int main()
{
Entree fry("Chicken Fry", 300);
Entree rice("Fried Rice", 350);
Entree soup("Crab Soup", 200);
Entree dessert("Chocolate Mousse", 400);
EntreeSampler sampler(soup, rice, fry, dessert);
sampler.listEntrees();
cout << "Total calories: " << sampler.totalCalories() << endl;
return 0;
}
output
The entrees in the sampler are
Name: Crab Soup
Name: Fried Rice
Name: Chicken Fry
Name: Chocolate Mousse
Total calories: 1250
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.