Which is used to describe the function using placeholder types?
Which of the following provides the best description of an entity type?
1 Crore+ students have signed up on EduRev. Have you? Download the App |
What can be passed by non-type template parameters during compile time?
Which of the following is true about templates.
(1) Template is a feature of C++ that allows us to write one code for different data types.
(2) We can write one function that can be used for all data types including user defined types. Like sort(), max(), min(), ..etc.
(3) We can write one class or struct that can be used for all data types including user defined types. Like Linked List, Stack, Queue ..etc.
(4) Template is an example of compile time polymorphism.
Which of the following is incorrect in C++ ?
(1) When we write overloaded function we must code the function for each usage.
(2) When we write function template we code the function only once.
(3) It is difficult to debug macros.
(4) Templates are more efficient than macros.
Output of following program? Assume that the size of char is 1 byte and size of int is 4 bytes, and there is no alignment done by the compiler.
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T, class U>
class A {
T x;
U y;
static int count;
};
int main() {
A<char, char> a;
A<int, int> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}
Output of following program.
#include <iostream>
using namespace std;
template <class T, int max>
int arrMin(T arr[], int n)
{
int m = max;
for (int i = 0; i < n; i++)
if (arr[i] < m)
m = arr[i];
return m;
}
int main()
{
int arr1[] = {10, 20, 15, 12};
int n1 = sizeof(arr1)/sizeof(arr1[0]);
char arr2[] = {1, 2, 3};
int n2 = sizeof(arr2)/sizeof(arr2[0]);
cout << arrMin<int, 10000>(arr1, n1) << endl;
cout << arrMin<char, 256>(arr2, n2);
return 0;
}
Which is called on allocating the memory for array of objects?
Output?
#include <iostream>
using namespace std;
template <class T>
T max (T &a, T &b)
{
return (a > b)? a : b;
}
template <>
int max <int> (int &a, int &b)
{
cout << "Called ";
return (a > b)? a : b;
}
int main ()
{
int a = 10, b = 20;
cout << max <int> (a, b);
}
How many kinds of entities are directly parameterized in c++?
73 videos|7 docs|23 tests
|