73. LED RAII Controller

Create a class LEDController that simulates controlling an LED on a microcontroller using RAII (Resource Acquisition Is Initialization) principles.

When an object of this class is created, the LED must turn ON.
When the object is destroyed (goes out of scope), the LED must turn OFF automatically.

The LED state must be stored in a static class member so that it can be read even after the object has been destroyed.

Class Requirements:

  • A private static integer state
    • 0 → LED OFF
    • 1 → LED ON
    • Initial value must be 0 (LED OFF)
  • Constructor
    • Sets state = 1 (LED ON)
  • Destructor
    • Sets state = 0 (LED OFF)
  • A static member function print() that outputs exactly:
    • LED=<state>
      

Program Behavior:

  • Read an integer x from standard input.
  • Input Constraint:
    x will be either 0 or 1 only.
    • 0 → do not print LED state inside the scope
    • 1 → print LED state inside the scope
  • Inside a scoped block { ... }:
    • Create a LEDController object.
    • If x == 1, print the LED state inside the block.
  • After the scoped block ends:
    • Print the LED state again.

The final print must reflect the destructor turning the LED OFF.

Example Timeline:

  • Enter block → constructor → LED ON
  • Exit block → destructor → LED OFF

 

Example 1

Input:

1

Output:

LED=1 LED=0 

 

Example 2

Input:

0

Output:

LED=0 

 

Explanation:

  • When x == 1, the LED state is printed while the object is alive inside the scope.
  • When x == 0, the LED state is not printed inside the scope.
  • After the scope ends, the object is destroyed and the LED is guaranteed to be OFF.
  • The final print always reflects the LED OFF state.

 

Constraints:

  • x{0, 1}
  • LED must always be OFF after object destruction.
  • LED state must be stored using a static member variable.
  • Output formatting must match exactly.

 

 

 

 

Loading...

Input

1

Expected Output

LED=1 LED=0