I have a parent class P, derived class D, and function show() in both of them. Also, i have an array of P objects, and in that array i am assigning objects derived from P, like D. And i'm calling show() function from array. But it seems that it calls only P empty function, not derived overriden function.
My Code:
//parent.h
class P
{
public:
P(){ }
~P(){ }
virtual void show(){ }
};
//derived.h
#include "parent.h"
class D : public P
{
public:
D(){ }
~D(){ }
void show();
};
//derived.cpp
#include "derived.h"
void D::show()
{
std::cout<<"Showing Derived Function\n";
}
//main.cpp
#include "derived.h"
#include <vector>
int main()
{
vector<P> objectz;
for(int i=0; i<8; i++)
{
D derp;
objectz.insert(objectz.begin(), derp);
}
for(int i=0; i<8; i++)
{
objectz[i].show(); //Call it Here !!
}
return 0;
}
v`)? Because then you need to read about [object slicing](http://stackoverflow.com/questions/274626/what-is-object-slicing). Please show how you use the classes, preferably you should create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us.
– Some programmer dude Apr 01 '15 at 16:37