Member Function Templates

Medium Templates & Generic Programming
Solve in Playground →

Problem Statement

Create a standard non-template class `MathOp`. Inside it, define a member function template `template T square(T val)` that returns `val * val`. Read a float from input, call `square()` using the `MathOp` object, and print ‘Result: [val]’.

Input Format

A single float.

Output Format

Print the formatted result.

Constraints

None

Sample Input

1.5

Sample Output

Result: 2.25

Explanation

You don't need to make the entire class a template. Individual methods inside regular classes can be templated.

Starter Code

#include <iostream>

class MathOp {
public:
    // Define template member function square() here
};

int main() {
    // Instantiate MathOp, read float, and print square
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used