Constructor and Destructor is very important basic concepts of C++. Whenever we create an object, there are two special types of functions that would be created automatically if we have not written them in the program. They are known as Constructor and Destructor.
Now Let us get the basic concepts of constructor and destructor.
Constructor:-
As its name is indicating, Constructor means "which constructs something" . We will see what it is going to construct..
Definition:-
Constructor is a special member function with the same name as its class.
Syntax :-
1. class_name(){ definition }
2. class_name(argument1, argument2,...){ definition }
We are going to write a constructor example to understand concept better.
Example:
class Person
{
int age;
int weight;
int height;
public:
Person() // constructor for class Person - same name as class , no return type
{
age = 20;
weight = 60;
height = 160;
}
void showData(); //other function
void enterData(); //other function
};
Can you guess what constructor is doing in above program? If your answer is initialization. then you are absolutely correct. The main responsibility of constructor is to initialize the data members of the class.
But above constructor is having one restriction, it will initialize same values to all the objects. It is not what we want.We should be able to provide different data values to every person. Every person has different body setup.
So we need to provide different values. How to do it? It is very simple. Add one more constructor. Having more than one constructor is called constructor overloading.
Hence this is also an example of constructor overloading.
class Person
{
int age;
int weight;
int height;
public:
Person() // constructor for class Person - same name as class , no return type
{
age = 20;
weight = 60;
height = 160;
}
Person(int a,int w,int h) // added new constructor can accept different values
{
age = a;
weight = w;
height = h;
}
void showData(); //other function
void enterData(); //other function
};
NOTE :- You can write Constructor definition outside the Class as you do normally with other function.
Example:
class Person
{
int age;
int weight;
int height;
public:
Person(); // constructor for class Person - same name as class , no return type
void showData(); //other function
void enterData(); //other function
};
Person::Person() // outside of the class
{
age = 20;
weight = 60;
height = 160;
}
Use:-
Constructors are used to create, and can initialize, objects of their class type.
Call:-
When an object of a class is created, constructor for that class is called.If there is no constructor defined, DEFAULT constructor is invoked. But default constructor doesn't initialize any variable.
Some facts about Constructors :
* Constructors are special member functions with the same name as the class.
* Constructors can not return any value (nothing even void) .
* Constructors are intended to initialize the members of the class when an instance of that class is created.
* Constructors are not called directly.
* Constructors can not be virtual.
* Constructors can not be static.
* Constructors can not be const.
* Constructors can not be volatile.
* Constructors aren't automatically inherited between base and derived classes.
* There can be any number of constructors in a same class.
* They must have different parameters to distinguish them. (=> Constructors can be overloaded )
Destructors :-
Destructors are opposite of Constructors.They are called when objects are destructed. They can be used to release memory occupied by the objects.
Definition :
Destructors in C++ also have the same name, but they are preceded by a '~' operator.For example destructor for Person class will be declared as ~Person().
Syntax:
~class_name(){}
Example :
class Person {
public:
// Constructor for class Person
Person();
// Destructor for class Person
~Person();
};
Use :-
Destructors are usually used to release memory and do other cleanup for a class object and its class members when the object is destroyed.
Call:-
The destructors are called when the object of a class goes out of scope or is explicitly deleted.
Some facts about Destructors:
* If the constructor/destructor is declared as private, then the class cannot be instantiated.
* It is not necessary to declare a constructor or a destructor inside a class. If not declared, the compiler will automatically create a default one for each.
* A destructor takes no arguments and has no return type.
* Its address cannot be taken.
* Destructors cannot be declared const.
* Destructors cannot be declared volatile.
* A destructor can be declared virtual or pure virtual.
* We don't have to call destructor explicitly, it is called automatically.
* Only one destructor can be there for each object.
I hope you have understood basic concepts of Constructors and Destructors in C++. Please don't forget to send your valuable feedback.