How to access a derived class' function from a base

I am trying to figure out how to pass a group of classes with the same base class to a vector. I figured out how to pass them through their BASE class, but I can't get them to access the derived class functions.

This is hard to explain, the example below shows what I'm trying to do. What I want the output to be:
A dinosaur
A lizard
A dinosaur

What I'm actually getting as output:
A generic reptile
A generic reptile
A generic reptile

Any help would be appreciated.

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
40
41
#include <iostream>
#include <vector>

class reptile {
public:
	int age;
	std::string toString() { return "A generic reptile."; }
};

class dinosaur : public reptile {
public:
	std::string toString() { return "A dinosaur"; }
};

class lizard : public reptile {
public:
	std::string toString() { return "A lizard"; }
};


int main()
{
	std::vector<reptile> rep;

	dinosaur larry; larry.age = 10;
	lizard curly; curly.age = 4;
	dinosaur moe; moe.age = 11;

	rep.push_back(larry);
	rep.push_back(curly);
	rep.push_back(moe);

	for (int i=0; i<rep.size(); i++)
		std::cout << rep[i].toString().c_str() << std::endl;


	std::cout << std::endl << "Press any key to quit...";
	std::cin.get();
	return 0;
}
The topic you are talking about is polymorphism.
You want to invoke the correct function of the derived class from a vector of base-class elements.
The problem is, that your vector holds the Objects themselves in the vector.
When adding an element of a derived class the rest of the class is just cut-off so all your elements are actually reptiles then.

To use polymorphsim you have to use pointers and virtual functions, so to solve your problem you have to do these things:
1. use pointers to your objects.
2. declare the toString() function in reptile virtual
3. ???
4. profit.

To learn more about polymorphism you should look around somewhere else in the internet or in a book of your choise, it would be to much to go in depth here.
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
#include <iostream>
#include <vector>

class reptile {
public:
	int age;
	virtual std::string toString() { return "A generic reptile."; }
};

class dinosaur : public reptile {
public:
	std::string toString() { return "A dinosaur"; }
};

class lizard : public reptile {
public:
	std::string toString() { return "A lizard"; }
};


int main()
{
	std::vector<reptile*> rep;

	dinosaur* larry = new dinosaur; larry->age = 10;
	lizard* curly = new lizard; curly->age = 4;
	dinosaur* moe = new dinosaur; moe->age = 11;

	rep.push_back(larry);
	rep.push_back(curly);
	rep.push_back(moe);

	for (int i=0; i<rep.size(); i++)
		std::cout << rep[i]->toString().c_str() << std::endl;

	return 0;
}
Thank you so much for the help. It works as intended. And yes, I will look more into polymorphism.
Topic archived. No new replies allowed.