Dynamic instantiation in C++-Collection of common programming errors
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 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.
if I add another fruit as strawberry in future, 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.