Introduction of Constructor and Destructor in C++
- With the help of a constructor and destructor in C++, we can initialize and destroy the memory occupied by variables/data of user-defined data type (class) by using a specialized function as a constructor and destructor in object-oriented programming.
- In Constructor and Destructor in C++, the Constructor is used to create the object in an object-oriented programming language while the destructor is used to destroy the object.
- Constructors and destructors in C++ have no return type, not even void.
- Constructor and destructor in C++ are built-in member functions and customized member functions.
Constructor in C++
- C++ is an object-oriented programming (OOP) language that provides a special member function called a constructor for initializing an object when it is created. This is known as the automatic initialization of objects.
- The constructor has the same name as that of the class’s name.
- Its main job is to initialize the value of the class.
- Whenever an object is created in a class, the special member function (constructor) is executed automatically to allocate the memory for the newly created object.
- If a class has a constructor, each object of that class will be initialized automatically.
- A constructor for a class is needed so that the compiler automatically initializes an object as soon as it is created.
- If a class has no user-defined constructor exists then the compiler implicitly declares a default parameter constructor. This system-defined constructor is an inline public member of its class. When the compiler uses this type of constructor to create an object for a class then the constructor will have no constructor initializer and have a null body.
Characteristics/Features of Constructor
The constructor has some special characteristics which are as follows: –
- Constructor is declared in the public section of a class.
- Constructors are invoked/called directly when an object is created.
- Like other C++ functions, constructors may have default arguments.
- A constructor can be inside the class definition or outside the class definition.
- A class may have one or more constructors as needed and hence overloaded.
Limitations/Demerits of Constructor
The destructor has also some limitations which are as follows: –
- Constructors don’t have a return type, not even void, and hence can’t return any values.
- We cannot have two/more constructors with similar arguments.
- We cannot declare a virtual constructor hence we cannot call virtual methods from the constructor.
- The constructor has to compile time-bound, not dynamic/run time.
- They can’t be used in the union.
- Constructor can’t be a friend function.
- Constructors can’t be inherited through a derived class but can call the base class constructor.
Types of Constructor
There are the following types of constructors –
(A) Default Constructor
- A constructor that either has no parameters or parameters with default/constant values.
- Using the default constructor, we can declare only instances of the class in an application.
- No default constructor is created for a class that has any constant or reference type members.
(B) Parameterized Constructor
- A constructor that can take arguments for its processing is called a parameterized constructor.
- When a constructor is parameterized, we must provide appropriate arguments for the constructor.
(C) Copy Constructor
- A copy constructor is used to declare and initialize an object from another defined object.
- The process of initializing an object’s value through a copy constructor is known as copy initialization.
- A copy constructor is always used when the compiler has to create a temporary object of a class object.
- The copy constructor takes an object of their own class as arguments and produces such an object.
- The copy constructors usually do not return a function value.
- The copy constructors are used in the following situations
- The initialization of an object by another object of the same class.
- Return of objects as a function value.
- Stating the object as by value parameters of a function.
Constructor Overloading
- When more than one constructor is defined inside a class, it is called constructor overloading.
- In other words, when a class uses multiple constructors in a class for a specific purpose, it is called constructor overloading.
- It is used to increase the flexibility of a class by having more number constructors for a single class.
- It gives us more than one way to initialize objects in a class.
Destructor in C++
- A destructor is another specialized member function that is used to destroy the object’s memory when it is no longer required.
- A destructor is a special member function that has the same name as that of the class but is prefixed with a ~ (tilde) symbol.
- A destructor is defined under the public section of the class so that its objects can be destroyed in any function.
- Destructors are functions that are complementary to constructors.
- A destructor is used to destroy the objects that have been created by using a constructor.
- They de-initialize objects when they are destroyed.
- A destructor is invoked/called when an object of the class goes out of scope, or when the memory occupied by it is de-allocated using the delete operator.
- It is called automatically by the compiler when an object is destroyed.
- There’s only one destructor for each object.
Characteristics/Features/Applications of Destructor
The main application and characteristics of destructors are given as follows: –
- Destructor functions are invoked automatically when the objects are destroyed.
- We can have only one destructor for a class, i.e. destructors can’t be overloaded.
- If a class has a destructor, each object of that class will be de-initialized before the object goes out of the scope.
- It is necessary for memory regeneration consumed by the constructor. In other words, a destructor cleans up the memory that is no longer required or accessible.
- The destructor function also obeys the usual access rules as other member functions do.
- Member function may be called from within a destructor.
- Destructors are less complicated than constructors.
- A destructor can be declared virtual or purely virtual.
- An object of a class with a destructor can’t be a member of a union.
Limitations/Demerits of Destructor
- It does not return any value, not even void.
- A destructor takes no arguments and has no return type.
- It is not called explicitly.
- It is not possible to take the address of the destructor.
- Destructors cannot be declared const, volatile, const volatile, or static.
- Destructors can’t be inherited.
- They are case-sensitive.
- They are not suitable for large & complex programs having thousands of lines of code.
0 Comments