What will be the output of the following C++ code? [code] #include <iostream> #include <string> #include <cstdlib> using namespace std; template

Lan Anh

New member
What will be the output of the following C++ code?
Code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
T func(T a)
{
	return a;
}
template<class T, class U>
T func(U a)
{
	return (T)a;
}
int main(int argc, char const *argv[])
{
	int a = 5;
	int b = func(a);
	int c = func(5.5);
	cout<<b<<c<<endl;
	return 0;
}
A. 5
B. 55
C. Error
D. Segmentation fault
 
Answer: b
Explanation: C++ allows such overlaoding of template functions therefore the program does not gives any error. The first template function returns the same parameter, whereas second template function returns type casted value.
 

Tìm kiếm

Back
Top