Constructors, Destructors and Inline Function in C++

Constructors:-

  • Constructor is a user defined function whose name is same as the class name.
  • Constructor is called as a user defined function, because, a programmer can provide its own constructor with its own functionality.
  • Constructor is a first function which is called implicitly in the objects lifetime.
  • In the constructor , memory is not allocated for the object but constructor is responsible for following things,

1)    It is responsible for initializing memory allocated for that object.

2)    Constructor is responsible for setting appropriate environment for a class.

3)    If our class contains a single virtual function or if our class is derived from such a class which contains at least single virtual function then there is need to create a VPTR as well as initialization of that VPTR with vtable is responsibility of the constructor.

  • We can call a constructor explicitly.
  • We can overload a constructor like a normal function overloading.
  • There is no return value for a constructor, because, we do not call constructor.
  • Constructor should always in the public section, because, if we write it in private section then we are unable to create an object of that class.
  • There are three types of constructor as below.

1)    Default constructor

2)    Parameterized constructor

3)    Copy constructor

  • If we do not provide any constructor then C++ compiler provides a default constructor for our class.
  • And if we provide any of the constructor from above then C++ compiler does not provide any type of constructor.

e.g.

If we provide copy constructor only then C++ does not provide a default constructor.

class demo

{

public:

int i,j;

// first constructor

demo() // default constructor

{

i=0;

j=0

}

//second constructor

demo(int x,int y) // parameterized constructor with two arguments

{

i=x;

j=y;

}

//Third constructor

demo(int z) // overloade parameterized constructor with single argument

{

i=z;

j=z;

}

//Forth constructor

demo(demo &obj) // copy constructor

{

i=obj.i;

j=obj.j;

}

};

int main()

{

demo d; // first constructor

demo d1(10,20); // second constructor

demo d2(30); // third constructor

demo d 3(d2); // forth constructor

return 0;

}

  • In the above example, second and third constructors are parameterized constructor and in this case the overload this constructor by differentiating no of arguments for that constructor.
  • We can also provide default arguments for our parameterized constructor.

e.g.

demo(int x,int y=10)

{

i=x;

j=y;

}

  • In this case, we provide default value for second argument.
  • This constructor is also called as parameterized constructor (not a default constructor).

 

Destructor:-

  • Destructor is used for de-allocating a memory which is allocated inside a constructor.
  • Destructor is not used for de-allocating a memory which is allocated for an object.
  • Memory for an object is de-allocated when scope of the object is ended.
  • Name of the constructor is same as the name of the class and to distinguish between a constructor and destructor ‘~’ operator is used.
  • As like a constructor we cannot overload a destructor.
  • If we do not provide any destructor C++ provides its own destructor.
  • If we allocate a memory dynamically from a constructor then good programming practice is that de-allocating that memory from destructor by using delete() or free().
  • There are no types of a destructors.

e.g.

class demo

{

~demo()

{

// code

}

};

  • Destructor is a last function which is called in the lifetime of our object, because, the use of destructor is for memory clean up.
  • We can call a destructor explicitly, like,

demo d;

d.~demo();

  • If we call a destructor explicitly till compiler calls destructor again means in the above code destructor is called two times.
  • First is for explicit call and second is for implicit call.
  • There is no co-relation between a constructor and a destructor means if we provide any type of a constructor and if we do not provide destructor then compiler provides own destructor.

 

Constructors Destructors c++

 

Combine example of all type of constructor:-

class demo

{

public:

int i;

float f;

char c;

demo() // default constructor

{

i=0; // thets are the default initialization which is used by the default constructor

f=0.0;// provided by the compiler itself but we can initialize it with any values.

c=’’

}

demo(int x,float z,char l) // parameterized constructor

{

i=x;

f=y;

c=l;

}

demo(int a) // overloaded parameterized constructor with single parameter

{

i=a;

f=0.0;

c=’’;

}

demo(demo &d) // copy constructor

{

i=d.i;

f=f.f;

c=d.c;

}

demo(int ii, float ff, char cc=’a’) // parameterized constructor with default value

{

i=ii;

f=ff;

c=cc;

}

demo(int ii,float ff) // error, because, there is an ambiguity while calling a

{                           // constructor

i=ii;

f=ff;

c=’’;

}

~demo()

{

// code to  free the memory which is allocated from the constructor

}

};

int main()

{

demo d; // default constructor is called.

demo d1(d); // copy constructor is called.

demo d2(10,2.2,’a’); // parameterized constructor is called.

demo d3(10,2,2); // parameterized constructor with default values is called.

return 0;

}

  • Destructor call is depend on its scope.
  • If the scope is only within the function then the destructor is called at the end of a function.
  • And if the scope is global then the destructor is called at the end of a program.

 

Inline Function in C++:-

  • Inline function is used in the C++ file.
  • When we define a function as a inline function, at the calling point of the function, definition of that function is replaced.
  • If we want to create a function as an inline function then we need to write a keyword ‘inline’ before a function name.

e.g.

inline int fun()

{

// code

}

  • When we define a function inside a class itself then that function is considered as inline function by default C++ compiler.
  • Writing an inline keyword before function does not mean that our function becomes inline function.
  • To become a function as an inline function that function should satisfy some rules which are provided by the C++ compiler.
  • And that rules can vary from compiler to compiler.
  • Generic rules are as follows,

1)    If the function is too big then that function is not considered as an inline function.

2)    If the function contains loop then that function is not considered as an inline function.

3)    If the function is recursive function then that function is not considered as an inline function.

4)    If there is function pointer is applied for a particular function then that function is not considered as an inline function.

5)    If the function accesses the static keyword then that function is not considered as an inline function.

The above rules vary from compiler to compiler.

  • There are some advantages and disadvantages in an inline function.
  • If the function is called multiple times and it is an inline function then size of our program is increased, because, at each and every calling point definition of a function is replaced.
  • But due to this approach our program runs faster as compare to normal functions.

class demo

{

public:

inline int fun()

{

// code

}

};

int main()

{

demo d;

fun(); // compiler replaces the code

fun(); // again the fun function is called.

}

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply