Skip to content
Related Articles

Related Articles

Improve Article

C++ | Templates | Question 4

  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021

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

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :