#include <iostream>
#include <string>
using namespace std;

class Operation {
public:
    // TODO: Declare a pure virtual function named apply

    virtual int apply(int x) const = 0;
};

class SquareOp : public Operation {
public:
    int apply(int x) const override {
        return x * x;
    }
};

class CubeOp : public Operation {
public:
    int apply(int x) const override {
        return x * x * x;
    }
};

int main() {
    int n;
    string opName;
    cin >> n >> opName;

    Operation* op = nullptr;

    if (opName == "square") {
        op = new SquareOp();
    } else if (opName == "cube") {
        op = new CubeOp();
    }

    if (op) {
        cout << op->apply(n);
        delete op;
    }

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5 square

Expected Output

25