67. SensorConfig Initialization

#include <iostream>
using namespace std;

class SensorConfig {
private:
    int id;
    int offset;
    int lastValue;

public:
    // Parameterized constructor enforces valid initialization
    SensorConfig(int sensorId, int calibrationOffset)
        : id(sensorId), offset(calibrationOffset), lastValue(0) {}

    void update(int raw) {
        lastValue = raw + offset;
    }

    int read() {
        return lastValue;
    }
};

int main() {
    int id, offset;
    cin >> id >> offset;

    SensorConfig cfg(id, offset);

    int r1, r2;
    cin >> r1 >> r2;

    cfg.update(r1);
    cfg.update(r2);

    cout << cfg.read();
    return 0;
}

Explanation & Logic Summary

  • The sensor configuration is enforced at construction time
  • lastValue is always initialized to a known safe state
  • Calibration offset is consistently applied on every update
  • The final reading reflects the most recent sensor data

Firmware Relevance & Real-World Context

  • Embedded drivers often require configuration at startup
  • Sensors commonly use calibration offsets stored in firmware
  • Prevents use of uninitialized hardware configurations
  • Demonstrates safe constructor-based initialization patterns used in firmware drivers

 

 

 

 

Loading...

Input

10 3 20 25

Expected Output

28