69. PWM Constructor Modes

Create a class PWMController that simulates a simple PWM output channel on a microcontroller.
The goal is to practice constructor overloading, state initialization, and safe parameter handling in Embedded C++.

Details you must implement

  • Private members
    • int frequency — PWM frequency in Hz
    • int duty — duty cycle percentage (0–100)
    • bool enabled — PWM output state
  • Constructor 1: Basic Initialization
    • PWMController(int freq)
      • Sets frequency = freq
      • Sets duty = 0
      • Sets enabled = false
    • This constructor represents a PWM channel that is configured but not active.
  • Constructor 2: Full Initialization
    • PWMController(int freq, int dutyCycle)
      • Sets frequency = freq
      • Sets duty = dutyCycle, clamped to the range 0–100
      • Sets enabled = true
    • This constructor represents a PWM channel that is configured and enabled immediately.
  • Public methods
    • void setDuty(int d)
      • Updates the duty cycle
      • Duty must always be clamped to 0–100
    • void disable()
      • Sets enabled = false
    • void print()
      • Prints the current PWM state in the exact format:
        • F=<frequency> D=<duty> EN=<0 or 1>
          

Program flow (main())

  1. Read an integer mode
  2. If mode == 1
    • Read freq
    • Construct PWMController using Constructor 1
  3. If mode == 2
    • Read freq and duty
    • Construct PWMController using Constructor 2
  4. Read integer x
    • If x == -1, call disable()
    • Otherwise, call setDuty(x)
  5. Print the final PWM state using print()

 

Example 1 

Input:

1 
1000 
50 

Output:

F=1000 D=50 EN=0 

 

Example 2

Input:

2
2000 120
-1 

Example Output

F=2000 D=100 EN=0 

 

Constraints

  • Both constructors must be implemented
  • Duty cycle must always remain within 0–100
  • Output format must match exactly (spacing and order matter)

 

 

 

Loading...

Input

1 1000 50

Expected Output

F=1000 D=50 EN=0