CPP

C++ Constructors and Destructors - Explained


Constructors and destructors are fundamental concepts in C++ object-oriented programming. Constructors are special member functions used for initializing objects, while destructors are used for cleaning up resources when objects go out of scope. In this guide, we'll explore the concepts of constructors and destructors in C++ with sample code.

Constructors

Constructors are member functions with the same name as the class. They are automatically called when an object of the class is created. Constructors can have parameters, and they initialize the object's state. Here's an example of a class with a constructor:

#include <iostream>
using namespace std;
class Rectangle {
public:
    // Constructor with parameters
    Rectangle(int w, int h) : width(w), height(h) {
        cout << `Constructor called.` << endl;
    }
    int area() {
        return width * height;
    }
private:
    int width;
    int height;
};
int main() {
    Rectangle r(5, 3); // Calls the constructor
    int a = r.area();
    cout << `Area: ` << a << endl;
    return 0;
}
    

In this example, the `Rectangle` class has a constructor that takes two parameters, `width` and `height`. When an object `r` is created, the constructor is automatically called to initialize the object's state.

Destructors

Destructors are also special member functions with the same name as the class, preceded by a tilde (`~`). They are called when an object goes out of scope or is explicitly deleted. Destructors are used to release resources, close files, or perform any necessary cleanup. Here's an example with a destructor:

#include <iostream>
using namespace std;
class ResourceHolder {
public:
    ResourceHolder() {
        cout << `Constructor called.` << endl;
    }
    ~ResourceHolder() {
        cout << `Destructor called.` << endl;
    }
};
int main() {
    ResourceHolder* rh = new ResourceHolder; // Calls the constructor
    // ...
    delete rh; // Calls the destructor
    return 0;
}
    

In this example, the `ResourceHolder` class has both a constructor and a destructor. When an object is explicitly deleted using `delete`, the destructor is called to perform cleanup.

Default Constructors

If you don't define any constructors, C++ provides a default constructor that initializes the object. However, if you define a constructor with parameters, the default constructor is no longer generated. Here's an example:

#include <iostream>
using namespace std;
class Point {
public:
    // Parameterized constructor
    Point(int x, int y) : xPos(x), yPos(y) {
        cout << `Constructor called.` << endl;
    }
private:
    int xPos;
    int yPos;
};
int main() {
    Point p1(3, 4); // Calls the parameterized constructor
    Point p2; // Error: No default constructor available
    return 0;
}
    

In this example, defining a parameterized constructor for the `Point` class means that there is no default constructor available. Therefore, attempting to create a `Point` object without providing values for `x` and `y` results in an error.

Conclusion

Constructors and destructors play a crucial role in C++ object-oriented programming. Constructors initialize object state, while destructors clean up resources when objects go out of scope. Understanding these concepts is essential for creating well-structured and resource-efficient C++ programs.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.