Javatpoint Logo
Javatpoint Logo

C++ Constructor

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure.

In brief, A particular procedure called a constructor is called automatically when an object is created in C++. In general, it is employed to create the data members of new things. In C++, the class or structure name also serves as the constructor name. When an object is completed, the constructor is called. Because it creates the values or gives data for the thing, it is known as a constructor.

The Constructors prototype looks like this:

The following syntax is used to define the class's constructor:

The following syntax is used to define a constructor outside of a class:

Constructors lack a return type since they don't have a return value.

There can be two types of constructors in C++.

  • Default constructor
  • Parameterized constructor

C++ Default Constructor

A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

Let's see the simple example of C++ default Constructor.


Default Constructor Invoked 
Default Constructor Invoked

C++ Parameterized Constructor

A constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct objects.

Let's see the simple example of C++ Parameterized Constructor.


101  Sonoo  890000
102  Nakul  59000

What distinguishes constructors from a typical member function?

  1. Constructor's name is the same as the class's
  2. Default There isn't an input argument for constructors. However, input arguments are available for copy and parameterized constructors.
  3. There is no return type for constructors.
  4. An object's constructor is invoked automatically upon creation.
  5. It must be shown in the classroom's open area.
  6. The C++ compiler creates a default constructor for the object if a constructor is not specified (expects any parameters and has an empty body).

By using a practical example, let's learn about the various constructor types in C++. Imagine you visited a store to purchase a marker. What are your alternatives if you want to buy a marker? For the first one, you ask a store to give you a marker, given that you didn't specify the brand name or colour of the marker you wanted, simply asking for one amount to a request. So, when we just said, "I just need a marker," he would hand us whatever the most popular marker was in the market or his store. The default constructor is exactly what it sounds like! The second approach is to go into a store and specify that you want a red marker of the XYZ brand. He will give you that marker since you have brought up the subject. The parameters have been set in this instance thus. And a parameterized constructor is exactly what it sounds like! The third one requires you to visit a store and declare that you want a marker that looks like this (a physical marker on your hand). The shopkeeper will thus notice that marker. He will provide you with a new marker when you say all right. Therefore, make a copy of that marker. And that is what a copy constructor does!

What are the characteristics of a constructor?

  1. The constructor has the same name as the class it belongs to.
  2. Although it is possible, constructors are typically declared in the class's public section. However, this is not a must.
  3. Because constructors don't return values, they lack a return type.
  4. When we create a class object, the constructor is immediately invoked.
  5. Overloaded constructors are possible.
  6. Declaring a constructor virtual is not permitted.
  7. One cannot inherit a constructor.
  8. Constructor addresses cannot be referenced to.
  9. When allocating memory, the constructor makes implicit calls to the new and delete operators.

What is a copy constructor?

A member function known as a copy constructor initializes an item using another object from the same class-an in-depth discussion on Copy Constructors.

Every time we specify one or more non-default constructors (with parameters) for a class, we also need to include a default constructor (without parameters), as the compiler won't supply one in this circumstance. The best practice is to always declare a default constructor, even though it is not required.

A reference to an object belonging to the same class is required by the copy constructor.

What is a destructor in C++?

An equivalent special member function to a constructor is a destructor. The constructor creates class objects, which are destroyed by the destructor. The word "destructor," followed by the tilde () symbol, is the same as the class name. You can only define one destructor at a time. One method of destroying an object made by a constructor is to use a destructor. Destructors cannot be overloaded as a result. Destructors don't take any arguments and don't give anything back. As soon as the item leaves the scope, it is immediately called. Destructors free up the memory used by the objects the constructor generated. Destructor reverses the process of creating things by destroying them.

The language used to define the class's destructor

The language used to define the class's destructor outside of it

Next TopicC++ Destructor

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA