Constructors in C++

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.

When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.

A constructor is declared without a return value, that also excludes void.
Therefore, when implemented, do not return a value:

Constructor Example

Default Constructor

A constructor is declared without a return value, that also excludes void. Therefore, when implemented, do not return a value:

This would produce:

I see a book…

This book constructor is a programmer created constructor and is empty. You might find it sometimes convenient to create your own constructor because, whether you create an empty constructor or not, this does not negatively impact your program but makes it more lively and allows other parts of the program to conveniently call the object using its constructor. A constructor is easily implemented once you have created one:

There are various categories of bricks used in the construction industry.

For this exercise, we will consider a simple one used in constructing domestic building foundations. It has a total length, here called Length; it also has a height, referred here to as Height; and it has a thickness that will be called Thickness. For resistance and static reasons, our brick will have two holes. Since we are more interested in the amount of cement used to create the brick, we will subtract the volume of the hole from the total volume. The dimensions we use are for simplicity. We will consider that the small wall of the brick has a thickness of 0.25; also, the static wall in the middle length has a thickness of 0.25.

Listing – Brick Unit – Header File: Bricks.h

Brick Unit – Source File: Bricks.cpp

Main File: Main.cpp

This program would produce:


    • When you create the object of a class, it calls the Default constructor of that class. It is a good practice that you create the default constructor. Also initialization of the object is done in the default constructor.

    • default constructor wil be without parameters.
      and wil be by compiler
      for eg.

      class abc
         int a,b;
        void sum()
      void main()
        abc obj;
    • class xyz
      xyz()//default constructor
      name starts with name of class

      ——– Original Message ——–
      Can u explain the initiallization in constructor

    • I think it depends upon condition. For Example

      If a C++ object is allocated from a memory pool, its constructor will not be called by default. This can sometimes be handled by initializing the object with an explicit call to the constructor. For example:

      Or you can use the new keyword to call the constructor and initilise the object. The new keyword allocates memory for an object or array of objects of type-name from the free store and returns a suitably typed, nonzero pointer to the object.

    • ok when we create a constructor, first memory is allocated for it and then constructor is called.

      ——– Original Message ——–
      I have doubt when we create an object then constructor is called first or Memory allocation occurs first. Please, explain it

Leave a Reply