140. Lazy Static Singleton

In firmware, we often need a single global object (like a PowerManager or Logger) accessible from anywhere. Using a raw global variable is dangerous because initialization order is undefined.

The C++ "Meyers' Singleton" pattern solves this using a Local Static Variable.

  1. The object is created the first time the function is called (Lazy Initialization).
  2. It guarantees the object is valid before it is used.
  3. It ensures only one instance exists for the entire program.

Your task is to implement a class SystemClock.

  1. Private Constructor: Prevent creating instances directly.
  2. Public Static Method getInstance():
    • It must define a static SystemClock instance.
    • It must return a reference to it.
  3. Public Member void tick(): Increments an internal counter.
  4. Public Member int getTime(): Returns the counter.

Program Flow:

  1. Call SystemClock::getInstance() to get a reference clk.
  2. Read integer N.
  3. Loop N times.
  4. Read string cmd.
  5. If cmd is "TICK", call clk.tick().
  6. If cmd is "SHOW", print Time: <value>.

Input Format:

  • First line: Integer N (1 to 20).
  • Next N lines: String cmd ("TICK" or "SHOW").
  • Input is provided via standard input (stdin).

Output Format:

  • For "SHOW": Time: <value>
  • Each output must be on a new line.

Example: 

Example 1

Input:

3
TICK
TICK
SHOW

Output:

Time: 2

Constraints:

  • The constructor must be private.
  • Must use a static local variable inside getInstance().

 

 

 

 

Loading...

Input

3 TICK TICK SHOW

Expected Output

Time: 2