#include <iostream>
#include <string>
using namespace std;
int square(int x) { return x * x; }
int cube(int x) { return x * x * x; }
void apply(int x, int (*op)(int)) {
cout << op(x);
}
int main() {
int n;
string opName;
cin >> n >> opName;
if (opName == "square") {
apply(n, square);
} else if (opName == "cube") {
apply(n, cube);
}
return 0;
}Explanation & Logic Summary:
apply doesn’t care which function it gets — it just calls the pointer.square or cube to apply lets you reuse the same logic.👉 In simple words:
Think of apply as a box where you put both a number and an instruction (square or cube). The box then gives you the result.
Firmware Relevance & Real-World Context:
Input
5 square
Expected Output
25