Board-Independent Status Indicator

#include <iostream>
using namespace std;

/*
Select exactly ONE hardware board at build time.
*/
#define BOARD_TYPE_A
//#define BOARD_TYPE_B 


#if defined(BOARD_TYPE_A) && defined(BOARD_TYPE_B)
#error "Select only ONE board type"
#endif
#if !defined(BOARD_TYPE_A) && !defined(BOARD_TYPE_B)
#error "You must define a board type"
#endif

// Abstract interface used by application code
class StatusIndicator {
public:
    virtual void toggle() = 0;
    virtual bool isOn() const = 0;
    virtual ~StatusIndicator() = default;
};

/*
TASK FOR LEARNER:

- Create TWO board-specific implementations of StatusIndicator
- Select exactly one using conditional compilation
- Enforce compile-time errors for invalid selections
- Implement the factory function below

DO NOT modify main()
*/
#ifdef BOARD_TYPE_A
class BoardA_Status_Indicator : public StatusIndicator {
private:
    bool status;
public:
    BoardA_Status_Indicator():status(false){}
    void toggle() override {
        status = !status;
    }
    bool isOn() const override{
        return status;
    }
    ~BoardA_Status_Indicator(){}
};
#endif
#ifdef BOARD_TYPE_B
class BoardB_Status_Indicator : public StatusIndicator {
private:
    bool status;
public:
    BoardB_Status_Indicator():status(false){}
    void toggle() override {
        status = !status;
    }
    bool isOn() const override {
        return status;
    }
    ~BoardB_Status_Indicator(){}
};
#endif
// Factory function declaration (not implemented here)
StatusIndicator& getStatusIndicator(){
#ifdef BOARD_TYPE_A
    static BoardA_Status_Indicator boarda;
    return boarda;
#endif
#ifdef BOARD_TYPE_B
    static BoardB_Status_Indicator boardb;
    return boardb;
#endif
};

int main() {
    int n;
    cin >> n;

    StatusIndicator& indicator = getStatusIndicator();

    for (int i = 0; i < n; ++i) {
        int evt;
        cin >> evt;

        if (evt == 1) {
            indicator.toggle();
        }

        cout << (indicator.isOn() ? "STATUS=ON\n" : "STATUS=OFF\n");
    }

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

5 1 1 0 1 1

Expected Output

STATUS=ON STATUS=OFF STATUS=OFF STATUS=ON STATUS=OFF