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. The constructor is a function whose name is the same as the object with no return type.
- The name of the destructor is also similar to the name of the class but preceded by a tilde (~).
- Constructors and destructors in C++f 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.
- Constructor has the same name as that of the class’s name and 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.
- A class constructor if defined is called whenever a program creates an object of that class.
- When a constructor is declared for a class, initialization of the class objects becomes necessary after declaring the constructor.
- 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.
- Constructors don’t have a return type, not even void, and hence can’t return any values.
- Constructors can’t be inherited through a derived class but can call the base class constructor.
- Like other C++ functions, Constructors may have default arguments.
- A constructor can be inside the class definition or outside the class definition.
Limitations/Demerits of Constructor
The destructor has also some limitations which are as follows: –
- Constructors have no return type, i.e. constructor cannot return any function values as a result.
- 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.
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++
- Another specialized member function is called destructor which is used to destroy the object’s memory when it is no longer required.
- A destructor is a function that has the same name as that of the class but is prefixed with a ~ (tilde).
- Destructors are functions that are complementary to constructors.
- A destructor is used to destroy the objects that have been created by using a constructor which is necessary for memory regeneration.
- 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.
- A destructor cleans up the memory that is no longer required or accessible.
- There’s only one destructor for each object.
- A destructor is defined under the public section of the class so that its objects can be destroyed in any function.
Applications/Characteristics/Features 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.
- The destructor function also obeys the usual access rules as other member functions do.
- No argument can be provided to a destructor, and neither does it return any value.
- Member function may be called from within a destructor.
- An object of a class with a destructor can’t be a member of a union.
- Destructors are less complicated than constructors.
- A destructor can be declared virtual or purely virtual.
Limitations/Demerits of Destructor
- A destructor takes no arguments and has no return type, or explicitly returns a value not even void.
- 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