Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" 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 private field with the appropriate parameter value. Ex: If the input is: M&M's 10.0 34.0 2.0 1.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 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 first FoodItem above is initialized using the default constructor. main.cpp #include "FoodItem.h" #include #include using namespace std; int main(int argc, char* argv[]) { FoodItem FoodItem1; string itemName; double amountFat, amountCarbs, amountProtein; cin >> itemName; cin >> amountFat; cin >> amountCarbs; cin >> amountProtein; FoodItem FoodItem2 = FoodItem(itemName, amountFat, amountCarbs, amountProtein); double numServings; cin >> numServings; FoodItem1.PrintInfo(); printf("Number of calories for %.2f serving(s): %.2f\n", numServings, FoodItem1.GetCalories(numServings)); cout << endl << endl; FoodItem2.PrintInfo(); printf("Number of calories for %.2f serving(s): %.2f\n", numServings, FoodItem2.GetCalories(numServings)); return 0; } FoodItem.h #ifndef FOODITEMH #define FOODITEMH #include using namespace std; class FoodItem { public: // TODO: Declare default constructor // TODO: Declare second constructor with arguments // 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 FoodItem.cpp #include "FoodItem.h" #include #include 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() { printf("Nutritional information per serving of %s:\n", name.c_str()); printf(" Fat: %.2f g\n", fat); printf(" Carbohydrates: %.2f g\n", carbs); printf(" Protein: %.2f g\n", protein); }
7.26 LAB: Nutritional information (classes/constructors) C++
Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" 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 private field with the appropriate parameter value.
Ex: If the input is:
M&M's 10.0 34.0 2.0 1.0where 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 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.00The first FoodItem above is initialized using the default constructor.
main.cpp
#include "FoodItem.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
FoodItem FoodItem1;
string itemName;
double amountFat, amountCarbs, amountProtein;
cin >> itemName;
cin >> amountFat;
cin >> amountCarbs;
cin >> amountProtein;
FoodItem FoodItem2 = FoodItem(itemName, amountFat, amountCarbs, amountProtein);
double numServings;
cin >> numServings;
FoodItem1.PrintInfo();
printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
FoodItem1.GetCalories(numServings));
cout << endl << endl;
FoodItem2.PrintInfo();
printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
FoodItem2.GetCalories(numServings));
return 0;
}
FoodItem.h
#ifndef FOODITEMH
#define FOODITEMH
#include <string>
using namespace std;
class FoodItem {
public:
// TODO: Declare default constructor
// TODO: Declare second constructor with arguments
// 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
FoodItem.cpp
#include "FoodItem.h"
#include <stdio.h>
#include <iostream>
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() {
printf("Nutritional information per serving of %s:\n", name.c_str());
printf(" Fat: %.2f g\n", fat);
printf(" Carbohydrates: %.2f g\n", carbs);
printf(" Protein: %.2f g\n", protein);
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images