Updated March 16, 2023
Introduction on Constructor and Destructor in C++
Both constructors and destructors are the member function of the class. A constructor is a function that initializes the object of the class and allocates the memory location for an object, the function has the name as the class name, known for creating the object, called when the instance of the class created. Destructor also has the same name as the class name, denoted by tilted ~ symbol, known for destroying the constructor, deallocates the memory location for created by the constructor. One class can have more than one constructor but have one destructor.
When we create an object of a class in C++, we may want to initialize fields of class with some default or certain values. A class has its fields and methods. Fields are mostly variables or objects. By assigning default values to these fields, we make our programming easy. The constructor is like normal function within the class and the compiler calls it automatically when we create a new object of that class.
While creating the object we actually pass the required values as an argument to the object, these arguments are passed to the constructor of the class. The argument values now can be assigned to corresponding fields, which are available in class. In addition, we can assign default values to the fields where we do not pass the arguments. There are as such different types and ways by which we initialize the constructor in C++.
The constructor is like a special member function of a class. Whenever we create a new object the constructor is executed. The syntax of a constructor is very simple, the function will have the same name as that of class in which it is defined and it will not have any return type, not even void. Constructors are very useful in assigning particular values to certain members of the class. The constructor can be used to perform class-related logic at the time of instantiation.
Syntax of Constructor
Below is the syntax mentioned:
class Box {
public:
int height;
//constructor
Box () {
// member initialization
}
}
The destructor in C++ is also a special member function in class. The destructor is called automatically by the compiler and gets executed whenever the object of that particular class goes out of the scope or it is deleted by using delete expression to the pointer of that object. Whenever the object is destroyed by a compiler, the destructor is called. In destructor, we can execute statements which may be anything that we want to perform when the object goes out of the scope. The post-operations like closing connections, closing files, etc are some of the examples that we can perform in the destructor.
Syntax of Destructor
Below is the syntax mentioned:
class Box {
public:
int height;
//destructor
~Box () {
// post statements
}
}
The Syntax of the destructor is very much similar to the constructor. It will have the same name as that of class in which it is defined and will have no return type. However, just it will have~ (tilde) sign before the destructor name. Usually, the destructor is used to deallocate the dynamically allocated memory for the class and its members. The destructors are very useful to perform post operations, which can be performed after business logic.
Why do we need a Constructor and Destructor in C++?
C++ is an object-oriented programming language. In any object-oriented programming language, we map original real-life entities with the class and objects.
Let’s understand why do we need a constructor and destructor in C++ by real-life example. Suppose we have entity Box and we have created a class for it and it will have objects also which can be used in other parts. Now the box will have certain parameters like height, length, width, etc. and whenever we create an object of class Box, we want it to take some default value. Let’s say for height, this goal can be achieved by assigning a particular default value to height in the constructor of that class. Similarly, for other parameters also values can be assigned. After using the object of Box let’s say we want some particular action immediately or call some function. This can be done by using Destructor in C++. The required statements will be added to the destructor of that class. These are the real-time benefits of having a constructor and destructor in C++.
How do Constructors and Destructors work in C++?
As discussed earlier, the constructor gets called by the compiler whenever an object is created. If we do not define constructor, the C++ compiler automatically generates a default constructor for us. The constructor is used to allocate the memory if required and constructing the object of class whereas, a destructor is used to perform required clean-up when an object is destroyed. The destructor is called automatically by the compiler when an object gets destroyed. If we don’t define our own destructor, the C++ compiler creates a default destructor.
Creating Constructor and Destructor in C++? (Examples)
1. Constructor Example
Let’s take the same example of class Box to declare the constructor. Let’s assign some default values to the height and weight of the box.
Code
class Box {
public:
int height;
int weight;
// constructor
Box () {
// member initialization
height = 10;
weight = 20;
}
}
So, whenever the object of this class is created, it will return the object of type Box with default values as defined above.
2. Destructor Example
Whenever the object of Box will go out of scope, we will print out some text on the console in our example. In actually coding there can be any specific activity performed.
Code
class Box {
public:
int height;
int weight;
// constructor
Box () {
// member initialization
height = 10;
weight = 20;
}
//destructor
~Box () {
// post statements
cout << "Box Object is being deleted." << endl;
}
}
So, whenever the object of Box gets out of scope, the compiler will print out the text as defined above on the console.
Conclusion – Constructor and Destructor in C++
So, Constructor and Destructor play an important role in the object-oriented programming language. A constructor is used to initialize the object and destructor is used while destroying the object. Both the constructor and destructor are very useful while programming in the C++ language.
Recommended Articles
This is a guide to Constructor and Destructor in C++. Here we discuss the needs of Constructor and Destructor in C++, how to use, different Syntax, and Examples. You can also go through our other related articles to learn more-