Templates


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <class type>
    class Example
    {
    public:
    Example();
    ~Example();
    type Data(type);
    };
    template <class type>
    type Example<type>::Data(type Value0)
    {
    return Value0;
    }
    template <class type>
    Example<type>::Example()
    {
    }
    template <class type>
    Example<type>::~Example()
    {
    }
    int main(void)
    {
    Example<char> Value1;
    cout << Value1.Data('L') << endl;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

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


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <class T>
    class N
    {
    public:
    N(int p): q(p) {}
    protected:
    int q;
    };
    template <class T>
    class M: public N<char>
    {
    public:
    M(): N<char>::N(50)
    {
    cout << q * 3 << endl;
    }
    };
    int main()
    {
    M<char> test;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

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



  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. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

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


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class classA
    {
    public:
    virtual ~classA(){}
    protected:
    char p;
    public:
    char getChar();
    };
    class classB : public classA
    {
    public:
    void printChar();
    };
    void classB::printChar()
    {
    cout << "False" << endl;
    }
    int main()
    {
    classB ch;
    ch.printChar();
    return 1;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    In this program, We are passing the values and inheriting it to the other class and printing the result.



  1. How many bits of memory needed for internal representation of class?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.