C++ Constructor

In C++, a constructor is a special member function that is automatically invoked when an object of a class is created. The primary purpose of a constructor is to initialize the object's data members. A constructor has the same name as the class or structure it belongs to and does not have a return type.

Key Features of Constructors:

Automatic Invocation: A constructor is automatically called when an object is created.

Initialization: It is primarily used to initialize the data members of an object.

Same Name as Class: The constructor shares the same name as the class or structure it belongs to.

No Return Type: Constructors do not have a return type, not even void.

Syntax of a Constructor:

The general syntax for defining a constructor inside a class is:

class ClassName {
public:
ClassName(list-of-parameters) {
// constructor definition
}
};

For defining a constructor outside of a class, the syntax is:

ClassName::ClassName(list-of-parameters) {
// constructor definition
}

Types of Constructors in C++

There are mainly two types of constructors in C++:

1. Default Constructor:

• A constructor that does not take any parameters.

• If you do not provide any constructor, C++ automatically provides a default constructor.

class Example {
public:
Example() {
// default constructor
}
};

2. Parameterized Constructor:

• A constructor that takes parameters, allowing you to pass values at the time of object creation.

class Example {
public:
Example(int x, int y) {
// parameterized constructor
}
};

Example:

<#include <iostream>>
  using namespace std;

  class Rectangle {
    private:
      int length;
      int breadth;

    public:
      // Default Constructor
      Rectangle() {
        length = 0;
        breadth = 0;
      }

      // Parameterized Constructor
      Rectangle(int l, int b) {
        length = l;
        breadth = b;
      }

      void display() {
        cout << "Length: " << length << ", Breadth: " << breadth << endl;
      }
  };

  int main() {
    Rectangle rect1;     // Default constructor is called
    Rectangle rect2(10, 20); // Parameterized constructor is called

    rect1.display();
    rect2.display();

    return 0;
  }

Output:

Length: 0, Breadth: 0
Length: 10, Breadth: 20

Characteristics of a Constructor

Same Name as Class: The constructor has the same name as the class it belongs to.

Public Declaration: Typically declared in the public section, though not mandatory.

No Return Type: Constructors do not have a return type.

Automatic Invocation: The constructor is called automatically when an object is created.

Overloading: Constructors can be overloaded with different parameters.

No Virtual or Inherited: Constructors cannot be declared as virtual and are not inherited by derived classes.

No Address Taken: You cannot take the address of a constructor.

Implicit Use of new and delete: Constructors are implicitly involved when new or delete is used for memory allocation.

What is a Copy Constructor?

A copy constructor is a special constructor that initializes an object using another object of the same class.


Purpose: The copy constructor is used to create a copy of an object, ensuring that the new object is identical to the original

Syntax:

Sample(Sample &t) {
id = t.id;
}

What is a Destructor in C++?

A destructor is a special member function that is automatically invoked when an object goes out of scope or is explicitly deleted. It is used to perform clean-up operations and free resources that were allocated to the object.

Syntax:

~ClassName() {
// Destructor body
}

Characteristics:

No Overloading: Only one destructor is allowed per class.

No Parameters: Destructors do not take any arguments and do not return any value.

Automatic Invocation: Destructors are automatically called when the object goes out of scope or is deleted.

Memory Deallocation: Destructors handle deallocating memory or releasing resources acquired by the object during its lifetime.