Question: In C++, Write A Class Declare a class named NutritionData that contains these private fields foodN (string) servingS (int) calCarb (double) calFat (double) calProtein (double)
In C++, Write A Class
Declare a class named NutritionData that contains these private fields
foodN (string)
servingS (int)
calCarb (double)
calFat (double)
calProtein (double)
totalCalories (double)
Add the default constructor.
Add a constructor that accepts these data: food name (string), serving size (int), cal from carb (double), cal from fat (double) and cal from protein (double).
Add mutator member functions to set the fields. One member function for each field except totalCalories. Each member function should begin with the word 'set' followed by the field name with the first letter changed to uppercase. Each member function should have a comment above the declaration describing its purpose.
Add accessor member functions to return the fields. One member function for each field. Each member function should begin with the word 'get' followed by the field name with the first letter changed to uppercase. Each member function should have a comment above the declaration describing its purpose.
Write the definition for the default constructor. The default constructor initializes the fields so that the food name is an empty string and all other fields are 0.0.
Write the definition for the other constructor which sets all the fields based on the parameters received. It sets the total calories field based on the given three calories.
Write the definition for every member function. When one of the mutator functions for the calories is set, it should also update the totalCalories.
Write the main function as follows:
Define and initialize an array named nutritionData using the following data:
Apples raw, 110, 50.6, 1.2, 1.0
Bananas, 225, 186, 6.2, 8.2
Bread pita whole wheat, 64, 134, 14, 22.6
Broccoli raw, 91, 21.9, 2.8, 6.3
Carrots raw, 128, 46.6, 2.6, 3.3
Use a range-based for loop to print the nutrition data for all foods to the screen using the accessor functions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
