?
Solved

Elementary question: Derived class and base class

Posted on 2005-04-08
1
Medium Priority
?
349 Views
Last Modified: 2010-04-01
#include <iostream.h>

class Base1
{
public:
      Base1()
      {
      cout<< "in ctor Base1\n";
      }
~Base1()
      {
      cout<< "in dtor Base1\n";
      }

};

class Base11:public Base1
{

public:
      Base11()
{
      cout<< "in ctor Base11\n";
}
~Base11()
      {
      cout<< "in dtor Base11\n";
      }
};

class Base21:public Base11
{
public:
      Base21()
{
      cout<< "in ctor Base21\n";
}
~Base21()
      {
cout<< "in dtor Base21\n";
      }
};

int main()
{
      Base1 base;
      Base11 base1;
      Base21 base2;

}

The output is
in ctor Base1
in ctor Base1
in ctor Base11
in ctor Base1
in ctor Base11
in ctor Base21
in dtor Base21
in dtor Base11
in dtor Base1
in dtor Base11
in dtor Base1
in dtor Base1
Press any key to continue

How do dervived classes Base11 and Base21 know about the base class Base1? Where is this information stored??

Regards,
INK777
0
Comment
Question by:ink777
1 Comment
 
LVL 9

Accepted Solution

by:
_ys_ earned 150 total points
ID: 13734828
The compiler simply augments your constructors to read:

class Base11:public Base1
{

public:
     Base11()
{
     Base1::Base1( );  // <--- base class constructor inserted *before* any custom code

     cout<< "in ctor Base11\n";
}
~Base11()
     {
     cout<< "in dtor Base11\n";

     Base1::~Base1( );  // <--- base class destructor inserted *after* any custom code
     }
};
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question