#include <iostream>
#include <string>
#include <functional>
class Button {
private:
std::function<void()> onClick;
public:
// TODO: Implement setCallback
void setCallback(std::function<void()> cb) {
onClick = cb;
}
// TODO: Implement press()
void press() {
if(onClick){
onClick();
}
}
};
int main() {
Button btn;
btn.setCallback([](){std::cout << "Action Executed!" << std::endl;}); //directly passing the lambda function as a parameter
int N;
if (!(std::cin >> N)) return 0;
for (int i = 0; i < N; ++i) {
std::string cmd;
std::cin >> cmd;
if (cmd == "PRESS") {
btn.press();
}
}
return 0;
}
Input
2 PRESS IDLE
Expected Output
Action Executed!