Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Constructors and Destructors in C++

Introduction

In C++, constructors and destructors are special member functions of a class that are used to initialize objects and clean up resources, respectively. Understanding these functions is crucial for managing object lifecycle and resource management in C++ programming.

Constructors

A constructor is a special member function that is called when an object is instantiated. It initializes the object and allocates resources if necessary. The constructor has the same name as the class and does not have a return type.

There are different types of constructors in C++:

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

Default Constructor

A default constructor is a constructor that takes no arguments. It is either defined by the user or provided by the compiler if no constructors are defined.

Example:

class MyClass {
public:
    MyClass() {
        // Constructor code here
    }
};

int main() {
    MyClass obj; // Default constructor called
    return 0;
}
                

Parameterized Constructor

A parameterized constructor is a constructor that takes arguments to initialize an object with specific values.

Example:

class MyClass {
    int a, b;
public:
    MyClass(int x, int y) {
        a = x;
        b = y;
    }
    void display() {
        std::cout << "a: " << a << ", b: " << b << std::endl;
    }
};

int main() {
    MyClass obj(10, 20); // Parameterized constructor called
    obj.display();
    return 0;
}
                

Copy Constructor

A copy constructor is a constructor that initializes an object using another object of the same class. It is used for copying objects.

Example:

class MyClass {
    int a;
public:
    MyClass(int x) {
        a = x;
    }
    MyClass(const MyClass &obj) {
        a = obj.a;
    }
    void display() {
        std::cout << "a: " << a << std::endl;
    }
};

int main() {
    MyClass obj1(10); // Parameterized constructor called
    MyClass obj2 = obj1; // Copy constructor called
    obj2.display();
    return 0;
}
                

Destructors

A destructor is a special member function that is called when an object goes out of scope or is explicitly deleted. It cleans up resources allocated by the object. The destructor has the same name as the class with a tilde (~) prefix and does not take any arguments or return any value.

Example:

class MyClass {
public:
    MyClass() {
        // Constructor code here
    }
    ~MyClass() {
        // Destructor code here
    }
};

int main() {
    MyClass obj; // Constructor called
    // Destructor will be called automatically when obj goes out of scope
    return 0;
}
                

Conclusion

Constructors and destructors play a vital role in the lifecycle of a C++ object. Constructors help in initializing objects with specific values while destructors help in releasing resources when the object is no longer needed. Understanding and correctly implementing these special member functions ensure efficient resource management and prevent memory leaks in your C++ applications.