11. Battery Monitor Class

#include <iostream>
#include <string>
using namespace std;

class Battery {
private:
    int voltage;
    int threshold;

public:
    Battery(int v, int t) : voltage(v), threshold(t) {}

    bool isLow() const {
        return voltage < threshold;
    }

    string getStatus() const {
        return isLow() ? "LOW" : "OK";
    }
};

int main() {
    int voltage, threshold;
    cin >> voltage >> threshold;

    Battery b(voltage, threshold);
    cout << b.getStatus();

    return 0;
}

 

Solution Details

  • Encapsulates voltage and threshold inside a class, keeping them private.
  • isLow() is a helper that checks if voltage falls below threshold.
  • getStatus() provides a user-friendly output string.

Significance for Embedded Developers: Battery monitoring is critical in low-power devices. This class models a simple threshold check, a common firmware pattern (e.g., checking if the battery is too low to transmit data or enable peripherals).
 

Loading...

Input

3000 3200

Expected Output

LOW