MCQs > IT & Programming > C++ > Answer the question that follows. class Person { public: Person(); virtual ~Person(); }; class Student : public Person { public: Student(); ~Student(); }; main() { Person *p = new Student(); delete p; } Why is the keyword 'virtual' added before the Person destructor?

C++ MCQs

Consider the sample code given below and answer the question that follows.

 class Person
 {
 public:
    Person();
       virtual ~Person();
 };
 class Student : public Person
 {
 public:
    Student();
    ~Student();
 };
 main()
 {
    Person *p = new Student();
    delete p;
 }

 Why is the keyword "virtual" added before the Person destructor?

Answer

Correct Answer: To ensure that correct destructor is called when p is deleted

Explanation:

Note: This Question is unanswered, help us to find answer for this one

C++ Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it

search

C++ Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it