What will be the output of the following C++ code? [code] #include <iostream> using namespace std; template<class T = float,

Khang Lê

New member
What will be the output of the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    template<class T = float, int i = 5> class A
    {
        public:
        A();
        int value;
    };
    template<> class A<> 
    { 
        public: A(); 
    };
    template<> class A<double, 10>
    { 
        public: A(); 
    };
    template<class T, int i> A<T, i>::A() : value(i)
    {
        cout << value;
    }
    A<>::A() 
    {
        cout << "default";
    }
    A<double, 10>::A() 
    {
        cout << "10" << endl;
    }
    int main() 
    {
        A<int, 6> x;
        A<> y;
        A<double, 10> z;
    }
A. 6
B. 10
C. 6default10
D. 6default
 
Answer: c
Explanation: In this program, We are defining three templates and specializing it and passing the values to it and printing it.
Output:
Code:
$ g++ spec5.cpp
$ a.out
6default10
 

Tìm kiếm

Back
Top