Open In App

C++ | Templates | Question 6

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;
}

(A)

16
24

(B)



8
16

(C)

20
28

(D) Compiler Error: template parameters cannot have default values.

Answer: (A)
Explanation: templates can also have default parameters. The rule is same all default values must be on the rightmost side.



Since count is static, it is not counted in sizeof.
Quiz of this Question

Article Tags :