Constructor

A constructor is a special method which is automatically called whenever an instance of class is created. It is used to initialize objects of a class. A constructor is also used to run a default code when an object is created.

Properties of Constructor

  1. Its name is same as the name of class.
  2. It has no return type.
  3. It is called whenever the object is created. (default constructor).
  4. It is used to initialize the data members of class
  5. Constructor is always public.

So, properties and behaviors are members of class and declared inside the class.

Understanding Constructors using real life example

Suppose you went to a shop to buy a pen. You say to the shop keeper, give me a marker. You are not specifying the brand or ink color. He will give you the most hot marker selling in the market. This is the default constructor. If you say give me a marker of xyz brand and ink should be blue, this is parameterized constructor. And if you take a marker with you and show it to the shopkeeper and say that give me a marker like this. This is your copy constructor. Let us explore some more details.

Default Constructors

They take no parameter. Now we will see, how a constructor can initialize the data members.

Parameterized Constructor

A constructor that takes one or more parameter to set values of data members. These values are passed when object is created. It is just like when we create and call functions.

Note: If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body)

"this" keyword

this refers to the member of current class in which we are present.

Copy Constructor

The copy constructor creates a new object by copying the values from existing object of class to other object of the same class.Copy constructor takes a reference to an object of the same class as an argument. Simply, copy constructor copies values from one object to another object.

Constructor Overloading

Constructor overloading is similar to function overloading. In constructor overloading, we make two constructors. Both have:

  • Different numbers of parameters, Or
  • Data types of parameters is different, Or
  • Sequence of parameters is different, Or
  • All three

Destructors

Destructors are called whenever object is destroyed. It is used to deallocate memory. It is automatically called at the end of program.

Properties of Destructors

  1. Destructor is the last function which is invoked when the object is destroyed.
  2. Its name is the name of the class.
  3. It is always public.
  4. It can not be static or constant.
  5. It is always unparameterized.
  6. It has no return type even void.
  7. We can not access the address of destructor.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min