Specialization(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option C. -> 6default10In this program, We are defining three templates and specializing it and passing the values to
it and printing it.
Output:
$ g++ spec5.cpp
$ a.out
6default10
Explanation:-
Answer: Option A. -> templateIn this program, We are computing the result in the specalized block of the program.
Output:
$ g++ spec3.cpp
$ a.out
template
Explanation:-
Answer: Option A. -> templateAnswer:d
Explanation:In this program, We are passing the value of specified type and printing it by specialization.
Output:
$ g++ spec2.cpp
$ a.out
1
1
1.2
Explanation:-
Answer: Option C. -> 2.1: 378.228In this program, We specifed the type in the template function. We need to compile this program by adding -std=c++0x.
Output:
$ g++ -std=c++0x spec1.cpp
$ a.out
2.1: 378.228
Explanation:-
Answer: Option B. -> constructorWhen you allocate memory for an array of objects, the default constructor must be called to
construct each object. If no default constructor exists, you’re stuck needing a list of pointers
to objects.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
template
4.
inline T square(T x)
5.
{
6.
T result;
7.
result = x * x;
8.
return result;
9.
};
10.
template
11.
string square(string ss)
12.
{
13.
return (ss+ss);
14.
};
15.
int main()
16.
{
17.
int i = 2, ii;
18.
string ww("A");
19.
ii = square(i);
20.
cout
Explanation:-
Answer: Option A. -> 2:4AATemplate specialization is used when a different and specific implementation is to be used for a
specific data type. In this program, We are using integer and character.
Output:
$ g++ spec.cpp
$ a.out
2:4AA