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

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)
{
	cout<<a;
	return a;
}
int func(int a)
{
	cout<<a;
}
int main(int argc, char const *argv[])
{
	int a = 5;
	int b = func(a);
	float c = func(5.5);
	return 0;
}
A. 5
B. 55.5
C. Error
D. Segmentation fault
 
Answer: b
Explanation: C++ allows to overload template functions with normal functions therefore the program does not gives any error. When program is executed then int b = func(a); calls the normal function whereas float c = func(5.5) calls the template function.
 

Tìm kiếm

Back
Top