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 7817809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:22:01+00:00 2026-06-02T06:22:01+00:00

/* Problem 50 */ #include <iostream> using namespace std; class a { char ach;

  • 0
/* Problem 50 */
#include <iostream>
using namespace std;

class a {
    char ach;
  public:
    a(char c) { ach = c - 1; }
    ~a(); // defined below
    virtual void out(ostream &os) {
      if ('m' < ach)
        os << ach << char(ach+7) << char(ach+6) << ' ';
      else
        os << ach << ach << ach;
    }
};

class b: public a {
    char bach;
  public:
    b(char c1, char c2) : a(c1) { bach = c2-1; }
    void out(ostream &os) {
      a::out(os);
      os << ' ' << bach << char(bach + 11);
    }
};

ostream &operator<<(ostream &os, a &x) {
  x.out(os);
  return os;
}

a::~a() {
  cout << *this; // calls above operator
}

int main() {
  b var1('n', 'e');
  a var2('o');
  cout << "Homer says: " << var1 << '\n';
  return 0;
}

I’m confuse why only two object being destruct while there are three object being construct

I also have put cout on each of the construct on the base_class and the derived_class to see how many were construct and I was right about the number of constructed object, but I was wrong when I did the destruct.

If anyone could please point me out why the last destruct didn’t apply to the first object that being create?

  • 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-02T06:22:03+00:00Added an answer on June 2, 2026 at 6:22 am

    when you are creating the first object in the main b var1('n', 'e'); which means that this object will be constructed by class b constructor b(char c1, char c2) : a(c1) and you are telling it to also use class a constructor which means that you have already have called 2 constructors in this case. and the last constructor is for this object a var2('o'); in this case you are using the constructor in class a. so in total you have used 2 constructors in class a and 1 constructor in class b.
    you have 2 objects and the reason why you see 2 objects being destructed is because you have a ~a() but you don’t have ~b().

    hope this will help

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

Sidebar

Related Questions

#include <iostream.h> using namespace std; class A { public: virtual char* func()=0; }; class
#include <iostream> using namespace std; class Marks { public: char* name(); }; char* Marks::name()
#include <iostream> using namespace std; class Street; class CrossStreet { private: char m_chName; Street*
I have the following problem: #include <vector> #include <iostream> using namespace std; class Mat
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
Suppose I have a class like this: #include <iostream> using namespace std; class Boda
#include <iostream> using namespace std; struct Term; struct Node; typedef Term* termPtr; typedef Node*
it's a basic program for array of pointer to objects. #include <iostream> using namespace
I have the following code: #include <iostream> #include Student.h <-- fixed the problem #include
What is the problem with the last two statements in the code? #include <iostream>

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.