some basic knowledge about constructor
Constructor
use of constructor:- constructor is used to initialize the value of variable just as we initialize the value of variable in a program.
Characteristics of constructor
- constructor dosent have a return type.
- they are used to initialize the value to variable.
- compiler has not given us permission to call constructor .
- constructor is called automatically at the time of object creation.
- constructor dosent return any value.
- constructor cannot be virtual nor can we refer to their address.
- they can make implicit calls to new and delete
- they cannot be inherited.
note:-normally people say that constructor dosent return anything but thats not true constructor return address implicitly.
yess Two types are there for return
1)explicitly return
2) implicitly return
- constructors are the examples that they return the address implicitly.
for e.g.
class a
{
a( )
{
}
}
void main()
{
a 01=new a( );
}
member are allocated to heap and refrence is created that is by a o1.
- so by creation of this object
so by taking that refrence, initialize the variables declared in class and at last return this variable that is used for storing the refrence of object .
Generally we write explicicitly return this in methods so we could know by which object the method is called .
But in the case of constructor compiler writes this.
So by returning this by the constructor ,after that the refrence of the object is stored into stack for that we have equal to sign.
Comments
Post a Comment