Evey object created by code is distinct and unique (unique address in memory, etc). The statement "Car CarOne;" creates an object of type Car, named CarOne. The constructor is invoked in that process at run time.
However, a constructor is not linked to any particular object. If you create n cars (n an integer) then the constructor is invoked separately to initialise each one. For example, if you do
Code:
// definition of the class Car
void some_function()
{
Car CarOne;
Car CarTwo;
// code here can do things with CarOne and CarTwo
}
a Car named CareOne is created (the constructor is invoked for it) then a Car named CarTwo (the constructor is invoked for it) whenever some code calls some_function(). Those two Car's continue to exist until some_function() returns. When some_function() returns, CarTwo is destroyed and then CarOne is destroyed (and the process of destroying each object invokes the destructor for that object).
If the user someone wants to arbitrarily say "create new unit of type X" then you need some code that interprets that instruction, and dynamically creates an object of the correct type. There are many ways of doing that. A simple way is, assuming you have the user input stored in an array of char, is.
Code:
#include <cstring>
Unit *Create_Unit(const char *user_input)
{
if (std::strcmp(user_input, "Car") == 0)
return new Car;
else if (std::strcmp(user_input, "Robot") == 0)
return new Robot;
// etc
}
The problem with this is that the caller must remember to delete the returned object when finished with it, or there will be a memory leak. The other problem is that the function above must be manually modified to specifically support every type of object needed - which is cumbersome and error prone when you have lots of object types.
There are more advanced techniques (eg look up the factory pattern) that take a bit more work to set up initially, but are easier to maintain in the long run.