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 are user defined data types, Q: What are User Defined data types? ...

Q: What are User Defined data types? C supports an extraordinary feature known as "type definition" that permits users to define an identifier that would represent an existing

When i throw this object, A: Depends. Might be "zero" Objects which are ...

A: Depends. Might be "zero" Objects which are thrown must have a publicly accessible copy-constructor. The compiler is permitted to generate code which copies the thrown object

Program is to append the contents of one file to another, Program is to app...

Program is to append the contents of one file to another: void main()    {   clrscr();   fstream file1,file2;   char st1[13],st2[13];/* 13 because a filename canno

Define array of structures, Define Array of Structures? An Array of Str...

Define Array of Structures? An Array of Structures is an assortment of the same data types which are declared as structures. It is useful to store large and different number of

What is a command line argument and what is its use, Question 1) What are ...

Question 1) What are the commonly used input/output functions in C? Question 2) What is the difference between function declaration and function definition? Write a recursive

Wap to print series from 1 to 10 & find its square and cube, # include stdi...

# include stdio.h> # include conio.h> # include math.h>   void main () { int a=1,sqr=0,cube=0; clrscr (); while (a { sqr=pow(a,2); cube=pow(a,3);

Function that have parameter and makes an integer mask, Write a function th...

Write a function that has an int parameter n, makes an integer mask having the bit 1 at the nth place from the rightmost bit, and returns the mask. For example, when n = 5 is passe

Write the program of function templates, Consider the following example: ...

Consider the following example: int max(int x, int y)                  {                                 return ( x > y) ? x : y ;                  }   float max

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