I want my code to be extensible, in a way where at runtime I create the objects.
For example, let's say I have a Grocery class which has an array of fruits and I want to fill this array of fruits with objects which derives from fruits.
class Fruit{
};
class Grocery{
std::vector<Fruit> m_fruits;
};
class Apple: Fruit{
};
class Pineapple: Fruit{
};
Now at runtime I want my Grocery class vector m_fruits
to be filled with class objects of Apple and Pineapple. So is it possible in some way.
The vector may store pointers. The point is not where the objects are stored or created but is it possible that in the future, if I add another fruit as strawberry, its object will be created and added to the vector of Grocery dynamically without changing the implementation of Grocery class?
Code help will be appreciated.