Storing base and derived classes in a vector???

Discussion in 'C++' started by H, Jul 16, 2007.

  1. H

    H Guest

    I'm trying to wrap my head around inheritance. The following code
    creates three objects, an Animal, a Dog and a Cat. Dog and Cat are
    derived from Animal and overwrite Animal's birth() method.

    I thought, since Dog and Cat are Animals, I can make a vector of Animals
    and store Dogs and Cats in it. There's no problem until I try accessing
    the elements of the vector, where they are now all treated as Animals.

    I suppose this makes sense, since I created a vector of Animals, that
    all the elements in it should be treated as Animals. How do I store a
    Dog in an Animal vector and retrieve it as a Dog? Can I even do that?

    I'm not even sure of the terminology of what I'm trying to do, so my
    Google searches haven't been very successful. Even a hint of what to
    search for would be helpful.

    Thanks,

    JamesW.


    #include <iostream>
    #include <vector>

    class animal {
    public:
    animal(){};
    ~animal(){};
    virtual void birth(){
    std::cout << "Gave birth to an animal" << std::endl;
    }
    };

    class dog : public animal {
    public:
    void birth(){
    std::cout << "Gave birth to a puppy" << std::endl;
    }
    };

    class cat : public animal {
    public:
    void birth(){
    std::cout << "Gave birth to a kitten" << std::endl;
    }
    };

    int main()
    {
    animal thing;
    dog max;
    cat lucky;

    std::vector<animal>list;

    list.push_back(thing);
    list.push_back(max);
    list.push_back(lucky);

    for(unsigned char i=0;i< list.size(); i++){
    list.birth();
    }

    return 0;
    }

    Output::
    Gave birth to an animal
    Gave birth to an animal
    Gave birth to an animal
    H, Jul 16, 2007
    #1
    1. Advertising

  2. H wrote:
    > [..] I created a vector of Animals, that
    > all the elements in it should be treated as Animals. How do I store a
    > Dog in an Animal vector and retrieve it as a Dog? Can I even do that?
    >


    No, you cannot. Read about "slicing". Then read the FAQ (#34.4 is
    one I found relevant, I am sure there are others). Then read the
    archives for this newsgroup (search for "vector of derived" or some
    such).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 16, 2007
    #2
    1. Advertising

  3. H

    Tim Love Guest

    H <> writes:

    >I thought, since Dog and Cat are Animals, I can make a vector of Animals
    >and store Dogs and Cats in it.

    I think the trouble is that when you store them, they become animals.
    Try the following, which uses std::vector<animal*>list;




    #include <iostream>
    #include <vector>

    class animal {
    public:
    animal(){};
    ~animal(){};
    virtual void birth(){
    std::cout << "Gave birth to an animal" << std::endl;
    }
    };

    class dog : public animal {
    public:
    void birth(){
    std::cout << "Gave birth to a puppy" << std::endl;
    }
    };



    class cat : public animal {
    public:
    void birth(){
    std::cout << "Gave birth to a kitten" << std::endl;
    }
    };

    int main()
    {
    animal thing;
    dog max;
    cat lucky;

    std::vector<animal*>list;

    list.push_back(&thing);
    list.push_back(&max);
    list.push_back(&lucky);

    for(unsigned char i=0;i< list.size(); i++){
    list->birth();
    }
    return 0;
    }
    Tim Love, Jul 16, 2007
    #3
  4. H

    H Guest

    Victor Bazarov wrote:
    > H wrote:
    >> [..] I created a vector of Animals, that
    >> all the elements in it should be treated as Animals. How do I store a
    >> Dog in an Animal vector and retrieve it as a Dog? Can I even do that?
    >>

    >
    > No, you cannot. Read about "slicing". Then read the FAQ (#34.4 is
    > one I found relevant, I am sure there are others). Then read the
    > archives for this newsgroup (search for "vector of derived" or some
    > such).


    Thanks for the hints.

    JamesW
    H, Jul 16, 2007
    #4
  5. H

    terminator Guest

    On Jul 16, 6:13 pm, (Tim Love) wrote:
    > H <> writes:
    > >I thought, since Dog and Cat are Animals, I can make a vector of Animals
    > >and store Dogs and Cats in it.

    >
    > I think the trouble is that when you store them, they become animals.
    > Try the following, which uses std::vector<animal*>list;
    >
    > #include <iostream>
    > #include <vector>
    >
    > class animal {
    > public:
    > animal(){};
    > ~animal(){};
    > virtual void birth(){
    > std::cout << "Gave birth to an animal" << std::endl;
    > }
    >
    > };
    >
    > class dog : public animal {
    > public:
    > void birth(){
    > std::cout << "Gave birth to a puppy" << std::endl;
    > }
    >
    > };
    >
    > class cat : public animal {
    > public:
    > void birth(){
    > std::cout << "Gave birth to a kitten" << std::endl;
    > }
    >
    > };
    >
    > int main()
    > {
    > animal thing;
    > dog max;
    > cat lucky;
    >
    > std::vector<animal*>list;
    >
    > list.push_back(&thing);
    > list.push_back(&max);
    > list.push_back(&lucky);
    >
    > for(unsigned char i=0;i< list.size(); i++){
    > list->birth();
    > }
    > return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    using pointers and referrences is a good idea but you will be
    responsible for garbage collection(delete pointers)so define some
    smart pointer and go on.
    terminator, Jul 18, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Goudie
    Replies:
    6
    Views:
    454
    Victor Bazarov
    Jan 26, 2004
  2. CD
    Replies:
    2
    Views:
    795
    Victor Bazarov
    Oct 5, 2004
  3. call_me_anything

    vector <Base *> = vector <Derived *> ??

    call_me_anything, Jan 23, 2007, in forum: C++
    Replies:
    16
    Views:
    606
    Noah Roberts
    Jan 24, 2007
  4. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page