Thread: Does a derived class inherit the constructors of its base class?

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001

    Does a derived class inherit the constructors of its base class?

    Well I was reading a chapter about class inheritance when noticed something strange (for me). The text says
    A derived class does not inherit the constructors of its base class.
    The reasoning is that this could lead too easily to the introduction of uninitialized derived class member errors.
    I find it very strange becuase because when a print some debug information it seems that the constructor(from the base class) is inherited.
    #include <iostream>
    using namespace std;
    class Base
    Base() {cout << "Base constructor" << endl;}
    class SubClass1  : public Base
    SubClass1() {cout << "SubClass1 constructor" << endl;}
    int main()
    SubClass1 test;
    return 0;
    This code clearly shows that both construtors are invoked (inherited???). I also made a seach on this board that "confirmed" my suspicions.

    Is my book incorrect in this subject???

  2. #2
    Registered User
    Join Date
    Jun 2002
    I was just looking at this site with online books and i read this.
    This implementation has the following disadvantage. T
    he C++ compiler will generate code to call the default constructor of a base class from each constructor in the derived class, 
    unless explicitly instructed otherwise. 
    This can be compared to the situation which arises in composed objects (see section 6.4).
    Im not sure if it answers your questions though. This is from "C++ Annotations." (thats the book).

    And then i found this from another book "Complete C++ language tutorial "

    What is inherited from the base class?
    In principle every member of base class is inherited by derived one but:
    Constructor and destructor 
    operator=() member 
    Although constructor and destructor of the base class are not inherited, 
    the default constructor (i.e. constructor with no parameters)
     and the destructor of the base class are always called when a new object of a derived class is created or destroyed.
    So yes i think your books is right.
    Hope that answers your question!!!

    NOTE: if your wondering what site im getting these books from here:
    C++ Rules!!!!
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    see, all these people get the terminalogy wrong...

    I agree with the fact that both constructors get called repectively.
    I emphasize the word "called", which doesn't mean "inherit."

    By "inherit" I mean, that the derived class would not the base constructor implicitly declared within its own body.
    Reason is that, when objects would be created, and would come time to call the "base" constructor then such implicit call would be very ambiguous.

    In fact, there are ways around it. There are ways of sort of "inheriting" the base constructor (virtual functions and stuff)

    Don't forget when an object of a derived class is instantiated, then both constructors are called subsequently.

    Hope that helps

  4. #4
    Does a derived class inherit the constructors of its base class?
    Base class constructor is always called first, then the derived constructor is called, constructors cannot be virtual although you can override the base class constructor and choose which base constructor is being called. A derived object's destructor is called before the base class destructor (opposite in order of the constructors) and you can have virtual destructors.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    A derived class does not inherit it's base class ctor, but it calls it when the derived class ctor is called to initialize the base class members.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. base and derived class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 03:11 PM
  4. Replies: 1
    Last Post: 11-27-2001, 01:07 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM