10.23 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in files Foodltem.
Posted: Sun Jul 10, 2022 11:25 am
starter code:
FoodItem.cpp:
#include "FoodItem.h"#include <iostream>#include <iomanip>
using namespace std;
// Define default constructor
// Define second constructor with arguments// to initialize private data members
string FoodItem::GetName() { return name;}
double FoodItem::GetFat() { return fat;}
double FoodItem::GetCarbs() { return carbs;}
double FoodItem::GetProtein() { return protein;}
double FoodItem::GetCalories(double numServings) { // Calorie formula double calories = ((fat * 9) + (carbs * 4) + (protein* 4)) * numServings; return calories;}
void FoodItem::PrintInfo() { cout << fixed << setprecision(2); cout << "Nutritional information per serving of" << name << ":" << endl; cout << " Fat: " << fat << "g" << endl; cout << " Carbohydrates: " << carbs<< " g" << endl; cout << " Protein: " << protein<< " g" << endl;}
--------------------------------------------------------------------------------------------------------------
main.cpp:
#include "FoodItem.h"#include <iostream>#include <iomanip>
using namespace std;
int main() { string itemName; double amountFat, amountCarbs, amountProtein; double numServings; cout << fixed << setprecision(2); cin >> itemName;
if(itemName == "Water" || itemName == "water"){ FoodItem foodItem; foodItem.PrintInfo(); cout << fixed <<setprecision(2); cout << "Number of calories for "<< 1.0 << " serving(s): " <<foodItem.GetCalories(1.0) << endl; } else { cin >> amountFat; cin >> amountCarbs; cin >> amountProtein; cin >> numServings; FoodItem foodItem = FoodItem(itemName,amountFat, amountCarbs, amountProtein); foodItem.PrintInfo(); cout << "Number of calories for "<< 1.0 << " serving(s): " <<foodItem.GetCalories(1.0) << endl; cout << "Number of calories for "<< numServings << " serving(s): " <<foodItem.GetCalories(numServings) << endl; }
return 0;}
--------------------------------------------------------------------------------------------------------------------
FoodItem.h:
#ifndef FOODITEMH#define FOODITEMH
#include <string>
using namespace std;
class FoodItem { public: // TODO: Declare default constructor
// TODO: Declare second constructor witharguments // to initialize private data members string GetName();
double GetFat();
double GetCarbs();
double GetProtein();
double GetCalories(double numServings);
void PrintInfo();
private: string name; double fat; double carbs; double protein;};
#endif
10.23 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in files Foodltem.h and Foodltem.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 Number of calories for 3.00 serving (s): 234.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.