C++ Constructor and Destructor
In this tutorial, I will discuss about two special types of member functions in C++ namely constructor and destructor.
Constructor in C++
A constructor in C++ is a special type of member function that gets called automatically (i.e. implicitly) when an object of the class is created.
A constructor has the same name as the name of the class to which it belongs and it is always public
.
A constructor does not have a return type (not even void), since it never returns a value.
A class constructor example is shown below:
class Rectangle {
public:
// data members
int length;
int breadth;
// class constructor
Rectangle() {
length = 10;
breadth = 10;
}
};
C++ constructors are of three types —
- Default constructor
- Parameterized constructor
- Copy constructor
We will describe each of them one by one.
1) Default constructor
Below is an example of a default constructor.
#include <iostream>
using namespace std;
class Rectangle {
public:
// data members int length; int breadth;
// default constructor
Rectangle() {
length = 10;
breadth = 10;
}
// member function
int computeArea() { return length * breadth; } };
int main() {
// creating object of Rectangle class Rectangle rect;
// computing area for rect
cout << "AREA : " << rect.computeArea();
return 0; }
Output:
AREA: 100
2) Parameterized constructor
Below is an example of a parameterized constructor.
This helps us to assign initial value to an object at the time of its creation as shown in the example below −
#include <iostream>
using namespace std;
class Rectangle {
private:
// data members int length; int breadth;
public:
// parameterized constructor
Rectangle(int l, int b) {
length = l;
breadth = b;
}
// member function int computeArea() { return length * breadth; } };
int main() {
// creating objects of Rectangle class Rectangle rect1(20, 15);
Rectangle rect2(30, 20);
// computing area for rect1
cout << "AREA1 : " << rect1.computeArea() << endl;
// computing area for rect2
cout << "AREA2 : " << rect2.computeArea() << endl;
return 0; }
Output:
AREA1: 300
AREA2: 600
3) Copy constructor
Below is an example of a copy constructor. It is used to copy data of one object to another.
See the following example −
#include <iostream>
using namespace std;
class Rectangle {
private:
// data members int length; int breadth;
public:
// parameterized constructor
Rectangle(int l, int b) {
length = l;
breadth = b;
}
// copy constructor with a Rectangle object as parameter
Rectangle(Rectangle &obj) {
length = obj.length;
breadth = obj.breadth;
}
// member function
int computeArea() {
return length * breadth;
}
};
int main() {
// creating an object of Rectangle class
Rectangle rect1(20, 15);
// computing area for rect1
cout << "AREA1 : " << rect1.computeArea() << endl;
// copy contents of rect1 to another object rect2
Rectangle rect2 = rect1;
// computing area for rect2
cout << "AREA2 : " << rect2.computeArea() << endl;
return 0;
}
Output:
AREA1: 300
AREA2: 300
Destructor in C++
A destructor in C++ is a special member function that is executed whenever an object of it’s class goes out of scope.
A destructor is having the same name as the class prefixed with a tilde (~)
It doesn’t take any parameter and never returns a value.
It can be very useful for releasing resources (such as closing files, releasing memory pointers etc.) before the program ends.
Following example explains the concept of destructor −
#include <iostream>
using namespace std;
class Rectangle {
private:
// data members
int length;
int breadth;
public:
Rectangle(int l, int b); // constructor declaration
~Rectangle(); // destructor declaration
int computeArea(); // member function declaration
};
// constructor definition
Rectangle :: Rectangle(int l, int b) {
cout << "constructor called" << endl;
length = l;
breadth = b;
}
// destructor definition
Rectangle :: ~Rectangle() {
cout << "destructor called" << endl;
}
// member function definition
int Rectangle :: computeArea() {
return length * breadth;
}
int main() {
// creating object of Rectangle class
Rectangle rect(20, 10);
// computing area for rect
cout << "AREA : " << rect.computeArea() << endl;
return 0;
}
Output:
constructor called
AREA : 200
destructor called