- With the help of constructor and destructor, we can initialize and destroy the memory occupied by variables/data of user defined data type (class) by using specialised function as constructor and destructor in object oriented programming.
- Constructor is used to create the object in object oriented programming language while destructor is used to destroy the object. The constructor is a function whose name is same as the object with no return type.
- The name of the destructor is also similar as name of the class preceded by tilde (~).
- Constructors and destructors have no return type, not even void.
- C++ is an object oriented programming (OOP) language which provides a special member function called constructor for initializing an object when it is created. This is known as 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 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 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.
- Constructor can be inside the class definition or outside the class definition.
Limitations/Demerits of Constructor
The destructor has also some limitation 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.
- Constructor has compile time bound, not dynamic/run time.
- They can’t be used in union.
- Constructor can’t be friend function.
Types of Constructor
There are following types of constructor –
(A) Default Constructor
- A constructor that either has no parameters, or parameters with default/constant values.
- Using 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 are called 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 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.
- When more than one constructor is defined inside a class, it is called constructor overloading.
- In another words, when a class uses multiple constructor in a class for specific purpose, it is called constructor overloading.
- It is used to increase the flexibility of a class by having more number of constructors for a single class.
- It gives us more than one way to initialize objects in a class.
- Another specialised member function is called destructor which is used to destroy the objects 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 complimentary to constructors.
- A destructor is used to destroy the objects that have been created by using 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 have destructor, each object of that class will be de initialized before the object goes out of the scope.
- Destructor function, also obey the usual access rules of as other member function do.
- No argument can be provided to a destructor, 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 pure 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 destructor.
- Destructors cannot be declared const, volatile, const volatile or static.
- Destructor can’t be inherited.
- They are case sensitive.
- They are not suitable for large & complex programs having thousand lines of code.
672 total views, 1 views today