3. Private Access

#include <iostream>
using namespace std;

class Sensor {
private:
    int value;
public:
    void setValue(int v) { value = v; }
    int getValue() { return value; }
};

int main() {
    Sensor s;
    s.setValue(75);
    cout << "Sensor value: " << s.getValue();
    return 0;
}

 

Solution Explanation

The variable is private, so outside code can’t modify it directly. Setters and getters provide controlled access.

 

Layman’s Terms

It’s like a thermometer — you can only see the reading, not change the mercury inside directly.

Significance for Embedded Developers

Encapsulation is crucial in drivers — you don’t expose registers directly, you give safe APIs like setBaudRate() or getTemperature().

Loading...

Input

Expected Output

Sensor value: 75