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); }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter3: Input/output
Section: Chapter Questions
Problem 12SA: 12. What is the output of the following program? (2, 3, 6, 8) #include <iostream> #include...
icon
Related questions
Question

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.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 <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);
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Returning value from Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr