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.

→ The constructor fn do not have any return type not even void.
→ The constructor fn is also called automatically for the object. When the object is declared that is constructed.
→  The construction function is to be declared as public member function of the class.
→  The constructor function is useful for automatic initialization of datamembers of the objects.
→  The constructor function can be declared without parameter.
→  The constructor function can be declared with parameter.
→ We can declare multiple constructions within a single class.
→ It is possible to declare constructor with default parameter.
→ The constructor function can be called explicitly as other member function.

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

Write suitable main fn:

# 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.

 

Overloading constructors :

 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.

Consider the statement.
C1, C2 (4), C3=5 ;
For object C1 constructor without parameter will be called and C1 side is initialised with 1.
For object C2 the value 4 is passed as parameter. Hence the constructor with parameter will come in picture and C2. Side will be initialized with 4.
For object C3 the values 5 is treated as parameter hence the constructor with parameter will be called automatically and C3.side will be initialized with 5.
Constructor with default parameter. The 2 constructor fn in the previous program can be replaced with a single a constructorwith default parameter. It will give the same effect as that of two independent constructors.

# 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.

Dynamic initialization using constructor:-

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.

 

Write suitable fn that will explain dynamic initialization using constructor.

# 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="<
}

counter :: counter int ( )

{

counter= 1 ;

}

void main ( )

{

counter C1,C2 (32) ;

C1.display ( ) ;

C2.display ( ) ;

C1.increment ( ) ;

C1.display ( ) ;

C2.decrement ( ) ;

C2.display ( ) ;

C2=counter (50) ;

C2.display ( ) ;

C1.counter (10) ;

C1.display ( ) ;

}

C2=counter (50) ;

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<

}
pqr :: pqr (int d)
{
data=d ;
}
pqr :: pqr (pqr &t)
{
data=t.data ;
}
void main ( )
{
pqr P1,P2 (3) ; // Normal constructor

P1.display ( ) ;

P2.display ( ) ;

Pqr P3 (P2) ; // copy constructor.

P3.display ( ) ;

Pqr P4=P1 ; // copy constructor

P4.display ( ) ;

Pqr P5 ; // Normal constructor

P5.display ( ) ;

P5=P4 ; // Assignment process.

P5.display ( ) ;
}
he constructor that excepts alias of object of same class is called as copy constructor.

Consider the statement ;

pqr p3 (p2) ;

In this statement we declare that we construct P3 and object P2, is passed as parameter. The copy constructor will be called for P3. The t will act as alias of P2.value of t.data i.e. P2 data = 3 is copied into data i.e. P3.data.
Thus we get the copy of P2 into P3. and hence the copy constructor.
Similar explanation is applicable for Pqr P4=P1; due to this statement the copy constructor will gives us copy of P1 into P4. P5=P4;
The value of P4.data will be copied into P5.data with the normal assignment process.

Detructor Function :
  1. The destructor has got same name as that of the class.
  2. The destructor do not have any return type not even void
  3. The name of the destruction fn will be proceed i.e. preferred with the  character
  4. The destructor fn will called automatically when the objects are deallocated i.e. destructed from the memory.
  5. 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<

}

counter :: counter int ( )

{

count=c ;

}

counter :: ~ counter ( )

{

cout<<"object with value"<
}

void counter :: increment ( )

{

count ++ ;

}

void counter :: decrement ( )

{

count - - ;

}

void main ( )

{

counter C1,C2 (10), C3 (25) ;

C1.display ( ) ;

C2.display ( ) ;

C3.display ( ) ;

C1.increment ( ) ;

C1.display ( ) ;

C2.decrement ( ) ;

C2. Display ( ) ;
}

O/P:- 0

10

25

1

9
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.



Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet