I am trying to create an array of objects whose class' all inherit from a Base class. Since I am just learning C++ after using Python, this was my first encounter with object slicing. From what I have read I should use pointers to the objects instead of instances of the objects themselves, but I can't seem to make it work.
For example, why does this program output: 1 1 instead of: 1 2
#include <iostream>
class A{
public:
int getNumber(){
return(1);
}
};
class B : public A{
public:
int getNumber(){
return(2);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A* list[2];
list[0] = new A();
list[1] = new B();
std::cout << list[0]->getNumber() << std::endl;
std::cout << list[1]->getNumber() << std::endl;
int x;
std::cin >> x;
return 0;
}