why can the members be accessed int his program below, but not the one i posted earlier. they look to be the same structure to me.
Code:
#include<iostream.h>
#include<string.h>
class Meal
{
friend istream& operator>>(istream& in, Meal &aMeal);
friend ostream& operator<<(ostream& out, Meal &aMeal);
private:
char mealName[10];
int calories;
public:
Meal(const char meal[], const int cal);
Meal operator+(const Meal transaction);
};
Meal::Meal(const char meal[], const int cal)
{
strcpy(mealName, meal);
calories=cal;
}
Meal Meal::operator +(const Meal transaction)
{
Meal temp("Daily Total",0);
temp.calories=calories+transaction.calories;
return(temp);
}
istream& operator>>(istream& in, Meal &aMeal)
{
cout<<"Enter entree name: ";
in>>aMeal.mealName;
cout<<"Calories from meal: ";
in>>aMeal.calories;
return(in);
}
ostream& operator<<(ostream& out, Meal &aMeal)
{
out<<"The "<<aMeal.mealName<<" contains "<<aMeal.calories<<" calories."<<endl;
return(out);
}
void main()
{
Meal breakfast("",0), lunch("",0), dinner("",0), total("Daily Total",0);
cout<<"Breakast: ";
cin>>breakfast;
cout<<"Lunch: ";
cin>>lunch;
cout<<"Dinner: ";
cin>>dinner;
total = breakfast + lunch + dinner;
cout<<breakfast<<endl;
cout<<lunch<<endl;
cout<<dinner<<endl;
cout<<"---------------------"<<endl;
cout<<total<<endl;
}