CJCoding With Joseph

Counting Objects With a Static Base Member

A static data member in a base class is shared across the whole hierarchy, so it can count every object created, including derived ones.

Create a base class Entity with a public static int count; that is incremented in the Entity constructor. Define the static member outside the class as int Entity::count = 0;.

Create a derived class Player (empty body) whose constructor automatically runs the base constructor and bumps the counter.

After the runner creates one Entity and two Players, it prints Entity::count:
3

Do NOT write a main function.

Expected Output:

3
Topics:
Inheritance
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.