grouping different kinds of food

N

news.hku.hk

I would like to group different kinds of food.
There are unknown number of kinds of food e.g. apple, orange, lemon, melon,
.........
of course it's only a finite number but i don't know.
also suppose apple will have different price for different objects.

i have a class food such that:

food[1].showname(); //return string apple
food[2].showname(); // return string orange
food[3].showname(); //return string lemon
food[4].showname(); //return string apple
food[5].showname(); //return string orange
//some more up to food[100].showname();

food[1].showprice(); //return integer value 13
food[2].showprice(); // return integer value 21
food[3].showprice(); //return integer value 19
food[4].showprice(); //return integer value 20
food[5].showprice(); //return integer value 11
//some more up to food[100].showprice();

what i want to do is to output the total price for different food like this:

apple: 23
orange: 32
lemon: 19

could you tell me how to do this ??

Thanks a lot
Regards,
Billy
 
J

JKop

class Fruit
{
public:

virtual char* GetName(void) const = 0;
virtual unsigned short int GetPrice(void) const = 0;
};


class Apple : public Fruit
{
public:

virtual char* GetName(void) const
{
return "Apple";
}

virtual unsigned short int GetPrice(void) const
{
return 23;
}
};

class Orange : public Fruit
{
public:

virtual char* GetName(void)
{
return "Orange";
}

virtual unsigned short int GetPrice(void)
{
return 17;
}
};




void DisplayFruitInfo(Fruit* f);


int main(void)
{
Apple a;
Orange b;

DisplayFruitInfo(&a);
DisplayFruitInfo(&b);

}


void DisplayFruitInfo(Fruit* f)
{
cout << "Name: " << f.GetName();
<< "\r\nPrice: " << f.GetPrice() << "\r\n";
}



That what you're looking for?
 
N

news.hku.hk

just to add some points,
food[6].showname(); //may return banana
food[7].showname(); //may return pear
and so on ..........
i mean, we don't know how many kinds of food are there and their total
amount. So it's not possible to inherit the class with "known" names.
Thanks for your attention.
 
J

John Harrison

news.hku.hk said:
I would like to group different kinds of food.
There are unknown number of kinds of food e.g. apple, orange, lemon, melon,
........
of course it's only a finite number but i don't know.
also suppose apple will have different price for different objects.

i have a class food such that:

food[1].showname(); //return string apple
food[2].showname(); // return string orange
food[3].showname(); //return string lemon
food[4].showname(); //return string apple
food[5].showname(); //return string orange
//some more up to food[100].showname();

food[1].showprice(); //return integer value 13
food[2].showprice(); // return integer value 21
food[3].showprice(); //return integer value 19
food[4].showprice(); //return integer value 20
food[5].showprice(); //return integer value 11
//some more up to food[100].showprice();

These are bad names. The functions are returning information, not showing
it. get_name and get_price would be better.
what i want to do is to output the total price for different food like this:

apple: 23
orange: 32
lemon: 19

could you tell me how to do this ??

Thanks a lot
Regards,
Billy

You need a map, look it up, they are extremely useful. But here's some code
to get you started.

map<string, int> price_totals;
for (int i = 1; i < = 100; ++i)
{
price_totals[food.showname()] += food.showprice();
}
for (map<string, int>::const_iterator i = price_totals.begin(); i !=
price_totals.end(); ++i)
{
cout << i->first << ": " << i->second << '\n';
}

john
 
K

Karthik

news.hku.hk said:
I would like to group different kinds of food.
There are unknown number of kinds of food e.g. apple, orange, lemon, melon,
........
of course it's only a finite number but i don't know.
also suppose apple will have different price for different objects.

i have a class food such that:

food[1].showname(); //return string apple
food[2].showname(); // return string orange
food[3].showname(); //return string lemon
food[4].showname(); //return string apple
food[5].showname(); //return string orange
//some more up to food[100].showname();

food[1].showprice(); //return integer value 13
food[2].showprice(); // return integer value 21
food[3].showprice(); //return integer value 19
food[4].showprice(); //return integer value 20
food[5].showprice(); //return integer value 11
//some more up to food[100].showprice();

what i want to do is to output the total price for different food like this:

apple: 23
orange: 32
lemon: 19

could you tell me how to do this ??

Thanks a lot
Regards,
Billy

Probably you might be interested in an array of pointers.

food[1]->showname();
food[2]->showname();

...


typedef unsigned int UINT;

class Food {

virtual void showname() = 0;
// showing the name as such doesnt make sense
virtual UINT showprice() = 0;
// For the same reason, make this a pure virtual function
};


Inherit from this and write separate implementations for apple, orange,
strawberry, (cranberry , whatever !! ) .
That should make things easy for anyone who wants to read your code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top