#include <iostream>
using namespace std;
class Device {
private:
int id;
static int count; // static variable shared by all objects
public:
Device(int id_) : id(id_) {
count++; // increment when object is created
}
static int getCount() {
return count;
}
};
// static member is also required to declare out of class
int Device::count = 0;
int main() {
Device d1(1);
Device d2(2);
Device d3(3);
cout << "Total devices created: " << Device::getCount();
return 0;
}
Solution Details
👉 In simple words:
A normal variable is like each device keeping its own personal notebook.
A static variable is like one shared whiteboard on the wall — every device adds to the same count.
Significance for Embedded Developers
Input
Expected Output
Total devices created: 3