Home Constructor and Destructor
Post
Cancel

Constructor and Destructor

Constructor


A constructor is a member function of a class that has the same name as the class name. It helps to initialize the object of a class.

It can either accept the arguments or not. It is used to allocate the memory to an object of the class. It is called whenever an instance of the class is created. It can be defined manually with arguments or without arguments.

There can be many constructors in a class. It can be overloaded with differing parameters.

Destructor


Like a constructor, destructor is also a member function of a class that has the same name as the class name, but it is preceded by a tilde(~) operator in C++. It helps to deallocate the memory of an object.

It is called while the object of the class is freed or deleted. In a class, there is always a single destructor without any parameters. So, it cannot be overloaded.

It is always called in the reverse order of the constructor. If a class is inherited by another class and both the classes have a destructor, then the destructor of the child class is called first, followed by the destructor of the parent or base class.

Implementation


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Object {
public:
    string name;

    // constructor
    Object(string name) {
        this->name = name;
        cout << name << " constructor called." << endl;
    }

    // destructor
    ~Object() {
        cout << name << " destructor called." << endl;
    }
};

int main() {
    Object one("one");  // constructor called

    while (true) {
        Object two("two");  // constructor called
        break;
    }   // destructor called for two
}   // destructor called for one

Output

1
2
3
4
one constructor called.
two constructor called.
two destructor called.
one destructor called.

References


This post is licensed under CC BY 4.0 by the author.

Object Copying

Process and Thread