Constructors and Destructors

Constructors and destructors are special member functions in C++ classes. They are automatically called when objects are created and destroyed.

What is a Constructor?

A constructor is a special function that automatically runs when an object of a class is created. It is mainly used to initialize object data.

Features of Constructor

  • Same name as the class
  • No return type
  • Called automatically
  • Used for initialization

Syntax of Constructor

class ClassName {

public:

ClassName() {

// constructor code
}
};

Example of Constructor

#include <iostream>
using namespace std;

class Student {

public:

Student() {

cout << "Constructor called" << endl;
}
};

int main() {

Student s1;

return 0;
}

Output

Constructor called

Types of Constructors

1. Default Constructor

A constructor without parameters.

class Demo {

public:

Demo() {

cout << "Default Constructor";
}
};

2. Parameterized Constructor

A constructor that accepts parameters.

#include <iostream>
using namespace std;

class Student {

public:

string name;

Student(string n) {

name = n;
}

void display() {

cout << name;
}
};

int main() {

Student s1("Ali");

s1.display();

return 0;
}

Output

Ali

What is a Destructor?

A destructor is a special function that automatically runs when an object is destroyed. It is mainly used to free resources and clean up memory.

Features of Destructor

  • Same name as class with ~
  • No return type
  • No parameters
  • Called automatically when object ends

Syntax of Destructor

class ClassName {

public:

~ClassName() {

// destructor code
}
};

Example of Destructor

#include <iostream>
using namespace std;

class Student {

public:

Student() {

cout << "Constructor called" << endl;
}

~Student() {

cout << "Destructor called" << endl;
}
};

int main() {

Student s1;

return 0;
}

Output

Constructor called
Destructor called

How Constructors and Destructors Work

  1. Constructor runs when object is created
  2. Object performs tasks
  3. Destructor runs automatically when object is destroyed

Constructor vs Destructor

ConstructorDestructor
Initializes objectCleans resources
Called at object creationCalled at object destruction
Can have parametersCannot have parameters

Real-Life Example

Think of entering and leaving a classroom:

  • Constructor = Opening classroom and preparing environment
  • Destructor = Closing classroom and cleaning everything

Advantages of Constructors

  • Automatic initialization
  • Cleaner code
  • Reduces errors
  • Improves object setup

Advantages of Destructors

  • Automatic cleanup
  • Prevents memory leaks
  • Manages resources efficiently

Why Constructors and Destructors are Important

They are important because they:

  • Automate object management
  • Improve memory handling
  • Support efficient programming
  • Increase program reliability

Applications

Constructors and destructors are widely used in:

  • Memory management
  • File handling
  • Database connections
  • Resource allocation
  • Object-oriented applications

Conclusion

Constructors and destructors in C++ are special functions that automatically manage object creation and destruction. Constructors initialize objects, while destructors clean resources. Together, they improve memory management, code reliability, and program efficiency.

Home » Advanced C++ > Object-Oriented Programming > Constructors and Destructors