Count Objects Using Static Variable
C++
Easy
3 views
Problem Description
Use static member variable to count total objects created of a class. This teaches static member concept.
Logic: Static variable is shared by all objects
Official Solution
class Counter {
public:
static int count;
Counter() {
count++;
}
};
int Counter::count = 0;
void question5_static_count() {
Counter c1, c2, c3;
cout << "Total objects created: " << Counter::count << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!