Constructor in C++ Language( part-2 )
Constructor in C++ Language
How to Call Constructor ?
Today, we take second example of 'Constructor is used to solve problem of
initialization' :--
1- First we understand why Constructor called Constructor because object become object with help
of constructor. for example:--
Here, we know in previous lecture of Constructor that at the time of object created , the constructor call and run automatically..but remember without any constructor , the object has garbage value only
and does not represent any member.
2- Now as we know above line that without any constructor,object contain garbage value and doesn't
represent to any member value this is called 'problem of initialization'
3- To avoid the above problem of initialization, we created an Constructor so that at the time of
object created constructor called and run some value so that object doesn't contain any garbage
value.
Types of Constructor :
Default Constructor :
By default constructor |
As we seen in above example, complex() {}; is empty constructor without any arguments and instructions therefore it is called by default constructor but remember if we do not created any
constructor still empty constructor created by the compiler and run because whenever object
created it has to be constructor created and run with object.
Parameterized Constructor (passing argument )
parameter in constructor |
Here, we seen in above example that at the time of Constructor Created some argument pass i.e
int x,int y therefore it called as Paremeterized Constructor.
Constructor Overloading
constructor overloading |
As we seen in above example that Constructor Overloading is nothing but a Function
overloading as we know that in Function overloading, The name of the function will be same but its argument will be different as we seen in above example that Constructor name will be same but its
argument different.Remember, "complex c1=complex(3,4), c2=5,c3,c4(3,8)" these are the different
way of calling complex constructor..
If this article is helpful to you then please like share and comments.. thank you..
Comments
Post a Comment