PLease! IN C++ Write A Class Declare a class/struct named NutritionData that contains these private fields foodName (string) servingSize (int) calFromCarb (double) calFromFat (double) calFromProtein (double) Use the data types in parentheses for the fields. Note that you need to use the proper C++ syntax for the fields.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

PLease! IN C++

Write A Class

Declare a class/struct named NutritionData that contains these private fields

foodName (string)

servingSize (int)

calFromCarb (double)

calFromFat (double)

calFromProtein (double)

Use the data types in parentheses for the fields. Note that you need to use the proper C++ syntax for the fields.

Each field should have a comment documenting what it is for. Place one comment above each field.

Add the public default constructor. Read the textbook for the syntax of the default constructor. Write the body of the default constructor inline.  The default constructor initializes the fields so that the food name is an empty string and all other fields are 0 for int and 0.0 for double.

Add public mutator member functions to set the fields. One mutator member function for each field. Each mutator member function's name 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. Write the bodies of the mutator functions inline.

Add public accessor member functions to return the fields. One accessor 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. You must place the keyword const to indicate accessor function (look up the textbook or some other resource for details). Each member function should have a comment above the declaration describing its purpose. Write the bodies of these accessor functions inline.

Add a special public accessor member function named getCaloriesPerServing that returns the total calories from carb, protein and fat. Again add the const keyword and place the comment above the declaration of this member function. Write the body outside of the class.

Write the definition of getCaloriesPerServing outside of the class declaration.

Regarding the block comment for the function, place it inside the class above the member function declaration.

Write the declaration of the function named printNutritionData. The function receives two parameters, a pointer to const NutritionData and an int for the number of NutritionData, and returns void. The parameters represent an array of NutritionData objects.

Write the main function as follows:

The main function gets nutrition data for a list of foods from a file named 'nutri.txt' and store them in an array of NutritionData objects. It then calls printNutritionData to print the nutrition data of the foods. You can download the file from nutri.txt  download and place it in the same folder/directory as your executable program of this lab.

The first line of the file contains the number of nutrition data stored in the file. Get that number and use that number to dynamically create an array of NutritionData object. Save the address in a pointer variable. Do not change this pointer until the memory is recycled through the delete operation.

For each object in the array, populate the object with data from the input file. The data for each food are stored in the file as a comma delimited string in the file.  They are food name, serving size (an integer), calories from carb, calories from fat, and calories from protein. Here is an example for one food.

Bread pita whole wheat, 64, 134, 14, 22.6

Use the mutator functions to set the nutrition data for each object in the array.

Print the nutrition data of foods on the screen by calling printNutritionData.

Recycle the allocated array using the delete operator.

Reset the pointer to nullptr. (Don't use NULL, we have a C++ keyword named nullptr.)

Write the block comment and the definition of the function printNutritionData.

Use a loop, cout, and the accessor functions to print the nutrition data.

Here is the template.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

// Write the class and definition of its member functions here . . .


// Write the prototype of printNutritionData here . . .

int main() {
// Write your code here . . .

return 0;
}

//******************************************************************************
//* Print an array of nutrition informatio.
//*
//* Parameters
//* ...
//* ...
//* Return
//* void
//******************************************************************************
// Write the definition of printNutritionData here . . .

 

|nutri - Notepad
File Edit Format View Help
5
Аpples 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
#include
#include <iomanip>
#include <string>
#include <fstream>
17
<iostream>
18
19
20
21
22
using namespace std;
23
24
// Write the class and definition of its member functions here .
25
26
27
// Write the prototype of printNutritionData here . . .
28
29
int main ()
{
30
// Write your code here.
31
32
return 0;
33
34
35
//* Print an array of nutrition informatio.
//*
36
37
//*
//*
38
Parameters
39
//*
//* Return
//*
//********
// Write the definition of printNutritionData here
40
...
41
42
void
43
44
Transcribed Image Text:|nutri - Notepad File Edit Format View Help 5 Аpples 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 #include #include <iomanip> #include <string> #include <fstream> 17 <iostream> 18 19 20 21 22 using namespace std; 23 24 // Write the class and definition of its member functions here . 25 26 27 // Write the prototype of printNutritionData here . . . 28 29 int main () { 30 // Write your code here. 31 32 return 0; 33 34 35 //* Print an array of nutrition informatio. //* 36 37 //* //* 38 Parameters 39 //* //* Return //* //******** // Write the definition of printNutritionData here 40 ... 41 42 void 43 44
Test The Program
The output should look exactly as follows:
Food Name: Apples raw
Serving Size: 110 g
Calories Per Serving: 52.8
Calories From Carb: 50.6
Calories From Fat: 1.2
Calories From Protein: 1.0
Food Name: Bananas
Serving Size: 225 g
Calories Per Serving: 200.4
Calories From Carb: 186.0
Calories From Fat: 6.2
Calories From Protein: 8.2
Food Name: Bread pita whole wheat
Serving Size: 64 g
Calories Per Serving: 170.6
Calories From Carb: 134.0
Calories From Fat: 14.0
Calories From Protein: 22.6
Food Name: Broccoli raw
Serving Size: 91 g
Calories Per Serving: 31.0
Calories From Carb: 21.9
Calories From Fat: 2.8
Calories From Protein: 6.3
Food Name: Carrots raw
Serving Size: 128 g
Calories Per Serving: 52.5
Calories From Carb: 46.6
Calories From Fat: 2.6
Calories From Protein: 3.3
Submit Lab3a.cpp
Your code may be tested with a different set of data.
Transcribed Image Text:Test The Program The output should look exactly as follows: Food Name: Apples raw Serving Size: 110 g Calories Per Serving: 52.8 Calories From Carb: 50.6 Calories From Fat: 1.2 Calories From Protein: 1.0 Food Name: Bananas Serving Size: 225 g Calories Per Serving: 200.4 Calories From Carb: 186.0 Calories From Fat: 6.2 Calories From Protein: 8.2 Food Name: Bread pita whole wheat Serving Size: 64 g Calories Per Serving: 170.6 Calories From Carb: 134.0 Calories From Fat: 14.0 Calories From Protein: 22.6 Food Name: Broccoli raw Serving Size: 91 g Calories Per Serving: 31.0 Calories From Carb: 21.9 Calories From Fat: 2.8 Calories From Protein: 6.3 Food Name: Carrots raw Serving Size: 128 g Calories Per Serving: 52.5 Calories From Carb: 46.6 Calories From Fat: 2.6 Calories From Protein: 3.3 Submit Lab3a.cpp Your code may be tested with a different set of data.
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
Data members
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education