125. Abstract Operation

You are given a base class Operation that represents a mathematical operation.

Your task is to declare a pure virtual function inside the Operation class so that it becomes an abstract base class.

Two derived classes already exist:

  • SquareOp → calculates the square of a number
  • CubeOp → calculates the cube of a number

The program will:

  1. Read an integer value
  2. Read an operation name ("square" or "cube")
  3. Dynamically create the corresponding derived object
  4. Invoke the operation using a base class pointer
  5. Print the computed result

This problem focuses on runtime polymorphism and abstract interfaces, which are commonly used in embedded and firmware systems.

Input Specification

  • One integer n
  • One string opName
    • Possible values: "square" or "cube"

Input is provided in a single line, separated by space.

Output Specification

  • Print a single integer value representing the result of the requested operation applied to n.

No extra spaces or newline characters are required.

Example 1

Input

5 square

Output

25

Example 2

Input

3 cube 

Output

27

Constraints

  • n is a valid 32-bit signed integer
  • Overflow behavior follows standard C++ int arithmetic
  • Exactly one valid operation name will be provided
  • Dynamic memory must be handled safely

 

 

 

 

Loading...

Input

5 square

Expected Output

25