60. Sensor Calibration Class

#include <iostream>
using namespace std;

class Sensor {
private:
    int rawValue;
    int offset;

public:
    Sensor() : rawValue(0), offset(0) {}

    void setRaw(int value) {
        rawValue = value;
    }

    void calibrate(int off) {
        offset = off;
    }

    int read() {
        return rawValue + offset;
    }
};

int main() {
    int raw, offset, newRaw;
    cin >> raw >> offset >> newRaw;

    Sensor s;
    s.setRaw(raw);
    s.calibrate(offset);
    s.setRaw(newRaw);

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

Explanation & Logic Summary

  • The Sensor class encapsulates raw sensor data and calibration parameters.
  • Direct access to sensor state is prevented using private members.
  • Calibration logic is applied internally and exposed via the read() function.
  • This structure mirrors how firmware drivers protect and process hardware data.

Firmware Relevance & Real-World Context

  • Many embedded sensors require calibration offsets (ADC inputs, temperature sensors, IMUs).
  • Firmware drivers typically store raw measurements and expose corrected values.
  • This problem reinforces encapsulation, controlled access, and clean driver-style APIs used in embedded systems.

 

 

 

 

Loading...

Input

100 -5 120

Expected Output

115