Hello friends, in this post today, we learn about what is constructor in c++ and types of constructor. so lets start...
Constructor in c++ |
Constructor:-
A constructor is a “special” member function which initializes the objects of class.
Constructor is used to solve problem of initialization.
Properties of constructor:-
Constructors are used to construct the object of the class.
Constructor name must be same as class name.
Constructor is invoked automatically when objects of the class are created.
The constructors are always declared in the public section. If declared in the private section then objects are can only be created inside the member functions but serve no purpose.
It must be an instance member function , that is, it can never be static.
Constructors do not have any return type not even void so they cannot return any value.
Constructor can also be overloaded.
Constructors cannot be inherited, but they can be called from the constructors of derived class.
Constructors cannot be virtual.
Addresses of constructors cannot be taken.
An object with a constructor cannot be used as a member of a union.
Constructor make implicit calls to operators new and delete in case memory allocation and de-allocation is to be performed.
Types of constructor :-
There are mainly three types of constructors as follows:
Types of constructor |
1. Default constructor:-
Default constructor is the one which invokes by default when object of the class is created.
It is generally used to initialize the value of the data members.
It is also called no argument constructor.
Remember:- If we do not create any constructor in a class, then compiler creates one default constructor (with no arguments) for us. And if we write a constructor with arguments or no-arguments then the compiler doesn't create a default constructor.
Example:
Output:-
The other way to write the above program would be :-
Output:
2. Parameterized constructor:-
Constructors that can take arguments are called parameterized constructors.
Sometimes it is necessary to initialize the various data elements of different objects with different values when they are created.
We can achieve this objective by passing arguments to the constructor function when the objects are created.
Example:
Output:
3. Copy constructor:-
A copy constructor is used to declare and initialize an object from another object.
For example, integer(integer &i); OR integer I2(I1).
Constructor which accepts a reference to its own class as a parameter is called copy constructor.
Remember:-The compiler always provides a one copy constructor when it is not define in a class which does a member-wise copy between objects .
Example:
Output:
All types of constructor in one example:-
Output:-
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment