Skip to content
Related Articles

Related Articles

C++ | Templates | Question 6
  • Last Updated : 16 Oct, 2013

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

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :