Im new to programming sorry if the terms im using are wrong.
I have to create a console app where the use can add, modify, print and delete food items.
I made a class for my food
class food{
private:
bool isBasic;
nmfvg MeatOrOther;
string name;
public:
food::food(){
isBasic=true;
MeatOrOther = NONE;
name = "";
}
food::food(string _name,bool _isBasic, nmfvg _MeatOrOther){
isBasic=_isBasic;
MeatOrOther=_MeatOrOther;
name = _name;
}};
and im putting the food the user is making into an array of type food.
food foods[100];
food temp("food1",true,VEG);
foods[0]=temp;
1-Is this the right way for me to store foods? 2-If it is how do i go about printing the name of foods[0]?
It's ok, but probably vector
will be better. You can print name
, by adding accessor
to name
class food {
//
public:
string get_name() const { return name; }
};
std::cout << foods[0].get_name() << std::endl;
or by use some function/ operator <<
for output, or by making name
public member and then simply use
std::cout << foods[0].name << std::endl;
As a better practice, you can keep the food objects in a std::vector instead of array like:
std::vector<food> foods;
foods.push_back(temp("food1",true,VEG));
To print name member, you may need a get method as public:
std::string food::GetName()
{
return name;
}
Then you can print the name by:
std::cout<<foods[0].GetName();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.