Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you answer and explain the following question? Given main(), complete the Foodltem class (in les Foodltemh and Foodltemcpp) with constructors to initialize each food

Could you answer and explain the following question?

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Given main(), complete the Foodltem class (in les Foodltemh and Foodltemcpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other elds to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private eld with the appropriate parameter value Ex: If the input is: M&M's 10.0 34.0 2.0 1.0 where lVl&M's is the food name, 100 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 1.0 is the number of servings, the output is: Nutritional information per serving of None: Fat: 0.00 g Carbohydrates: 0.00 g Protein: 0.00 g Number of calories for 1.00 serving(s): 0.00 Nutritional information per serving of M&M's: Fat: 10.00 g Carbohydrates: 34.00 g Protein: 2.00 g Number of calories for 1.00 serving(s): 234.00 The rst Foodltem above is initialized using the default constructor. File is marked as read only Current file: main.cpp - 1 #include "FoodItem.h" 2 #include 3 #include using namespace std; 7 int main(int argc, char* argv) ({ CO FoodItem FoodItem1; 9 10 string itemName; 11 double amountFat, amountCarbs, amountProtein; 12 13 cin > > itemName; 14 cin >> amountFat; 15 cin >> amountCarbs; 16 cin >> amountProtein; 17 18 FoodItem FoodItem2 = FoodItem(itemName, amountFat, amountCarbs, amountProtein); 19 20 double numServings; 21 cin > > numServings; 22 23 FoodItem1 . PrintInfo( ) ; 24 cout using namespace std; 8 class FoodItem { 9 public: 10 // TODO: Declare default constructor 11 12 // TODO: Declare second constructor with arguments 13 // to initialize private data members 14 15 string GetName ( ) ; 16 17 double GetFat(); 18 19 double GetCarbs () ; 20 21 double GetProtein( ); 22 23 double GetCalories (double numServings) ; 24 25 void PrintInfo(); 26 27 private : 28 string name; 29 double fat; 30 double carbs; 31 double protein; 32 1; 33 34 #endifCurrent file: Fooditem.cpp - Load default template... 1 #include "FoodItem.h" 2 #include 3 #include 4 using namespace std; 6 7 // Define default constructor 8 9 // Define second constructor with arguments 10 // to initialize private data members 11 12 string FoodItem: :GetName ( ) { 13 return name; 14 } 15 16 double FoodItem: : GetFat( ) { 17 return fat; 18 } 19 20 double FoodItem: : GetCarbs() { 21 return carbs; 22 } 23 24 double FoodItem: : GetProtein() { 25 return protein; 26 } 27 28 double FoodItem: : GetCalories (double numServings) { 29 // Calorie formula 30 double calories = ((fat * 9) + (carbs * 4) + (protein * 4) ) * numServings; 31 return calories; 32 } 33 34 void FoodItem: :PrintInfo( ) { 35 cout

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

Step: 3

blur-text-image

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

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions