#include <functional>
#include <iostream>
#include <array>
//We cannot touch this class
class Button
{
public:
virtual void onClick()
{
}
};
void click_button(Button& button)
{
button.onClick();
}
class CustomButton: public Button
{
const std::function<void(void)> callback;
public:
CustomButton(std::function<void(void)> cb) : callback(std::move(cb)) {}
virtual void onClick() override
{
callback();
}
};
void meow() {std::cout << "meow";}
void beep() {std::cout << "beep";}
void clack() {std::cout << "clack";}
int main()
{
std::array<CustomButton, 3> buttons {{{meow}, {beep}, {clack}}};
std::cout << "Which button do you want to press? (1-3)\n";
int i;
std::cin >> i;
std::cout << "Button " << i << ' ';
click_button(buttons[i-1]);
std::cout << "'s\n";
}
I2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFycmF5PgoKLy9XZSBjYW5ub3QgdG91Y2ggdGhpcyBjbGFzcwpjbGFzcyBCdXR0b24KewpwdWJsaWM6Cgl2aXJ0dWFsIHZvaWQgb25DbGljaygpCgl7CgoJfQp9OwoKdm9pZCBjbGlja19idXR0b24oQnV0dG9uJiBidXR0b24pCnsKCWJ1dHRvbi5vbkNsaWNrKCk7Cn0KCmNsYXNzIEN1c3RvbUJ1dHRvbjogcHVibGljIEJ1dHRvbgp7Cgljb25zdCBzdGQ6OmZ1bmN0aW9uPHZvaWQodm9pZCk+IGNhbGxiYWNrOwpwdWJsaWM6CglDdXN0b21CdXR0b24oc3RkOjpmdW5jdGlvbjx2b2lkKHZvaWQpPiBjYikgOiBjYWxsYmFjayhzdGQ6Om1vdmUoY2IpKSB7fQoKCXZpcnR1YWwgdm9pZCBvbkNsaWNrKCkgb3ZlcnJpZGUKCXsKCQljYWxsYmFjaygpOwoJfQp9OwoKdm9pZCBtZW93KCkge3N0ZDo6Y291dCA8PCAibWVvdyI7fQp2b2lkIGJlZXAoKSB7c3RkOjpjb3V0IDw8ICJiZWVwIjt9CnZvaWQgY2xhY2soKSB7c3RkOjpjb3V0IDw8ICJjbGFjayI7fQoKaW50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