When objects are declared that is constructed for a particular class then this objects will occupy certain memory locations. The data members of these objects will have garbage values. In object oriented applications we will prefer that this values should not be garbage rather the datamember of the objects should be automatically initialised with some predefined values.
For eg:- When we declare object of class count we will prefer its initial value to be zero than garbage. Similarly if we declare object of class abc we may prefer the side of the abc as units than the garbage ( unit abc)The constructors are the fn which are used for automatic initialization of data members of the objects.
Constructors function:
The constructor fn is a fn that has got same name as that of the class.
Construction function without parameter / default constructor:
Develop a class cube with data member side. The class should have following member function.
Setside ( ) ; 2) getside ( ) ; 3) display ( ) ; 4) A function to calculate and return the volume of the cube. 5) a construction fn that will initialize side of the cube with 1
# include
class cube ;
{
int side ;
public:
void setside (int) ;
void getside ( ) ;
void display ( ) ;
int volume ( ) ;
cube ( ) ;
};
{
cube :: cube ( ) ;
side=1 ;
}
void cube :: setside ( int s )
{
side = s ;
}
void cube :: display ( )
{
cout<<"side= "<
}
int cube :: volume ( )
{
return side x side x side ;
}
void main ( )
{
cube C1 ;
C1.display ( ) ;
cout<<"volume="<
C1.setside (5) ;
C1. display ( ) ;
cout<<"volume="<
}
side=1
volume=1
side=5
volume=125.
In the above program we declare constructor function without parameter.
Consider the declaration cube C1; with this statement we declare i.e. we construct object C1 of class cube. Memory space of two bytes will be allocated for the object ( ). The constructor fn gets called automatically for the object C1. And hence C1. Side will be initialized with value 1. Thus automatic object initialization with predefined value is obtained using constructor fn.
Limitation:-
In the above program whenever object of class cube is constructed its side will be automatically initialized with 1. It is better than having the garbage value for the side. However the limitation is the side will be compulsorily initialized with one. We overcome this limitation by writing constructor with parameter.
Develop a class cube whose description is very similar to the previous program. The only difference is define 2 construction fn within this class. One without parameter and other with parameter. Write suitable main fn.
# include
class cube{
int side ;
public:
void setside (int) ;
void getside ( ) ;
void display ( ) ;
int volume ( ) ;
cube ( ) ;
cube (int) ;
};
cube :: cube ( )
{
side=1 ;
}
cube :: cube (int 5)
{
side=5 ;
}
void cube :: setside (int s)
{
side=s ;
}
void cube :: getside ( )
{
cin>>side ;
}
void cube :: display ( )
{
cout<<"side="<
}
int cube :: volume ( )
{
return side*side*side ;
}
void main ( ) ;
{
Cube C1, C2 (4), C3=5 ;
C1.display ( ) ;
Cout<<"volume="C1.volume ( ) <<'\n' ;
C2.display ( ) ;
Cout<<"volume="<
C3.display ( );
Cout<<"volume="<
}
Side=1
volume=1
side=4
volume=6
side= 5
volume=125.
In the above class we have declare two constructors one with without parameter and other with parameters.
# include
class cube
{
int side ;
public:
: : : :
: : : :
};
cube :: cube (int 5 )
{
Side=5 ;
}
The body of set side, get side, display, volumes main will be same as that of the previous program.
Explanation:-
For object C1 since the parameter is not passed the constructor will utilized default value and hence C1.side = 1, for object C2 and C3. Since the parameter is present the default value will not be ustilised and C2.side and C3.side will be initialized with 4 and 5 respectively.
Explicit call of the constructor.Develop a class counter with data member count.
The class should have following member fn:
get count ( ) 2) display ( ) 3) increment ( ) 4) decrement ( ) 5) A constructor function that will intilise the count with the accepted value and if the value is not supplied the count should be initialized with zero.
# include
class counter{
int count ;
public:
void getcount ( ) ;
void display ( ) ;
void increment ( ) ;
void decrement ( ) ;
counter (int=0) ;
};
void counter :: get count ( )
{
cin>>count ;
}
void counter :: increment ( )
{
count ++ ;
}
void counter :: decrement ( )
{
count - - ;
}
void counter :: display ( )
{
cout<<"count="<
In the above statement the constructor fn is called explicitly for object C2 and 50 is passed as parameters. The C2.count is initialized with 50.the C2.count is initialized when the program is running. Thus the dynamic initialization is obtained using the constructor function. Similar explanation is applicable for C1.counter (10);
- Copy constructor:
Consider the following eg:-
# include
Class pqr
{
int data ;
public:
void display ( ) ;
pqr ( int=0) ; // Normal constructor
pqr (pqr &) ; // Copy constructor
};
void pqr :: display ( )
{
cout<
-
The destructor has got same name as that of the class.
-
The destructor do not have any return type not even void
-
The name of the destruction fn will be proceed i.e. preferred with the character
-
The destructor fn will called automatically when the objects are deallocated i.e. destructed from the memory.
-
The destructor also comes in picture when dynamically allocated objects are made free i.e. deleted.
Develop a class counter with datamember count.one class will have following member fn
get count ( ) ; display ( ) ; increment ( ) ; decrement ( ) ;
constructor with default parameter destructor fn with appropriate logic.
# include
class counter
{
int count ;
public:
void getcount ( ) ;
void display ( ) ;
void increment ( ) ;
void decrement ( ) ;
counter ( int=0 ) ;
~ counter ( ) ;
};
void counter :: getcount ( )
{
cin>>count ;
}
void counter :: display ( )
{
cout<
Object with value 25 will be destructed object with value a will been destructed object with value has been destructed.
Explanation:
→ In the above program we declare destructor function within the class counter. In the main function we declare i.e we construct objects C1,C2 and C3. Constructor fn will be called automatically for all the three objects in the order C1,C2, and C3.
→ When main fn is about to finish then the complies will understand that the objects are deallocated from the memory. The destructor fn will be called automatically for all the three objects in the order C3, C2,and C1. For every object the destructor fn is called first and once the destructor fn execution is finished the corresponding object is deallocated from the memory.