โ Back to Inheritance
Question 449
Counting Objects With a Static Base Member
Counting Objects With a Static Base Member
Astaticdata member in a base class is shared across the whole hierarchy, so it can count every object created, including derived ones. Create a base classEntitywith a publicstatic int count;that is incremented in theEntityconstructor. Define the static member outside the class asint Entity::count = 0;. Create a derived classPlayer(empty body) whose constructor automatically runs the base constructor and bumps the counter. After the runner creates oneEntityand twoPlayers, it printsEntity::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.