Constructor and destructor function with derived classes, C/C++ Programming

Assignment Help:

Constructor and Destructor function with derived classes

If there are constructors included in the base class and the derived class, the compiler automatically calls both of them. This occurs because as soon as the derived class constructor gets control and establishes formal arguments, the base class constructor will be called instantaneously, i.e., before the derived class initialization list is honored.

Syntax in the derived class base/members initialization list :

  • The base class name ( name of base constructor )
  • A list of arguments among parentheses, as in any normal function call. To explicitly invoke the base class default constructor , leave the argument list empty.

Remember the base class constructor always is called first.

                class base

                 {

                  public :

base(int n=0);

 ~base();

int get_base_number();

                  protected:

                int base_number;

                 };

base::base(int n=0)

 {

base_number = n;

cout << "base constructor ";

 }

base::~base()

 {

cout << "base destructor ";

 }

int base::get_base_number()

 {

return base_number;

      }

 

                class derived : public base

                 {

                  public :

derived(int b=0, int d =0): base(b),derived_number(d);

~ derived();

int get_derived_number();

                   private :

 int derived_number;

                  };

 

derived::derived(int b=0, int d =0): base(b),derived_number(d)

  {

                                cout << " derived constructor ";

  }

 

derived::~ derived()

                 {

                     cout << "derived destructor ";

                 }

int derived::get_derived_number()

 {

return derived_number;

 }

void main()

 {

  derived d(1,2);

 

cout < " d= " << d.get_base_number()<<",";

cout << d.get_derived_number();          

 }

 

 

Output:

 

                                Base constructor

                                Derived constructor

                                D = 1,2

                                Derived  destructor

                                Base destructor

 


Related Discussions:- Constructor and destructor function with derived classes

., what is polymorphism

what is polymorphism

Programming, pseudocode for gregorian calendar

pseudocode for gregorian calendar

Miniumshelf, write a prgm to find minimum total number of shelves including...

write a prgm to find minimum total number of shelves including the intial one required for loading process

Padovan string, A Padovan string P(n) for a natural number n is defined as:...

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2

Program to calculate pie, This problem familiarizes you with using random n...

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of π using a simulation method called "Monte Carlo". The following fi

C++, write a c++ program for minimum shelf downloads

write a c++ program for minimum shelf downloads

Write a fragment of code that declares a variable, (a) Write a fragment of ...

(a) Write a fragment of code that declares a variable colSp of type ColourSpot, and then moves it to the point (3, -4), and sets its colour to Green. (b) A function QU8 is speci

Functions and stack frame, Each function has its own stack frame between %f...

Each function has its own stack frame between %fp and %sp. Let Caller calls Callee. Then Caller's %sp becomes callee's %fp, and callee's %sp set to be a new value (a smaller one be

Luminous Jewels - The Polishing Game, 1. Jewels can only be removed for pol...

1. Jewels can only be removed for polishing from either end of the necklace (i.e. head or tail) 2. Once a jewel type is removed from the necklace, all other instances of the same j

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd