2

I have:

class fruit
{
}

class apple:fruit
{
}

class pear:fruit
{
}

I want to create a function "addFruit" to add fruit into a vector of fruit. like:

vector<fruit> fruits;

How can I do it?

edited: I tried put a virtual function in base class. and do the really operation in subclass. But it failed. I got a lot of errors. SO I just need a clear thoughts and example code snippet.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
  • 3
    Studying some C++ fundamentals might help: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Fred Larson Apr 09 '11 at 02:13
  • 2
    "I tried", "it failed", and "I got a lot of errors" is no information. Show us what "I tried" looks like in code, and tell us how "it failed" and what "a lot of errors" were, and we can maybe help fix it. Otherwise, it's not a real question. (If you didn't have 100+ questions, I'd think you were looking for homework help - maybe you are?) – Ken White Apr 09 '11 at 02:35
  • Don't put a function "addFruit()" in your `fruit` base class. every fruit can have (e.g) a color, an average weight, and so on, but should not have a method "add me to some fruit vector". create such a method outside the `fruit` class (and outside every derived class) – Gian Paolo Aug 28 '17 at 13:32

2 Answers2

5

Hmmmm.....

vector<fruit*> fruits;

fruits.push_back(new apple());
fruits.push_back(new pear());

etc? is that what you are looking for?

snoofkin
  • 8,725
  • 14
  • 49
  • 86
5

To properly use inheritance you'd want to do something like this:

#include <iostream>
#include <vector>

class Fruit
{
public:
    virtual void Eat()
    {
        std::cout << "Mmm.. a fruit!" << std::endl;
    }
    virtual ~Fruit() {}
};

class Apple : public Fruit
{
public:
    void Eat()
    {
        std::cout << "Mmm.. an apple!" << std::endl;
    }
};

class Pear : public Fruit
{
public:
    void Eat()
    {
        std::cout << "Mmm.. a pear!" << std::endl;
    }
};

int main()
{
    std::vector<Fruit *> fruits;
    fruits.push_back(new Pear());
    fruits.push_back(new Apple());
    fruits.push_back(new Fruit());

    for (int i = 0; i < fruits.size(); i++)
        fruits[i]->Eat();

    return 0;
}

You need to use pointers to the base class (Fruit *) in order to take advantage of dynamic dispatch. Otherwise, it'll just call the Eat() method of Fruit.

The example in my answer allows for classes that derive from Fruit to override Eat() if they want, but it's not necessary. If you make the virtual function pure, then derived classes must implement it.

class Fruit
{
public:
    // Cannot instantiate this class, and derived classes must provide this
    virtual void Eat() = 0; 
};

Going off of Donotalo's comment, the functionality you want can be implemented as:

class FruitCollection
{
private:
    std::vector<Fruit *> fruits;

public:
    void Add(Fruit *fruit);
};

void FruitCollection::Add(Fruit *fruit)
{
    fruits.push_back(fruit);
}

This is probably overkill in most circumstances, and you'll probably need far more operations than this extremely simple example.

Mike Bailey
  • 12,479
  • 14
  • 66
  • 123
  • i need a function about addFruit. How can I do this?? – Josh Morrison Apr 09 '11 at 02:20
  • @Josh: What exactly are you trying to accomplish? Your question is rather vague. – Mike Bailey Apr 09 '11 at 02:21
  • I want a function. this function like addFruit(). It can be called in main(). the functionality is push back those fruit into vector. – Josh Morrison Apr 09 '11 at 02:25
  • The function you're asking for is one line.. All it is, is v.push_back(fruit); – Mike Bailey Apr 09 '11 at 02:27
  • @Josh, more OO approach would be to create a FruitBasket class which will contain addFruit() and also will encapsulate vector. Savvy? – Donotalo Apr 09 '11 at 02:42
  • The base class Fruit should have a virtual destructor to allow destruction of child classes through pointer to base class. Ie current design don't handle Fruit *p = new Pear; delete p; correctly. – Tobias Apr 09 '11 at 07:03
  • @Justin, @Tobias: Yes, this was a very simplistic example just to get the point across of how you would get polymorphic behavior. A more realistic implementation would be much more careful in those and other respects. – Mike Bailey Apr 09 '11 at 18:58