70. ADCChannel Constructor Validation

Create a class ADCChannel that represents an analog-to-digital converter (ADC) configuration on a microcontroller. The object must validate inputs inside the constructor, ensuring the channel and resolution are always safe and legal.

Details you must implement:

  • Private members:
    • int channel
    • int resolution
  • Constructor Requirements:
    • Create a constructor:
      • ADCChannel(int ch, int res)
    • It must enforce the following rules:
      • Valid channels are 0 to 15
        • If ch < 0, set channel = 0
        • If ch > 15, set channel = 15
      • Valid resolutions are 8, 10, or 12 bits
        • If any other value is provided, set resolution = 12 (safe default)
  • Public methods:
    • void print()
      • Should display exactly:
        • CH=<channel> RES=<resolution>

Program behavior (main()):

  • Read ch and res
  • Construct an ADCChannel object
  • Print its internal values

 

Example 1

Input:

3 10

Output:

CH=3 RES=10 

 

Example 2

Input (invalid channel and invalid resolution):

20 9

Example Output:

CH=15 RES=12 

Explanation:

  • 20 → clamped to 15
  • 9 → not in {8,10,12} → default = 12

 

Constraints:

  • You must validate channel and resolution inside the constructor.
  • Resolution must fall back to 12 when invalid.
  • Output must match exactly.

 

 

 

Loading...

Input

3 10

Expected Output

CH=3 RES=10