asked 205k views
4 votes
Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files Foodltem.h and Food item.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "Water" 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 class data member with the appropriate parameter value.

asked
User Zackify
by
8.4k points

2 Answers

1 vote

Final answer:

The FoodItem class needs constructors to initialize each food item, including a default constructor and a second constructor with four parameters.

Step-by-step explanation:

The FoodItem class needs to be defined with constructors that initialize each food item. The default constructor should set the name to 'Water' 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. Each class data member should be assigned the appropriate parameter value.

answered
User Thanx
by
8.4k points
4 votes

Below is an example implementation of the FoodItem class with the specified constructors in both the header file (FoodItem.h) and the source file (FoodItem.cpp).

#ifndef FOODITEM_H

#define FOODITEM_H

#include <string>

class FoodItem {

public:

// Default constructor

FoodItem();

// Parameterized constructor

FoodItem(const std::string& name, double fat, double carbohydrates, double protein);

// Other member functions if needed

private:

std::string name;

double gramsOfFat;

double gramsOfCarbohydrates;

double gramsOfProtein;

};

#endif // FOODITEM_H

Write a program that aligns with the information below

Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files Foodltem.h and Food item.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "Water" 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 class data member with the appropriate parameter value.

answered
User Termosa
by
8.7k points