News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

What is inherited from the base class?

Started by VelMurugan, Jan 03, 2009, 11:17 PM

Previous topic - Next topic

VelMurugan

What is inherited from the base class?

In principle, a derived class inherits every member of a base class except:

    * its constructor and its destructor
    * its operator=() members
    * its friends

Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.

If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class:

derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

For example:


// constructors and derived classes
#include <iostream>
using namespace std;

class mother {
  public:
    mother ()
      { cout << "mother: no parameters\n"; }
    mother (int a)
      { cout << "mother: int parameter\n"; }
};

class daughter : public mother {
  public:
    daughter (int a)
      { cout << "daughter: int parameter\n\n"; }
};

class son : public mother {
  public:
    son (int a) : mother (a)
      { cout << "son: int parameter\n\n"; }
};

int main () {
  daughter cynthia (0);
  son daniel(0);
 
  return 0;
}

   
Quote
mother: no parameters
daughter: int parameter

mother: int parameter
son: int parameter

Notice the difference between which mother's constructor is called when a new daughter object is created and which when it is a son object. The difference is because the constructor declaration of daughter and son:

Quotedaughter (int a)          // nothing specified: call default
son (int a) : mother (a)  // constructor specified: call this

Source : cplus

MrEARTHSHAcKER

I saw this example on cplusplus.com, but I still have one question:

son (int a) : mother (a)

Does this mean  it will use derived class's default constructor and simultaneously pass the parameter to base class's default constructor?

My conclusion came from here:

daughter (int a)

Which uses base class's default constructor without parameters which is being called considering it hasn't been called from derived class, as it's case with class son.

So question was>> Does this mean  it will use derived class's default constructor and simultaneously pass the parameter to base class's default constructor?