C++ template function Question

Introduction

Write a program that defines a template for a function that sums two numbers.

Numbers are of the same generic type T and are passed to function as arguments.

Instantiate the function in main() using int and double types:

You can use the following code structure.

#include <iostream> 

int main() 
{ 
    //your code here
} 


#include <iostream> 

template <typename T> 
T mysum(T x, T y) 
{ 
    return x + y; 
} 

int main() 
{ 
    int intresult = mysum<int>(10, 20); 
    std::cout << "The integer sum result is: " << intresult << '\n'; 
    double doubleresult = mysum<double>(123.456, 789.101); 
    std::cout << "The double sum result is: " << doubleresult << '\n'; 
} 



PreviousNext

Related