Home » C++ Programming » Templates » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template<typename T>class Example
    {
    public:
    T var;
    Example(T k)
    {
    this->var = k;
    }
    void test()
    {
    cout << var << endl;
    }
    };
    class Child : public Example<char>
    {
    public:
    Child(): Example<char>( 0 )
    {
    }
    Child(char ch): Example<char>( ch )
    {
    }
    void test2()
    {
    test();
    }
    };
    int main()
    {
    Example <int> obj0( 50 );
    Child obj1( 'Z' );
    obj0.test();
    obj1.test();
    return 0;
    }
    1. 50
    2. Z
      50
    3. 50
      Z
    4. Z
    5. None of above
Correct Option: C

In this program, We are passing the values by using the template inheritance and printing it.



Your comments will be displayed only after manual approval.