Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes, overloaded insertion operator and such

Status
Not open for further replies.

bnd98

Technical User
Apr 1, 2003
17
US
Looks like a great forum! After many years going to the same job day after day I decided to take a class to see what may spark some interest. It's a free class, and we do not even get grades, just sort of an introductory. Well I am stuck (at least not on the first program) and have no idea what to do. Here is the objective:

A. 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 arguements, or uses default values when no arguements are provided.

B. Include an overloaded insertion operator function that displays a Meal's values.

C. Include an overload extraction operator that prompts a user for an entree name and calorie count for a meal.

D. Include an overloaded operator+() function that allows you to add two or more Meal object. 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 the program then display values for the four Meal objects.

F. Write a main() program that delcares an array for 21 Meal objects. Allow a user to enter values for 21 Meals for the week. Total these meals and display calorie total for the end of the week.

It is alot I know! For an intro class I think this one is extremely hard and again I am lost. Any suggestions? Additional sites, reference books...anything for help is greatly appreciated.
 
there are a few posts out now on insertion and extraction operators, you might check out activity in the last couple of days in the forum, it might help, and prevent repeating information...

check for &quot;insertion&quot; or &quot;>>&quot; or &quot;<<&quot;, those subject key phrases will send you to what you're looking for :) The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
That sounds like a great project! I might just use that when mentoring in the future, plagiarism and all LOL

Now to your specific problem. In general I simplify any project down to the individual problems that I need to solve, you have already done that fairly well. Usually I will solve each problem in an isolated fashion first before I integrate each solution into the end result objective code base. These isolated solutions include unit test code to prove the solution wherever needed. If you should choose to take this approach then I would be happy to assist you with each individual problem as you tackle it Post small relevant blocks of code that you have problems with or questions about.

As for book resources for beginners I always suggest Bjarne Stroustrup’s “The C++ Programming Language”.

-pete


 
Thank you both! Now this is the code that I have and I think that it covers A thru D:

#include<iostream.h>
#include<conio.h>
#include<string.h>

class Meal
{
friend ostream& operator<<(ostream& out, const Meal &oMeal);
friend istream& operator>>(istream& in, Meal &oMeal);
private:
char entree[20];
int calorie;
public:
Meal(char ent = &quot;Noodles&quot;, int cal = 25);
double add(const Meal &aMeal);
double operator+(const Meal &aMeal);
};

Meal::Meal(char ent = &quot;Noodles&quot;, int cal = 25);
{
entree = ent;
calorie = cal;
};

ostream& operator<<(ostream& out, const Meal &oMeal)
{
out<<&quot;The name of the entree is <<oMeal.entree<<&quot; . Calories:<<oMeal.calorie<<endl;
return(out);
};

istream& operator>>(istream& in, Meal &oMeal)
{
in>>&quot;Enter the entree name: &quot;<<oMeal.entree<<endl:
in>>&quot;Enter the amount of calories: &quot;<<oMeal.calorie<<endl;
return(in);
};

double Meal::add(const Meal &aMeal)
{
double total;
total = calorie + aMeal.calorie;
return(total);
};

double Meal::eek:perator+(const Meal &aMeal)
{
double total;
total = calorie + aMeal.calorie;
return(total);
};

Now I don't know how to even start E. What I have so far looks o.k.?

Debbie
 
Amazing what you see after you post :) This is one part that I corrected after I posted.

istream& operator>>(istream& in, Meal &oMeal)
{
cout<<&quot;Enter the entree name: &quot;;
in>>oMeal.entree;
cout<<&quot;Enter the amount of calories: &quot;;
in>>oMeal.calorie;
return(in);
};
 
I fixed all of the problems. Boy was I way off. Thanks again you two!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top