Constructor and Destructor in C++

Share Now

In C++, constructors and destructors are special member functions of a class used for initializing and cleaning up objects.

Constructor and Destructor in C++

What is constructor in C++?

A constructor in C++ is a special member function that is automatically called when the object of the class is created. The main purpose of the constructor is to initialise the data member. The constructor declaration is different from the normal function:

  • In the constructor, the class name and function name should be the same.
  • The constructor does not have a return type.
  • The constructor will be called automatically when it is created.
  • If we do not declare a constructor, then C++ creates a default constructor.

Syntax:

#include <iostream>
using namespace std;

class Person {
public:
    // Constructor without parameters, you can see class name and function name are same
    Person() {
        cout << "Constructor executed!" << endl;
    }
};

int main() {
    Person p1;  // Constructor runs automatically
    return 0;
}

Type of Constructors in C++

There are different types of constructors used in C++ based on the situation.

  • Default Constructor
  • Parameterised constructor
  • Overloading Constructor
  • Empty Constructor
  • Copy Constructor
1. Default Constructor

The constructor created automatically is known as the default constructor. The default constructor does not take arguments, and it is called automatically. The default constructor is also known as the argument constructor.

#include <iostream>
using namespace std;

class Student {
public:
    // Default constructor (no parameters)
    Student() {
        cout << "Default Constructor called!" << endl;
    }
};

int main() {
    Student s1;  // Constructor runs automatically
    return 0;
}
2. Parameterised constructor

A parameterised constructor can take arguments, so it is called a parameterised constructor. It helps to pass specific values during the object creation.

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    // Parameterized constructor
    Student(string n, int a) {
        name = n;
        age = a;
        cout << "Parameterized Constructor called!" << endl;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    // Passing values while creating object
    Student s1("Anurag", 20);

    s1.display();

    return 0;
}
3. Overloading Constructor

A constructor can be overloaded when multiple constructors with the same name but different parameters are given. The compiler decides which constructor should be called at compile time based on the parameter.

#include <iostream>
using namespace std;

class Student {
public:
    // Default constructor
    Student() {
        cout << "Default Constructor called!" << endl;
    }

    // Constructor with one parameter
    Student(string n) {
        cout << "Name: " << n << endl;
    }

    // Constructor with two parameters
    Student(string n, int a) {
        cout << "Name: " << n << ", Age: " << a << endl;
    }
};

int main() {
    Student s1;              // Calls default constructor
    Student s2("Anurag");    // Calls constructor with one parameter
    Student s3("Riya", 21);  // Calls constructor with two parameters
    return 0;
}
4. Empty constructor

The constructor with no parameters is known as the empty constructor. These constructors are defined as nop (empty block of instruction). This constructor does nothing in the programming.

#include <iostream>
using namespace std;

class Demo {
public:
    // Empty constructor (does nothing)
    Demo() { }
};

int main() {
    Demo obj;  // Empty constructor runs here
    cout << "Object created successfully!" << endl;
    return 0;
}
5. Copy constructor

The copy constructor is a special type of constructor that creates a new object as a copy of an existing object.

What is destructors in C++?

In C++, when the constructor is called, then the constructor automatically stores the object in memory; this object is not destroyed automatically. To destroy or delete the constructor from memory, we use destructors. Destructors take no arguments, and destructors are also automatically called when an object is destroyed. The destructor name will be the same name as the class, only at the start we have to use a tilde (~).

  • A destructor is a special member function with the same name.
  • A destructor is invoked automatically.
  • A destructor does not take arguments.
  • A destructor does not return values.
  • A class has only one destructor and cannot be overloaded.
  • A destructor is mainly used to destroy objects from the memory.
#include <iostream>
using namespace std;

class Test {
public:
    // Constructor
    Test() {
        cout << "Constructor Called" << endl;
    }

    // Destructor
    ~Test() {
        cout << "Destructor Called" << endl;
    }
};

int main() {
    Test t;  // Object created, constructor runs
    // When main ends, destructor is automatically called
    return 0;
}

Disclaimer: We have provide you with the accurate handout of “Constructor and Destructor in C++“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.

Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.

cbseskilleducation.com

Leave a Comment