A little push with C++ assignment

Hello, I have a program I need to solve, and i can't seem to figure out how to code it. the question is as follows:

Design a Meal class with two fields, one that holds the name of the entree, the other that holds a calorie count integer. Include a constructor that sets a Meal's fields with arguments, or uses default values when no arguments are provided. Include an overloaded insertion operator function that displays a Meal's values. Include an overloaded extraction operator that prompts a user for an entree name and calorie count for a meal. Include an overloaded operator +() function that allows you to add two or more Meal objects. Adding two Meal objects means adding their calorie values and creating a summary Meal object in which you store "Daily Total" in the entree field. E. Write a main() program that declares four Meal objects named breakfast, lunch, dinner, and total. Provide values for the breakfast, lunch, and dinner objects. Include the statement total = breakfast + lunch + dinner; in your program, then display values for the four Meal objects.


I haven't gotten far in my code, this is what I have so far:

#include<iostream>

using namespace std;


class Meal
{
private:
string name;
int calories;
public:
Meal(string,int);
int operator+(Meal);
};

Meal :: Meal(string name = "Daily Total", int calories = 100)
{
this->name = name;
this->calories = calories;

}


int Meal :: operator+(Meal meal)
{
int cals = calories + meal.calories;
}

int main()
{
Meal breakfast;
Meal lunch;
Meal dinner;
Meal total;



}



Im not sure where to go from here, if anyone can give me a push it would be greatly appreciated. Thank you
1) For you constructors, I believe you should have two separate ones.
1
2
3
4
5
6
7
8
9
10
11
Meal::Meal() //default constructor
{
this->name = "Not Given";      //Can set to whatever deafault value
this->calories = 0;                 //I would set default calories to zero
}

Meal::Meal(string name, int calories)
{
this->name = name;
this->calories = calories;
}

Then when you call the constructor in main(), you can either do Meal breakfast() and the default values will be put into the Meal variables, or you could do put arguments in.
1
2
3
4
5
6
7
8
9
10
int main()
{
Meal breakfast();  //default constructor, so default values
Meal lunch("lunch", 400);
Meal dinner("dinner", 500);

int mealsCombined = lunch + dinner;

return 0;
}



2) For your overloading of the + operator, you need to return it so you can assign it to a variable in main(). Then you can output it or whatever you need to do to it in main, or send it to another function to use it there.
1
2
3
4
5
int Meal::operator+(Meal meal)
{
int cals = calories + meal.calories;
return cals;
}
Last edited on
Looks like that was what i was doing wrong with the operator function, thanks a ton!
Topic archived. No new replies allowed.