← 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(see Expected Output). Do NOT write a main function.
Expected Output:
3= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
Inheritance
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.