Function that counts the number of instances of a class

Write a Function that counts the number of instances of a class?

How can you extended this so that you count only the number of instances of itself and not its base or derived classes?