One way to do this is by having a global variable which keeps count of the number of objects. This would be a global variable to the entire application. Global variables are not a good idea at all. Another approach would be to have a container class that has the above required class as a data member and thus keeps track of the number of objects declared. Of course , now creating an object of that type would require going through this container class and requesting a new object of that type which is not quite elegant.
Static variables are an alternative and perhaps even the preferred alternative.
Static variables do not belong to any one object as such but rather the entire group of objects. The code under , should give you an idea of how static variables and functions could be used to keep track of a count of the objects . Changes made by any of the object to this static variable is visible to the other objects of the same class.