Hardware Monitor

#include <iostream>
#include <string>

class TemperatureSensor {
private:
    int raw_adc_value;

    // TODO: Declare 'debugMonitor' as a friend function
    // friend void ...
    friend void debugMonitor(const TemperatureSensor& s);

public:
    TemperatureSensor() : raw_adc_value(0) {}

    void update(int val) {
        raw_adc_value = val;
    }
};

// TODO: Implement the global function debugMonitor
// It accepts 'const TemperatureSensor& s'
// It prints "Debug: Raw ADC = " << s.raw_adc_value
void debugMonitor(const TemperatureSensor& s) {
    // Placeholder to allow compilation (this will fail if not friended)
    std::cout << "Debug: Raw ADC = " << s.raw_adc_value << std::endl;
}

int main() {
    TemperatureSensor sensor;
    int N;
    if (!(std::cin >> N)) return 0;

    for (int i = 0; i < N; ++i) {
        std::string cmd;
        std::cin >> cmd;

        if (cmd == "READ") {
            int val;
            std::cin >> val;
            sensor.update(val);
        } else if (cmd == "DEBUG") {
            debugMonitor(sensor);
        }
    }
    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

3 READ 1024 DEBUG READ 2048

Expected Output

Debug: Raw ADC = 1024