In embedded drivers, notifying the application about hardware events (such as a button press) typically requires a callback mechanism. Legacy C implementations often rely on function pointers, which can be cumbersome. Modern C++ allows the use of std::function and lambda expressions to define callbacks inline, improving readability and maintainability.
Your task is to implement a class named Button that supports a callback mechanism using modern C++ features.
The Button class must:
std::function<void()> to store a callback.void setCallback(std::function<void()> cb) to register the callback.void press() that executes the stored callback only if it is valid.In the main function, you must register a lambda expression as the callback.
When invoked, this lambda must print:
Action Executed!
followed by a newline.
Program Flow
Button object."Action Executed!" followed by a newline.button.setCallback(...).N representing the number of commands.N times:"PRESS", call button.press().Input Format
N (1 ≤ N ≤ 20)N lines: A string command ("PRESS" or any other string)Input is provided via standard input (stdin).
Output Format
For each "PRESS" command, output:
Action Executed!
followed by a newline.
Example
Input
2
PRESS
IDLE
Output
Action Executed!
Constraints
<functional> header.main (no free functions).void().
Input
2 PRESS IDLE
Expected Output
Action Executed!