derived class not over writing base class function - using vectors

Hello Everyone.

So I have a base class, lets call it base.
In base I have a virtual function called update(), update just couts "base"

then I have a class derived from base called derived;
it has a function called update(), update just couts "derived"

then I create a vector called Vec it's initialised like this:

 
std::vector<base> Vec;


then I add an element into it like this

1
2
Derived DerElement;
Vec.push_back(DerElement);


then when I type:
1
2
3
4
for (int i=0; i<Vec.size(); i++)
{
	Vec.at(i).Update();
}


it outputs:

base


and I know it works normally (without using vectors) cause then I tried this
1
2
Derived DerElement2;
DerElement2.Update();


and it outputs this:

derived


here is a full example of code that will generate this problem:
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
#include <iostream>
#include <vector>

class Base
{
public:
	virtual void Update()
	{
		std::cout << "Base" << std::endl;
	}
};

struct Derived : public Base
{
public:
	void Update()
	{
		std::cout << "Derived" << std::endl;
	}
};

int main()
{
	std::vector<Base> Vec;

	Derived DerElement;
	Vec.push_back(DerElement);

	for (int i=0; i<Vec.size(); i++)
	{
		Vec.at(i).Update();
	}

	Derived DerElement2;
	DerElement2.Update();
}


and this is it's output:

Base
Derived
Press any key to continue . . .


any help on this would be greatly appreciated, ThankYou.
Okay, turns out this has already been answered, silly me should've researched this more before posting a question.
apparently I was doing something called object slicing, well I fixed it now, and you can find the topic where it was solved right here:
http://stackoverflow.com/questions/21120285/derived-class-object-override-a-function-of-base-class-in-a-vector-of-objects
Topic archived. No new replies allowed.