Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class ParentClass
    {
    public:
    ParentClass(void)
    {
    cout << "Parent()\n";
    }
    ParentClass(int k)
    {
    cout << "Parent("<< k << ")\n";
    };
    ParentClass(void)
    {
    cout << "~Parent()\n";
    };
    };
    class ChildClass1 : public ParentClass{ };
    class ChildClass2 : public ParentClass
    {
    public:
    ChildClass2 (void)
    {
    cout << "Child2()\n";
    }
    ChildClass2 (int k) : ParentClass(k)
    {
    cout << "Child2(" << k << ")\n";
    }
    ~ChildClass2 (void)
    {
    cout << "~Child2()\n";
    }
    };
    int main (void)
    {
    ChildClass1 obj1;
    ChildClass2 obj2;
    ChildClass2 obj3(69);
    return 0;
    }
    1. Compilation Error
    2. Runtime Error
    3. Garbage value
    4. ParentClass()
      ParentClass()
      ChildClass2()
      ParentClass(69)
      ChildClass2(69)
      ~ChildClass2()
      ~ParentClass()
      ~Child2Class()
      ~ParentClass()
      ~ParentClass()
    5. None of these
Correct Option: A

In this program, We got an error in overloading because we didn’t invoke the destructor of parent.



Your comments will be displayed only after manual approval.