Constructors and Destructors

Expert Answer & Key Takeaways

Deep dive into Object initialization and destruction in C++: Default, Parameterized, Copy constructors, and Destructors.

Constructors and Destructors in C++

When creating objects from a class, it is crucial to initialize their data members properly. Relying on normal functions to do this can lead to errors if the programmer forgets to call them. C++ solves this using Constructors, which automatically execute when an object is created.

1. What is a Constructor?

A Constructor is a special member function of a class that is automatically called when an object of that class is instantiated (created). Its primary purpose is to initialize the object's data members.

1.1 Rules for Constructors

  1. Name: It MUST have the exact same name as the Class itself.
  2. Return Type: It does NOT have a return type (not even void).
  3. Access: It should normally be placed in the public section (otherwise, you cannot create objects outside the class).
  4. Execution: It is invoked automatically, only once per object, at the time of creation.

2. Types of Constructors

2.1 Default Constructor

A constructor that takes no arguments is called a Default Constructor. If you don't write any constructor, the C++ compiler automatically provides an invisible, empty default constructor for you.
class Student { public: int roll; // Default Constructor Student() { roll = 0; // Initialization cout << "Object created!"; } }; // Usage: Student s1; (Automatically prints "Object created!" and sets roll to 0)

2.2 Parameterized Constructor

A constructor that takes one or more arguments. It allows you to pass different values to different objects right at the moment they are created.
class Student { public: int roll; // Parameterized Constructor Student(int r) { roll = r; } }; // Usage: Student s1(101); Student s2(102);

2.3 Copy Constructor

A copy constructor is used to initialize an object using another object of the same class. It takes a reference to an object of the same class as an argument.
class Student { public: int roll; Student(int r) { roll = r; } // Copy Constructor Student(const Student &obj) { roll = obj.roll; } }; // Usage: // Student s1(101); // Student s2(s1); // s2 is created as a copy of s1. s2.roll becomes 101.

3. Constructor Overloading

Just like function overloading, a class can have multiple constructors as long as they have different numbers or types of parameters. The compiler will automatically choose the correct constructor based on the arguments passed during object creation.

4. What is a Destructor?

A Destructor is another special member function that is executed automatically when an object goes out of scope or is explicitly deleted. Its purpose is to clean up resources (like freeing dynamic memory or closing files) before the object is destroyed.

4.1 Rules for Destructors

  1. Name: It has the exact same name as the Class, but preceded by a tilde symbol (~).
  2. Return Type: Like constructors, it has no return type.
  3. Arguments: A destructor takes NO arguments. Therefore, it cannot be overloaded. There can be only ONE destructor per class.
  4. Execution: It is invoked automatically in the reverse order of object creation (Last-In, First-Out).
class FileHandler { public: // Constructor FileHandler() { cout << "File Opened"; } // Destructor ~FileHandler() { cout << "File Closed"; } };

Course4All Editorial Board

Verified Expert

Subject Matter Experts

Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.

Pattern: 2026 Ready
Updated: Weekly