Constructor and Destructor
Introduction
C++ can provide a special member function called the constructor , that enables an object to initialize , copy into another object; when it is created . The initialization of object with the help of constructor is known as automatic initialization of objects. C++ also provides member function called destructor it destroy the objects when they are not useful.
- constructor : It is a special member function whose task is to initialize the objects of the class . It's name is the same with the class name constructor is invoked automatically whenever an object of it's associated class is created. Since it constructs the value of the data member of the class , therefore it is called constructor. The definition and declaration of constructor is :
class _ name (arguments) // definition of constructor inside the class
{
------------
------------
}
class_name : : class _ name (arguments) // definition of constructor outside the class
{
------------
------------
}
When a class contains a constructor then the object created by the class will be initialized automatically . for example :Class Max
{Int x , y ;
Public :
max ( ) ; // constructor declaration
};
max :: max ( )
{
x = 0 ; y =0;
}
void main ( )
{
max m1 ; // object is created
}
The above statement max m1 ; not only creates the object m1 of type max but also initializes it's data members x and y to zero . There is no need to write any other statement to call the constructor function .
The following characteristics of constructor function as :
- They should be declared in public section.
- They are invoked automatically when the objects are created .
- They do not return any value and do not contain void ;
- They cannot be inherited .
- They can contain default arguments . Ex : max ( int x = 5, int y = 6)
- A constructor without any argument is called default constructor.
* Program to demonstrate the declaration , definition and use of constructor *?
# include<iostream .h>
# include <conio .h>Class max
{
Int x , y ;
Public :
max ( ) // default constructor
{
x = 0 ; y = 0 ;
}
Void display ( )
{
cout <<" x="<<" \+ y = << y << end l ;
};
Void main ( )
{
max m1 ;
clrscr ( );
m1.display ( );
getch( );
}
Parameterized Constructor
The constructor that can take arguments is called Parameterized constructor .The parameterized constructor may be written as :Class _ name ( arg 1 , arg 2 _ _ _ _ _ _ ) ;
Ex : Class max
{
Int x , y ;
Public :
max ( int a , int b) ;
}
max : : max ( int a , int b); // parameterized constructor
{
x = a ; y = b ;
}
When a constructor is declared as parameterized constructor then the object declaration such as : Max m1 ; . We must pass the initial values as arguments to the constructor function when an object is declared . This can be done in two ways :
- Explicit calling
- Implicit calling
The following declaration illustrates the explicit calling of constructor max m1 = max (10, 20);
This statement creates an object m1 of type max and passes the value 10 & 20 to it. The max m1( 10, 20);
The above method is called implicit calling of constructor or shorthand method . It is very useful and easy to implement .
- Example of parameterized constructor :
{
int a , b , c;
Public :
max ( int i , int j)
{
x = i ; y =j ;
}
Void product ( )
{
c= a* b ;
cout << " multiplication = "<< c ;
}
};
Void main ( )
{
max m1 (10 , 15);
m1 . product ( ) ;
getch ( );
}
* Program to calculate factorial of a number using constructor *?
#include <iostream .h>#include <conio.h>
Class Fact
{Int n ;
Public :
Fact (int b)
{
int f =1;
n = b ;
while ( n>1)
{
f = f * n ;
n = n - 1 ;
cout <<" factorial ="<< f
}
}
};Void main ( )
{
clrscr ( );
fact A (5) ;
getch ( );
}
* Program to calculate area of triangle using parameterized constructor :?
#include < iostream .h>#include < conio .h>
Class AOT
{Private :
Float base , height ;
Public :
AOT ( int x , int y)
{
base = x ; height = y ;
}
Void area ( )
{
Int a ;
a = ( base * height ) / 2;
cout << " area of triangle = "<< a ;}
};
Void main ( )
{
AOT A1 (5.5 , 6.12 );
Clrscr ( );
A1 . area ( ) ;
getch ( ) ;
}
- Copy Constructor : A copy constructor is used to declare and initialize an object with the help of another object of the same class . The copy constructor can be declared as class _ name ( class _ name & obj) ; for ex : if max is a class then copy constructor of class is declared as max( max & m1 ) . Where m1 is a pointer to an object of the class max that is a copy constructor takes a reference to an object of the same class as argument. A copy constructor outside the class may be defined as:
Class _ name : : class _ name ( class _ name and obj){
--------------
--------------
--------------
}
* Program to demonstrate the use of copy constructor* ?
# include <iostream .h ># include <conio.h>
Class max
{
int x , y ;
Public :
max ( int i , int j ) // parameterized constructor
{
x = i ; y = j ;
}
max ( max & A ) // copy constructor
{
x = A.x ; y = A.y ;
}
Void display ( )
{
cout << "x = "<< x << "\+y ="<<y<< end l ;
}
};Void main ( )
{
max m1 ( 10 ,20) ; // calling parameterized constructor
max m2 ( m1) ; // calling copy constructor
max m3 = m1 ; // calling copy constructor
clrscr ( ) ;
cout << " object m1 \n; m1.display( ) ;
cout << " object m2 \n; m2.display( ) ;
cout << " object m3 \n; m3.display( ) ;
getch ( );
}
- Multiple Constructor in a class : C++ provides a facility to use multiple constructor in a class. The constructor may be default constructor , copy constructor , parameterized constructor etc .
Example of multiple constructor
Class base{
Private :
Int a , b ;
Public :
base ( )
{
a = 0; b =0 ;
}
base ( int x , int y)
{
a = x ; b = y ;
}
base ( base & b1)
{
a = b1.a ; b = b1 .b ;
}
Void display ( )
{
cout << " a = " << a << "\t b = " << b << end l ;
}
};
Void main ( )
{
base b2 ; // default constructor calling
base b3( 10 , 20) ; // parameterized constructor calling
base b4 (b3 ) ;
base b5 ( b2) ; // copy constructor calling
cout << " object b2 \n; b2.display( ) ;
cout << " object b3 \n; b3.display( ) ;
cout << " object b4 \n; b4.display( ) ;
cout << " object b5 \n; b5.display( ) ;
getch ( ) ;
}
Output :
object b2
a =0 b =0
object b3
a =10 b =20
object b4
a =10 b =20
object b5
a =0 b =0
Constructor with default Arguments : When a constructor contains arguments with default value , then it is called constructor with default arguments . Ex : Sum (int x , int y =15).In the above declaration of constructor the default value of argument y is 15 . When we call this constructor as sum (10);The value 10 will be assign to argument x and the value to argument y while the following statement : sum ( 10 ,35) assign value 10 to x 35 to y . In this call of constructor the actual value 35 overrides the default value 15 . The default argument constructor can be called with one argument or with no argument . when it is called with no argument it known as default constructor.Program with default argument constructor?Class date{
Int day , month , year ;
Public :
date ( int d = 26, int m = 1, int y = 2014)
{
day d ; month = m ; year = y;
}
Void display ( )
{
cout << day << "/"<< month <<"/" year << end l;
}
};
Void main ( )
{
date d1 , d2 (15), d3( 15 , 8) , d4 ( 15 , 8, 2014);
Clrscr( );
cout<<"date d1 = \t "; di.display( );
cout<<"date d1 = \t "; d2.display( );
cout<<"date d1 = \t "; d3.display( );
cout<<"date d1 = \t "; d4.display( );
getch ( );
}
Default Arguments : When the arguments of any function contain default values then the arguments are called default arguments . If the function is invoked with no arguments then the function works on default values of the arguments. If function is invoked with actual values then the actual value override the default values of the arguments at the function call and function works on actual arguments.
Program to calculate addition of three number using default arguments ?
Class base{
Private :
Int x , y , z ;
Public:
Int i =50 , int j = 70 , int k = 90
{
x= i ; y = j ; z = k;
cout <<" x + y+ z ="<< x+y+z << end l;
}
Void sum ( )
{
cout <<x<<"+"<<y"+"<<z<<"="<<x+y+z<<end l;
}
};
Void main ( )
{
Clrscr ( );
base b1 , b2 (10) , b3(10,20), b4(10,20,30);
cout <<"object b1 =\t" ; b1.sum ( ) ;
cout <<"object b2 =\t" ; b2.sum ( ) ;
cout <<"object b3 =\t" ; b3.sum ( ) ;
cout <<"object b4 =\t" ; b4.sum ( ) ;
getch( );
}
Destructor
It is also a member function that stores the name of the class and is used to destroy the object created by the constructor . The destructor is preceded by a tilde(~) operator . For example if max is a class then the destructor of class max may declared and defined as :~ max ( )
{
--------
--------
--------
}
A destructor neither takes arguments nor return any value . It is invoked by the compiler when the program exits or any function or block exits . It cleans up the storage that is not useful .
Program to understand the use of destructor ?
# include <iostream.h># include <conio.h>
int A = 1;
Class des
{
Public :
des ( )
{
cout << A << "\+\t";
A = A+1 ;
}
~ des ( )
{
A = A - 1;
cout << A<<"\t \t ";
}
};
Void main ( )
{
clrscr ( );
{
des d1 , d2 , d3 , d4 ;
cout <<"===========\n";
} // destructor calling
getch ( );
}
0 comments:
Post a Comment