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

  • Home
  • SEARCH
  • 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 8889997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:23:57+00:00 2026-06-14T22:23:57+00:00

#include <iostream> #include <string> using namespace std; class Person { string name; public: Person():name()

  • 0
#include <iostream>
#include <string>

using namespace std;

class Person
{
    string name;

    public:
        Person():name("")
        {
            cout << "Person default ctor\n";
        }
        Person(const string& name_in):name(name_in)
        {
            cout << "Person string ctor: " << name << "\n";
        }
        ~Person()
        {
            cout << "Person dtor: " << name << "\n";
        }
        string get_name()
        {
            return name;
        }
};

class Professor:public Person
{
    int office;

    public:
        Professor(const string& name_in, int office_in):Person(name_in), office(office_in)
        {
            cout << "Professor string ctor: " << get_name() << endl;
        }
        ~Professor()
        {
            cout << "Professor dtro: " << get_name() << endl;
        }
};

int main()
{
    Person alice("Alice");
    Professor bob("Bob", 10);

    return 0;
}

I would assume that the output should be:

Person string ctor: Alice

Person dtor: Alice

Professor string ctor: Bob

Professor dtor: Bob

Since it seems like that should logically follow from the structure of the program. However, the real output is:

Person string ctor: Alice

Person string ctor: Bob

Professor string ctor: Bob

Professor dtor: Bob

Person dtor: Bob

Person dtor: Alice

Can someone explain why this is? What am I not understanding about classes/constructors/destructors that’s making me come up with the wrong output?

  • 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-14T22:23:58+00:00Added an answer on June 14, 2026 at 10:23 pm

    First of all, Professor derives from Person. This means that every instance of Professor implicitly contains an instance of Person (observe how you’re calling Person(name_in) in Professor‘s constructor). This implicit instance will automatically get destroyed when the Professor object is being destroyed.

    This explains why you see:

    Person string ctor: Bob
    Person dtor: Bob
    

    As to the ordering of the destructor calls, the variables are destroyed in the reverse order of construction. This explains why Bob is destroyed before Alice:

    Person string ctor: Alice
    Professor string ctor: Bob
    Professor dtor: Bob
    Person dtor: Alice
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<iostream> #include<fstream> #include<string.h> using namespace std; class base { public: int lookup(char c);
#include <iostream> #include <set> using namespace std; class StudentT { public: int id; string
#include <iostream> #include <string> using namespace std; class phonebook { string name; string prefix;
I have the class Furniture with: Furniture.h: #include <iostream> #include <string> using namespace std;
I have this in furniture.h: #include <iostream> #include <string> using namespace std; class Furniture
Having a program like this: #include <iostream> #include <string> using namespace std; class test
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; class Dict {
#include <iostream.h> using namespace std; class A { public: virtual char* func()=0; }; class
The code in c++ is as follows: #include<iostream> #include<string> #include<set> using namespace std; class
consider the following : #include <iostream> #include <string> using namespace std; class A {

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.