Home » C++ Programming » Templates » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template<typename T>
    inline T square(T num1)
    {
    T Res;
    Res = num1 * num1;
    return Res;
    };
    int main()
    {
    int k, kk;
    float num1, num2;
    double num3, num4;
    k = 3;
    num1 = 2.3;
    num3 = 4.2;
    kk = square(k);
    cout << k << " " << kk << endl;
    num4 = square(num3);
    cout << num3 << " " << num4 << endl;
    }
    1. 3 9
      4.2 17.64
    2. 4.2 17.64
    3. 4.2 17.64
      3 9
    4. Compilation Error
    5. None of these
Correct Option: A

In this program, We are passing the values and calculating the square of the value by using the function template.



Your comments will be displayed only after manual approval.