Skip to content
Related Articles

Related Articles

C++ | Templates | Question 4
  • Difficulty Level : Medium
  • Last Updated : 16 Oct, 2013

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


(A)

0
0

(B)

1
1

(C)

2
1

(D)



1
0


Answer: (C)

Explanation: There are two classes created by the template: Test and Test.

Since count is a static member, every class has its own copy of it. Also, count gets incremented in constructor.

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 :