Constructor and destructor
we know that a class is more important than the object it represents the reason being that once a class is defined any number of the object of the class type can be churned out at the runtime of the program we also know that the class is a set of specification that is used by the compiler for the purpose of construction of objects
In fact, for the creation of an object the compiler creates a function having the same name as the class itself, it may be noted that the name of the class is my class and the name of the function created by the compiler is also my class this function is called as a constructor function as the name suggests the constructor function creates an object of its corresponding class type and demand in a program
Thus the constructor function 'my class ()' would create an object 'my object' of my class type as and when the falling object creation statement is executed
my class my OBj;
It may be further noted that the class is a compile-time entity whereas the object is a runtime entity therefore the object will get the memory and the resources and not the class
For that reason, the constructor function allocates the memory space for the data items or state variables of each object. However, the member functions are allocated space only once for all instances of the object but the inline functions are duplicated for each created object.
Now the most important question is that because the constructor function is especially when it is a run time entity? The answer is that the compiler inserts the following line in the class at compile time and binds it with its constructor function
my class (){}
It is important to know that the sentence of the constructor is especially in the sense that not only its name is same as the name of the class but also it has no return type, not even void since the constructor is the first function to be called before the construction of an object the programmer can use constructors for variety of purpose as discussed in the following section
Rules of constructor definition and usage
- The name of the constructor must be the same as that of the class
- The constructor can have a parameter list
- The constructor function can be overloaded
- A constructor with no arguments is the default constructor
- If there is no constructor in a class a default constructor is generated by the compile
- A constructor is executed automatically
Types of constructor
- The default constructor
- User-defined constructor
- Parameterized constructor
- The copy constructor
Default constructor and user-defined constructor
It may be further noted that the compiler has on its own included the following statement in the class definition
my class(){}
This is called as a default constructor it can be precisely defined as a constructor which is defined and called by the compiler it does not process but reserves memory for the object it takes no parameters
Thus a default constructor with the empty body does do nothing worthwhile but it is legal at the time of the creation of an object it is executed and memory is allocated for the object. However, the programmer can also explicitly define a user-defined constructor for his her on advantage
Example:
using namespace std;
class student
{
private:
public:
student()//Default constructor
}
parameterized constructor
we can also include arguments in the constructor function definition the constructors with arguments are also known as a parameterized constructor as this is helpful in a situation where the programmer wants to provide explicit initial values at the time of the creation of an object
using namespace std;
class student
{
private:
public:
student(int x , int y)// Parameterized constructor
}
Destructor
A destructor is an inverse of a constructor in the sense of that it removes the memory of an object which was allocated by the constructed during the creation of the object it carries the same name as the class but with a tilde (~) as a prefix
{
student()
~student()
};
The rules for destructor definition and usage are
Does destructor has the same name as that of the class prefix by the title character~
The destructor cannot have to augment
It has no return type, not even void type
Destructor cannot be overloaded there can be only one destructor in a class
0 Comments