#include <iostream>
using namespace std;

class LEDController {
private:
    // Static member variable to store the LED state (0 = OFF, 1 = ON)
    // Static members must be initialized outside the class.
    static int state;

public:
    // Constructor: Called when the object is created
    // Sets the LED to ON
    LEDController() {
        state = 1;
    }

    // Destructor: Called when the object goes out of scope
    // Sets the LED to OFF (RAII principle)
    ~LEDController() {
        state = 0;
    }

    // Static member function to print the current state
    static void print() {
        cout << "LED=" << state << "\n";
    }
};

// Initialize the static member variable to 0 (LED OFF)
int LEDController::state = 0;

int main() {
    int x;
    if (!(cin >> x)) return 0;

    {
        // LEDController object created here (Constructor runs)
        LEDController led;

        if (x == 1) {
            LEDController::print();
        }
        // Object 'led' goes out of scope here (Destructor runs)
    }

    // Print state after destruction to verify it is OFF
    LEDController::print();
    
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

1

Expected Output

LED=1 LED=0