Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:35:17+00:00 2026-06-18T12:35:17+00:00

class Person { public: Person(std::string& name,long id) : name_(name), id_(id) {} ~Person() {} private:

  • 0
class Person
{
public:
    Person(std::string& name,long id) : name_(name), id_(id) {}
    ~Person() {}
private:
    std::string& name_;
    long id_;
};

class Student : public Person
{
public:
    Student(std::string& name, long id) : Person(name,id) {}
    Student(std::string& name, long id, const std::vector<int*>& courses) : Person(name,id), courses_(courses) {}
    virtual ~Student() {
        std::vector<int*>::iterator it;
        for (it = courses_.begin(); it != courses_.end(); ++it)
        delete (*it);
    }
    const std::vector<int*>& getCourses() const { return courses_; }
private:
    std::vector<int*> courses_;
};

int main(){
    std::string name("Rina");
    std::vector<int*> courses;
    courses.push_back(new int(1345));
    Person* p = new Student(name,37,courses);
    Student* s = (Student*)p;
    std::cout << (*(s->getCourses().at(0)));
    delete p;
}

I understand that the

delete p;

makes us realize the problem that

~Person()

is not virtual.
My question is: why should

~Person() 

be virtual?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:35:18+00:00Added an answer on June 18, 2026 at 12:35 pm

    For the same reason that you make other methods virtual.

    Person *p;
    ...
    
    delete p;
    

    wouldn’t handle subclasses of Person properly if the destructor wasn’t virtual. i.e. it would simply invoke the Person component and not the subclass component (e.g. Employee)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code: class Address{ private: std::string street; int house; public: Address(std::string s,
#include <iostream> #include <string> using namespace std; class Person { string name; public: Person():name()
I have a class: class Person{ public: Person(); ~Person(); string name; int* age; };
Very basic Model: public class Person { public string Name; public int Age; }
as for the definition of Person: public class Person { private int id; private
Given the classes: public class Person { public string Name { get; set; }
#include <string> using namespace std; class PersonList { private: char aName[7]; int aBribe; PersonList
class Person { public string name; public void SetName(string name) { name = name;
A class: public class Person { public string Title; public string Name; public Int32
public class Person { public int Id { get; set; } public string FirstName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.