Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Constructor in C++

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:03:00

What is a Constructor in C++?

In C++, a constructor is a specific member function that is called automatically whenever a class object is created. It is commonly used to initialize the data members of a new object. They can also be used to execute a default code when an object is created. In C++, the constructor is named after the class or structure and does not return anything.

Characteristics of C++ Constructors

  • The constructor's name is the same as the class name.
  • Constructors are often declared in the public part of a class, although they can also be declared in the private section.
  • Constructors have no return type since they do not return values.
  • When we create a class object, we automatically invoke its constructor.
  • Constructors can become overloaded.
  • A constructor cannot be declared virtual.
  • Constructors cannot be inherited.
  • The Constructor's addresses cannot be referred to.
  • During memory allocation, the constructor implicitly calls the new and deleted operators.

Types of Constructors in C++

Constructors are classed based on the conditions in which they are used. C++ has 4 different types of constructors:

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Move Constructor

Default constructor in C++

A default constructor does not accept any arguments. It contains no parameters. It is also known as a zero-argument constructor.

Parameterized Constructor in C++

Parameterized constructors allow arguments to be sent to constructors. When an object is formed, these arguments are typically used to help initialize it. To create a parameterized constructor, simply pass parameters to it as you would any other function. When you define the constructor body, use the parameters to set up the object.

Uses for Parameterized Constructor

  • When new objects are formed, it is used to initialize their various data elements with varying values.
  • Constructors are overloaded with it.

Copy Constructor in C++

It is a member function that initializes an object's data members with another object of the same class. In other words, the Copy constructor in C++ is used to copy data from one object to another.

Uses of Copy Constructor in C++

  • Copy Constructor creates new objects by copying values from existing ones.
  • Deep copying is possible with this.
  • If necessary, modify certain attributes when copying.

Move Constructor in C++

Recently added to C++'s constructor family is the move constructor. It is similar to a copy constructor in that it creates an object from existing objects, but instead of duplicating the item into a new memory, it uses move semantics to transfer ownership of the previously produced object to the new object without making extra copies.

Uses for Move Constructor

  • Move constructors effectively transfer ownership of these resources rather than creating copies of them.
  • This reduces overhead and prevents excessive memory copying.
  • You can create your move constructor to manage specialized resource transfers.

What is a Destructor in C++?

In C++, destructors are class member methods that remove objects. They are called when the class object exits scope, such as when a function or program terminates, a delete variable is called, and so on. Destructors do not accept any arguments and do not return anything. 

Characteristics of a C++ Destructor

  • When an object is destroyed, the destructor function is automatically run.
  • It cannot be specified as static or const.
  • The destructor has no parameters.
  • It does not have a return type.
  • An object from a class that has a destructor cannot join the union.
  • A destructor must be declared in the class's public section.
  • The programmer does not have access to the destructor's address.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this