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

C program for function of merge , C Program for FUNCTION OF MERGE #inc...

C Program for FUNCTION OF MERGE #include conio.h> #include stdio.h> char cot(char a[],char b[]); void main() {           char a[50],b[50];           clrscr()

Flowchart, create a flowchart that display the assume that there are 3 sect...

create a flowchart that display the assume that there are 3 section each student ?

Verification class, I need help to understand and do this assignment ******...

I need help to understand and do this assignment ********************************************************* You are to insert the missing code in the C program given for combinatio

Coding, how to make the coding

how to make the coding

C program to construct a structure , c program to construct a structure: ...

c program to construct a structure: struct sensus                 {                                 char name[30];                                 long int population;

Area under the curve, Write a program to find the area under the curve y ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Write a program to implement operator functions, Implementing Operator Func...

Implementing Operator Functions The general format of the Operator function is: return_type operator op ( argument list );   Where op is the symbol for the operator be

Explain public and private members, Public, Private and Protected members: ...

Public, Private and Protected members: Class members can either be declared in public','protected' or in the 'private' sections of the class. But as one of the features of OOP i

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