+ 2

Count number of objects in C++?

Is there a way to count the number of objects in c++? For example, in this code right above the name of each person, ai want it to output "Person #2" or something like that. https://code.sololearn.com/c454ZOctiB5J/?ref=app I've been trying to figure this out for a while lol

3rd Mar 2019, 8:42 AM
Daniel Cooper
Daniel Cooper - avatar
2 Answers
+ 7
In the Human constructor: static int id = 1; // ...rest of code... id++; And you can now use the id integer in your code. You could also set an id property to each Human, if you want.
3rd Mar 2019, 8:53 AM
Rowsej
Rowsej - avatar
+ 2
Ooh it works Thanks I didn't think about using static because I entirely forgot about it's existence xD Now I'm trying to figure out a random 4 digit ID for each human
3rd Mar 2019, 5:33 PM
Daniel Cooper
Daniel Cooper - avatar