Question

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

Nutritional information per serving of M&Ms: Fat: 10.00 g Carbohydrates: 34.00 g Protein: 2.00 g Number of calories for 1.00

File is marked as read only Current file: main.cpp 1 #include FoodItem.h 2 #include <stdio.h> 3 #include <iostream> 4 5 usi

double numServings; cin >> numServings; FoodItem1. PrintInfo(); printf(Number of calories for %.2f serving(s): %.2f\n, nums

Current file: Foodltem.h Load default template... 1 #ifndef FOODITEMH 2 #define FOODITEMH 3 4 #include <string> 5 6 using nam

21 double GetProtein(); 22 23 double GetCalories (double numServings); 24 25 void PrintInfo(); 26 27 private: 28 string name;

Current file: Foodltem.cpp Load default template... 1 #include FoodItem.h 2 #include <stdio.h> 3 4. // Define default const

21 double FoodItem::GetProtein() { 22 return protein; 23 } 24 25 double FoodItem::GetCalories (double numServings) { 26 // Ca

In C++ please.

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Step1:

FoodItem.h file :

Everything about Code is Explained in Comments of Code:

#ifndef FOODITEM
#define FOODITEM
#include <string>
using namespace std;
class FoodItem
{
    //private data members
private:
    string name;
    double fat;
    double carbs;
    double protein;

//public data members
public:
    FoodItem();
    FoodItem(string name, double fat, double carbs, double protein);
    string getName();
    double GetFat();
    double GetCarbs();
    double GetProtein();
    double GetCalories(double numServings);
    void PrintInfo();
};
#endif

Step2:

FoodItem.cpp: Everything About Code is Explained in Comments of Code:

#include "FoodItem.h"
#include <stdio.h>
//default constructor 
FoodItem::FoodItem()
{
    name = "None";
    fat = 0.0;
    carbs = 0.0;
    protein = 0.0;
}
//parametrize Constructor 
FoodItem::FoodItem(string name, double fat, double carbs, double protein)
{
    this->name = name;
    this->fat = fat;
    this->carbs = carbs;
    this->protein = protein;
}
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)
{
    double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;
    return calories;
}
void FoodItem::PrintInfo()
{
    printf("Nutritional information per serving of %s:\n", name);
    printf("  Fat: %0.2lf g", fat);
    printf("  Carbohydrates: %0.2lf g", carbs);
    printf("  Protein: %0.2lf g", protein);
}

Step3:

main.cpp: Everything About Code is Explained in Comments of code:

#include "FoodItem.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int args, char *argv[])
{
    //object 1 of Class FoodItem
    FoodItem FoodItem1;
    string ItemName;
    double amountFat, amountCarbs, amountProtein;
    //input
    cin >> ItemName;
    cin >> amountFat;
    cin >> amountCarbs;
    cin >> amountProtein;
    //object 1 of Class FoodItem
    FoodItem FoodItem2 = FoodItem(ItemName, amountFat, amountCarbs, amountProtein);
    double numServings;
    cin >> numServings;
    //This function will print all information
    FoodItem1.PrintInfo();
    printf("Number of Calories for %0.2lf serving(s): %0.2lf", numServings, FoodItem1.GetCalories(numServings));
    cout << "\n\n";
    FoodItem2.PrintInfo();
    printf("Number of Calories for %0.2lf serving(s): %0.2lf", numServings, FoodItem2.GetCalories(numServings));
    cout << endl;
    return 0;
}

Output:

TERMINAL PS C:\VSCODE> cd c:\VSCODE\ ; if ($?) { g++ main.cpp -o main }; if ($?) { . \main} M&Ms 10.0 34.0 2.0 1.0 Nutriti

Thanks

Add a comment
Know the answer?
Add Answer to:
In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Nutritional information (classes/constructors)

    7.16 LAB: Nutritional information (classes/constructors)Given main(), complete the FoodItem class (in file FoodItem.java) 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...

  • Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the...

    Solve in C++ for Car.h and Car.cpp 8.21 LAB: Car value (classes) Given main, complete the Car class (in files Car hand Car.cpp) with member functions to set and get the purchase price of a car (SetPurchase Price().GetPurchase Price)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...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • I am struggling with a program in C++. it involves using a struct and a class...

    I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors. Instructions A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per...

  • c++ program Notes 1. Your program should use named string constant for the file name to open the file. Do not add any pa...

    c++ program Notes 1. Your program should use named string constant for the file name to open the file. Do not add any path to the file name because the path on your computer is not likely to exist on the computer the instructor uses to grade the program. Points may be deducted if you don't follow this instruction. 2. Split statements and comments longer than 80 characters into multiple lines and use proper indentations for the split portions. 3....

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • can you please follow all the instructions ? The answers that I got has either missing...

    can you please follow all the instructions ? The answers that I got has either missing range for loop or one funtion . Read the instructions carefully. At least 10% will be deducted if the instructions are not followed. For general lab requirements, see General Lab Requirements. Do not prompt the user for input during the execution of your program. Do not pause at the end of the execution waiting for the user's input. Notes 1. Your program should use...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT