2
/* 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?

4 Answers 4

6

There are only two objects being constructed, the 3 couts you see in the constructor is because of the fact that when you create a derived class object the base class constructor is called. As a side note, you need to declare class a destructor as virtual.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the information!. Actually this is from my professor for practicing for our exam.
2

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

1 Comment

Thanks for the deep answer, and I've just tried created ~b() and yes it was call for another destruct!
1

If Multiple objects are being created in nesting then they will be destroyed in the opposite manner they have been created.. example.... if class1 object is created first then inside this class2 and then inside that class3.. then class 3 object will be destroyed first and so on... so try to look whether your flow is going correct....

Comments

1

I see only two objects being constructed in the main() function. one is var1 and other is var2. While the objects are passed by reference, so no case of a copy of any object being constructed. So you should be seeing only 2 objects. Since one of the object is a derived class, so it's natural to see the constructor print statements for both derived class and base class constructors. You haven't defined a destructor for derived class, hence it's not printing it. I think you are misreading the number of print statements as the number of objects, which is not true for the derived class. For calrity, also print the address of "this" in all the print statements of your constructor and destructor. See the address and verify that there are only two objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.