How to Display Vector of Pointers to Abstract Objects

Hi guys,

I'm creating an animal board game where various animals are placed on the board.

I have an Animal class that is abstract.
I then have a Rabbit and a Turtle class which both derive from Animal.

All information about the animal needs to be stored in a vector of pointers to the Animal object.

One of the things I have to do is initialise the animal vector, which I think I have already done.

1
2
3
4
5
6
7
8
9
10
 int main()
{


    cout << "Welcome To Animals Life: A Game" << endl;
    vector<Animal*> animalList;

    animalList.push_back(new Rabbit());
    animalList.push_back(new Turtle());
}


All the information for the animals is loaded from a text file into the vector from another method.

My problem is that I'm unable to display the animals from the vector. I have a "displayAllAnimals" method, but whenever I attempt to display them all the values come up as 0. How do I correctly pass the values in the vector to different methods?

Here is what I attempted to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void displayAllAnimals(vector<Animal*> animalList) {

    string animalType;


    if (animalList.empty())
        cout << "vector is empty." << endl;
    else
    {
        for (int i = 0; i < animalList.size(); i++) {
            cout << animalList[i] << " ";
        }
        cout << "\n" << endl;


        for (int i = 0; i < (int)animalList.size(); i++)
        {
            if (animalType == "T") {
                cout << "Animal Type: Turtle" << endl;
                cout << "Animal ID: " << animalList[i]->getId() << "  " << endl;
                cout << "Animal Position: " << animalList[i]->getPosition() << "  " << endl;
                cout << "Animal Direction: " << animalList[i]->getDirection() << "  " << endl;
                cout << "Animal Size: " << animalList[i]->getSize() << "  " << endl;
                cout << "Animal Alive: " << animalList[i]->getAlive() << "  " << endl;
            }
            else {
                cout << "Animal Type: Rabbit" << endl;
                cout << "Animal ID: " << animalList[i]->getId() << "  " << endl;
                cout << "Animal Position: " << animalList[i]->getPosition() << "  " << endl;
                cout << "Animal Direction: " << animalList[i]->getDirection() << "  " << endl;
                cout << "Animal Size: " << animalList[i]->getSize() << "  " << endl;
                cout << "Animal Alive: " << animalList[i]->getAlive() << "  " << endl;
                cout << "Animal Hop Length: " << animalList[i]->getHopLength() << "  " << endl;
            }
        }
    }


}

Last edited on
You did not show the interface that your class hierarchy has. How could we tell how to use/improve it?
@keskiverto ... I don't know what you mean by that. Are you suggesting I show the animal classes?
Yes.

[edit]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Animal {
  virtual void print( std::ostream& ) const = 0;
};

class Unicorn {
  virtual void print( std::ostream& ) const;
};

std::ostream& operator<< ( std::ostream& out, const Animal& obj ) {
  obj.print( out );
  return out;
}

for ( auto ap : animalList ) {
  std::cout << *ap;
}
Last edited on
but whenever I attempt to display them all the values come up as 0
How do you initialize the member variables of those classes?
Topic archived. No new replies allowed.