What will be the output of the following C++ code? [code] #include <iostream> using namespace std; template <class T> class Test { private:

What will be the output of the following C++ code?
Code:
#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.
2
1
B.
1
2
C.
1
1
D.
2
2
 
Answer: a
Explanation: In this as we have first used int to define the objects of class Test so count is increased to 2 for int types and declared the double object once therefore the constructor is called only once hence value of count is 1.
 

Tìm kiếm

Back
Top