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

Binary search tree and graph, Important Note: No course works, which have b...

Important Note: No course works, which have been submitted via hard copies or emails, will be accepted a short essay below edited in a document (word, other) files with

I need computer application/program, I need Computer application/program. ...

I need Computer application/program. Project Description:                                                                I want a project done. It is a computer desktop appli

Write a program to calculate the average of ten numbers, Write a program to...

Write a program to calculate the average of ten numbers       #include stdio.h     /* Only this header is needed since only IO are used */     void main()     {          char

Common c control flow instructions, In this Lab you will code 8086 assemble...

In this Lab you will code 8086 assembler equivalents (Virgo) for common C control flow instructions. This will give you an appreciation for how control flow is implemented in assem

Padovan string, a padovan string p(n) for a natural number n is defined a...

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

Operator precedence and associatively, Op e r a t o r P r e c e d ...

Op e r a t o r P r e c e d e n c e a n d A s s o c i a t i v e l y : T h e op e r a t o r p r e c e d e n c e a n d i

Addition of array elements, Addition of array elements: #define rows 3 ...

Addition of array elements: #define rows 3 #define cols 3 void main() {                 int i=0,j=0,sum=0;                 int arr[rows][cols];

Calculation, write a program to calculate the cuboid

write a program to calculate the cuboid

What are virtual functions in c++, A virtual function permits derived class...

A virtual function permits derived classes to replace the implementation given by the base class. The compiler makes sure the replacement is always known as whenever the object in

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