In: Computer Science
7.26 LAB: Nutritional information (classes/constructors)
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
__________________________________________________________
Given Code:
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>
// 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);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU WANT ANY CHANGES REGARDING THE SOLUTION OR IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CODE TO COPY
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.cpp
#include "FoodItem.h"
#include <stdio.h>
FoodItem::FoodItem()
{
name = "None";
fat = 0.0;
carbs = 0.0;
protein = 0.0;
}
FoodItem::FoodItem(string itemName,double amountFat,double
amountCarbs,double amountProtein){
name = itemName;
fat = amountFat;
carbs = amountCarbs;
protein = amountProtein;
}
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);
}
FoodItem.h
#ifndef FOODITEMH
#define FOODITEMH
#include <string>
using namespace std;
class FoodItem {
public:
FoodItem();
FoodItem(string itemName,double amountFat,double
amountCarbs,double amountProtein);
string GetName();
double GetFat();
double GetCarbs();
double GetProtein();
double GetCalories(double numServings);
void PrintInfo();
private:
string name;
double fat;
double carbs;
double protein;
};
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EXPLANATION
IN FoodItem.h
FoodItem();//declaring no argument construtoe
FoodItem(string itemName,double amountFat,double amountCarbs,double amountProtein); // declaring arguments and their datatype along with their name
In FoodItem.cpp
FoodItem::FoodItem() // CREATING DEFINITION TO THE NO ARGUMENT
CONSTRUCTOR
{
name = "None"; // assigning name as NONE
fat = 0.0; // assigning all other variables as
0.0
carbs = 0.0;
protein = 0.0;
}
//creating definition to argumented constructor
FoodItem::FoodItem(string itemName,double amountFat,double
amountCarbs,double amountProtein){
name = itemName; //assigning the name
fat = amountFat; // assigning the fat
carbs = amountCarbs; // assigning the carbs
protein = amountProtein; //assigning the fat
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU HAVE ANY DOUBTS REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////