Track Number of Objects

Understand how to track the number of objects created from a Sample class.

Challenge

Write a program to track the number of objects that are created from a class Sample. Define a getCount() member function in it, with which we can get at any time the current count of objects that have been created from the Sample class.

Coding exercise

You have to declare a count variable. Define the constructor and a getCount() method in the code below. Try to solve this challenge on your own. If you can’t solve it, you can look at the solution and explanation below.

Get hands-on with 1200+ tech skills courses.