Templates
- How many types of templates are there in c++?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
There are two types of templates. They are function template and class template.
- What is the output of this program?
#include <iostream>
using namespace std;
template <class T>
inline T square(T p)
{
T result;
result = p * p;
return result;
};
template <>
string square<string>(string str)
{
return (str+str);
};
int main()
{
int k = 4, kk;
string bb("A");
kk = square<int>(k);
cout << k << kk;
cout << square<string>(bb) << endl;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are using two template to calculate the square and to find the addition.
- What is the output of this program?
#include <iostream>
using namespace std;
template <typename T, typename U>
void squareAndPrint(T p, U q)
{
cout << p << p * p << endl;
cout << q << " " << q * q << endl;
};
int main()
{
int kk = 3;
float LL = 3.12;
squareAndPrint<int, float>(kk, LL);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this multiple templated types, We are passing two values of different types and producing the result.
- What is the output of this program?
#include <iostream>
#include <string>
using namespace std;
template<typename T>
void Print_Data(T output)
{
cout << output << endl;
}
int main()
{
double var = 6.25;
string str("Hello Interview Mania");
Print_Data( var );
Print_Data( str );
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are passing the value to the template and printing it in the template.
- What is the output of this program?
#include <iostream>
using namespace std;
template<typename type>
type Maximum(type num1, type num2)
{
return num1 > num2 ? num1:num2;
}
int main()
{
int Res;
Res = Maximum(150, 250);
cout << Res << endl;
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We are returning the maximum value by using function template.