7.24 LAB - Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files Foodltem.h

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

7.24 LAB - Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files Foodltem.h

Post by answerhappygod »

7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 1
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 1 (50.71 KiB) Viewed 46 times
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 2
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 2 (38.14 KiB) Viewed 46 times
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 3
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 3 (39.28 KiB) Viewed 46 times
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 4
7 24 Lab Nutritional Information Classes Constructors Given Main Complete The Fooditem Class In Files Foodltem H 4 (44.94 KiB) Viewed 46 times
7.24 LAB - Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files Foodltem.h and Food item.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "Water" and all other fields 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 class data member with the appropriate parameter value. Ex: If the input is: Water the output is: Nutritional information per serving of Water: Fat: 0.00 g Carbohydrates: 0.00 g Protein: 0.00 g Number of calories for 1.00 serving (s): 0.00 Ex: If the input is: M&M's 10.0 34.0 2.0 3.0 where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 3.0 is the number of servings, the output is: 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 Number of calories for 3.00 serving (s): 702.00 Note: The program outputs the number of calories for one serving of a food and for the input number of servings as well. The program only outputs the calories for one serving of water.
LAB ACTIVITY 1 #include "FoodIten.h" 2 #include <iostream> 3 #include <iomanip> 4 5 6 using namespace std; 7 // Define default constructor 8 7.24.1: LAB - Nutritional information (classes/constructors) 9 10 // to initialize private data members 11 12 string FoodItem::GetName() { 13 return name; 14 } 17 18 } 15 16 double FoodItem::GetFat() { return fat; Define second constructor with arguments 19 20 double Food Item::GetCarbs() { 21 return carbs; 36 37 38 Current file: Fooditem.cpp 22 } 23 24 double FoodItem::GetProtein() { 25 return protein; 26} 27 28 double FoodItem::GetCalories (double numServings) { 29 // Calorie formula 30 39 40 31 32 } 33 34 void FoodIten::PrintInfo() { 35 cout << fixed <<<setprecision (2); cout << "Nutritional information per serving of " << name << ":" << endl; cout << "Fat: " << fat << "g" << endl; cout << Carbohydrates: double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings; return calories; << carbs << g" << endl; cout << "Protein: "<<protein << "g" << endl; 11 0/20 Load default template...
LAB ACTIVITY File is marked as read only 1 #include "FoodIten.h" 2 #include <iostream> 3 #include <iomanip> 4 5 using namespace std; 6 7 int main() { 8 9 10 11 12 13 14 15 15 16 17 18 92222222=28===== 19 20 } 21 23 24 25 26 27 30 31 32 33 34 7.24.1: LAB - Nutritional information (classes/constructors) 35 36 37 m string itemName; double amountFat, amountCarbs, amountProtein; double numServings: cout << fixed <<setprecision (2); cin >> itemName; if(itemName == "Water" || itenName == "water") { FoodIten foodItem; foodIten. PrintInfo(); } else { Current file: main.cpp cin >> amountFat; cin >> amountCarbs; cin >> amountProtein; cin >> numServings; cout << fixed <<setprecision (2); cout << "Number of calories for " << 1.0 << " serving (s): "<<foodIten.GetCalories (1.0) << endl; Foodltem.cpp main.cpp Foodltem.h return 0; FoodIten foodItem = FoodItem(itemName, amountFat, amountCarbs, amount Protein); foodIten. PrintInfo(); cout << "Number of calories for " << 1.0 << " serving(s): << foodIten.GetCalories (1.0) << endl; cout << "Number of calories for " << numServings << "serving(s): <<<foodIten.GetCalories (numServings) << endl; 0/20
LAB ACTIVITY 1 #ifndef FOODITEMH 2 #define FOODITEMH 3 4 #include <string> 5 6 using namespace std; 7 8 class FoodIten { 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 7.24.1: LAB -Nutritional information (classes/constructors) 25 26 27 28 public: // TODO: Declare default constructor string GetName(); double GetFat(); I double GetCarbs(); I double GetProtein(); T double GetCalories (double numServings); void PrintInfo(); private: Current file: Foodltem.h▾ Foodltem.cpp TODO: Declare second constructor with arguments to initialize private data members string name: double fat; double carbs; double protein; 30 31 32 3: 33 34 #endif Develop mode Submit mode main.cpp Foodltem.h 0/20 Load default template... Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply