148. Shadowing Resolution

In clean code, we often want function parameters to have descriptive names (like speed or config). However, if a class has a member variable with the exact same name, the parameter "shadows" the member. Writing speed = speed; does nothing (it assigns the parameter to itself).

To fix this, we use the this pointer to explicitly refer to the member variable: this->speed = speed;.

Your task is to implement a class Timer.

  1. Private member: int period (initialized to 0).
  2. Method void setPeriod(int period):
    • The argument name MUST be period.
    • You must use this-> to assign the argument to the member.
  3. Method void log(): Print Timer Period: <period> ms.

Program Flow:

  1. Instantiate Timer.
  2. Read integer N.
  3. Loop N times.
  4. Read integer p.
  5. Call timer.setPeriod(p).
  6. Call timer.log().

Input Format:

  • First line: Integer N.
  • Next N lines: Integer p.
  • Input is provided via standard input (stdin).

Output Format:

  • Timer Period: <value> ms
  • Each output on a new line.

Example: 

Example 1

Input:

2
100
500

Output:

Timer Period: 100 ms
Timer Period: 500 ms

Constraints:

  • The argument of setPeriod must be named period (same as the member).
  • Must use this->period to resolve the conflict.

 

 

 

Loading...

Input

2 100 500

Expected Output

Timer Period: 100 ms Timer Period: 500 ms