76. GPIO Pin Operator Overload

Create a class GpioPin that simulates a single digital GPIO pin used in embedded systems.
The pin can hold only one of two logic levels:

  • LOW (0)
  • HIGH (1)

The pin state must be represented using a strongly typed enumeration.

enum class Level { LOW = 0, HIGH = 1 };

The class must support intuitive read and write semantics using operator overloading, similar to how GPIO abstractions are used in embedded firmware.

Functional Requirements

Your GpioPin class must support:

Writing to the pin using an overloaded assignment operator

pin = Level::HIGH;

Reading the pin using an overloaded type conversion operator

int value = pin;   // must return 0 or 1 

Program Behavior

Input format

initial_level new_level

Where:

  • initial_level is either 0 (LOW) or 1 (HIGH)
  • new_level is either 0 (LOW) or 1 (HIGH)

Execution Steps

  1. Construct a GpioPin object using initial_level
  2. Assign new_level using the overloaded operator=
  3. Print the final pin level as an integer (0 or 1)

 

Example 1

Input:

0 1

Execution Explanation:

  • Initial level: LOW
  • Write operation: HIGH

Output:

1

 

Example 2

Input:

1 0

Output:

0

 

Constraints

  • Must use enum class Level
  • Must overload:
    • operator=
    • operator int()
  • No GPIO direction handling (input/output)
  • No validation logic required beyond provided inputs
  • Logic must remain minimal and deterministic
  • No dynamic memory allocation

 

 

 

 

Loading...

Input

0 1

Expected Output

1