Templates
- Which of the things does not require instantiation?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.
- What is a template?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
A template is a formula for creating a generic class
- 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 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>
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.