Question

In: Computer Science

7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp)...

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

Solutions

Expert Solution

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Related Solutions

7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp)...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors...
7.28 LAB: Artwork label (classes/constructors). Written in C++ Given main(), complete the Artist class (in files...
7.28 LAB: Artwork label (classes/constructors). Written in C++ Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()). Ex: If the input is: 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is: Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770 Note: printInfo() should use three spaces for...
9.9 LAB: Artwork label (classes/constructors) Define the Artist class with a constructor to initialize an artist's...
9.9 LAB: Artwork label (classes/constructors) Define the Artist class with a constructor to initialize an artist's information and a print_info() method. The constructor should by default initialize the artist's name to "None" and the years of birth and death to 0. print_info() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class with a constructor to initialize an artwork's information and a print_info() method. The constructor should by...
7.24 LAB: Triangle area comparison (classes) Language: C++ Given class Triangle (in files Triangle.h and Triangle.cpp),...
7.24 LAB: Triangle area comparison (classes) Language: C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions. Ex: If the input is: 3.0 4.0 4.0 5.0 where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is: Triangle...
8.16 LAB: Mileage tracker for a runner Given the MileageTrackerNode class, complete main() to insert nodes...
8.16 LAB: Mileage tracker for a runner Given the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user-input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. DO NOT print the dummy head node. Ex. If the input is: 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 the output is: 2.2, 7/2/18 3.2, 7/7/18 4.5, 7/16/18 _____________________________ The given code that...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the formula is: team_wins / (team_wins + team_losses) Note: Use floating-point division. Ex: If the input is: Ravens 13 3 where Ravens is the team's name, 13 is the number of team wins, and 3 is the number of team losses, the output is: Congratulations, Team Ravens has a winning average! If the input is Angels 80 82, the output is: Team Angels has a...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT