Oct 17, 2019, 07:03 AM

News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!


What is inherited from the base class?

Started by VelMurugan, Jan 04, 2009, 04:47 AM

previous topic - next topic
Go Down

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:

Code: [Select]

// 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:

Quote
daughter (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:

Code: [Select]
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:

Code: [Select]
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?


Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Design Chennai | GinGly :: Mobile SMS Backup | CineBuzz :: Latest Cinema News | My Kids Diary :: Kids magical moment :: Book domain name @ 99 Rs monthly
Copyright 2005 - 2019 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 14 years - Powered by IT Acumens Dedicated Server