Predict the output?
#include <iostream>
using namespace std;
template <typename T>
void fun(const T&x)
{
static int count = 0;
cout << "x = " << x << " count = " << count << endl;
++count;
return;
}
int main()
{
fun<int> (1);
cout << endl;
fun<int>(1);
cout << endl;
fun<double>(1.1);
cout << endl;
return 0;
}
1 Crore+ students have signed up on EduRev. Have you? Download the App |
Output of following program?
#include <iostream>
using namespace std;
template <class T>
class Test
{
private:
T val;
public:
static int count;
Test() { count++; }
};
template<class T>
int Test<T>::count = 0;
int main()
{
Test<int> a;
Test<int> b;
Test<double> c;
cout << Test<int>::count << endl;
cout << Test<double>::count << endl;
return 0;
}
Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler.
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T, class U, class V=double>
class A {
T x;
U y;
V z;
static int count;
};
int main()
{
A<int, int> a;
A<double, double> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}
What may be the name of the parameter that the template should take?
What is the output of the program?
#include <iostream>
using namespace std;
template <int i>
void fun()
{
i = 20;
cout << i;
}
int main()
{
fun<10>();
return 0;
}
Output?
#include <iostream>
using namespace std;
template<int n> struct funStruct
{
static const int val = 2*funStruct<n-1>::val;
};
template<> struct funStruct<0>
{
static const int val = 1 ;
};
int main()
{
cout << funStruct<10>::val << endl;
return 0;
}
73 videos|7 docs|23 tests
|