Home » C++ Programming » Questions and Answers » Question
  1. What is the output of this program?
    #include <iostream>
    #include <typeinfo>
    #include <exception>
    using namespace std;
    class BaseClass
    {
    virtual void f(){}
    };
    class DerivedClass : public BaseClass {};
    int main ()
    {
    try
    {
    BaseClass* ptr1 = new BaseClass;
    BaseClass* ptr2 = new DerivedClass;
    cout << typeid(*ptr1).name() << " ";
    cout << typeid(*ptr2).name();
    }
    catch (exception& excep)
    {
    cout << "Exception: " << excep.what() << endl;
    }
    return 0;
    }
    1. 9BaseClass
    2. 12DerivedClass
    3. 9 12
    4. Both 9BaseClass and 12DerivedClass
    5. None of these
Correct Option: D

In this program, We apply the typeid to the polymorphic class.



Your comments will be displayed only after manual approval.