Professional Documents
Culture Documents
INTRODUCTION
C++ provides a special member function called Constructor which enables an object
initialize itself when it is created. This is known as automatic initialization of an object.
It is also provides another special member function called Destructor that destroys the
objects when they are no longer required.
CHARACTERISTICS OF CONSTRUCTOR
There are many types of constructors are available in C++ and they are
explained below:
The default constructor is a constructor that takes no arguments. The default constructor
for a class test has the form:
test ( )
{
// initialization of data members
}
The default constructor define outside the class can be from:
test :: test ( )
{
// initialization of data members
}
#include <iostream.h>
#include <conio.h>
class test
{
int m, n ;
public :
test () // default constructor
{
m = 0 ; // when object is created the
n = 0 ; // value of m and n is 0
}
void display()
{
cout << "m=" << m << endl;
cout << "n=" << n ;
}
};
main()
{
test t1 ; //object t1 is created
// default constructor invoked
t1.display () ;
getch () ;
return 0 ;
}
output:
m = 0
n = 0
PARAMETERIZED CONSTRUCTOR
The constructor that can take arguments are called parameterized constructor. For
example class test that can take two argument of integer type can be form:
private:
int x , y ;
public:
test (int a, int b) // parameterized constructor with two arguments
{
x = a;
y = b ;
}
For example class test having a parameterized constructor with two arguments can be
For example class test having a parameterized constructor with two arguments can be
#include <iostream.h>
#include <conio.h>
class test
{
int a, b ;
public :
//parameterized constructor
test (int x, int y)
{
a = x ;
b = y ;
}
void display ()
{
cout << "a= " << a << endl ;
cout << "b= " << b << endl ;
}
};
main ()
{
clrscr () ;
//constructor called implicitly
test t1 (100, 200) ;
getch () ;
return 0 ;
}
output:
using object1 t1
a = 100
b = 200
using object2 t2
a = 25
b = 45
In a class, if we use more than one constructor with each constructor are different, like no
argument, one argument, two arguments and so on. This is called multiple constructors in
a class.
This concept is also called constructor overloading. The example of multiple constructor
in a class are:
Example (Multiple Constructors in a Class)
#include <iostream.h>
#include <conio.h>
class test
{
int a, b ;
public :
void display ()
{
cout << "a=" << a << endl ;
cout << "b=" << b << endl ;
}
};
int main ()
{
clrscr();
test t1 ;
test t2 (10) ;
test t3 (25, 35) ;
getch () ;
return 0 ;
}
output:
using object t1
a = 0
b = 0
using object t2
a = 10
b = 0
using object t3
a = 25
b = 35
#include<iostream.h>
#include<conio.h>
class maximum
{
public:
void main()
{
clrscr();
maximum m1(5);
getch();
}
Output:
COPY CONSTRUCTOR
(1) test t1 ;
test t2 (t1) ;
or
(2) test t1 ;
test t2 = t1 ;
#include <iostream.h>
#include <conio.h>
class test
{
int id ;
public :
test ()
{
id = 100 ;
}
test (test & x)
{
id = x.id ;
}
void display ()
{
cout << id << endl ;
}
};
main()
{
test t1 ;
test t2 (t1) ; //copies the value of t1 into t2
test t3 = t1 ; //copies the value of t1 into t3
getch () ;
return 0 ;
}
Output:
id of t1 : 100
id of t2 : 100
id of t3 : 100
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a , b ;
A()
{
a = 0 ;
b = 0 ;
}
A(int x, int y)
{
a = x;
b = y;
}
void show()
{
cout << "Value of a is:= " <<a << endl ;
cout << "Value of b is:= " <<b << endl ;
}
};
main()
{
clrscr();
A a1;
int p,q;
cout <<"Enter two values: ";
cin>>p>>q;
getch();
return 0;
}
Output:
In above example, the object a2 has initialize it value at runtime which will be receive by
two variables p and q, is called dynamic initialization of objects using constructor.
DYNAMIC CONSTRUCTOR
The constructor can also be used to allocate memory while creating objects. This will
enables the system to allocate the right amount of memory for each objects when the
objects are not the same size, thus resulting in the saving memory.
Allocation of memory to objects at the time of their construction is called dynamic
construction of objects.
The memory is allocated with the help of new operator.
The example of dynamic constructor with use of new operator is shown below.
#include <iostream.h>
#include <conio.h>
class DynCos
{
int * p;
public:
DynCos()
{
p=new int;
*p=10;
}
DynCos(int v)
{
p=new int;
*p=v;
}
int disp()
{
return(*p);
}
};
void main()
{
clrscr();
DynCos d1, d2(9);
cout<<"The value of object d1's p is: ";
cout<<d1.disp()<< endl;
Output:
The value of object d1s p is: 10
The value of object d1s p is: 9
DESTRUCTOR
A destructor is used to destroy the objects that have been created by constructor.
Like constructor, destructor is a member function, whose name is same as class name but
it is denoted by tilde ~ sign.
For example, destructor for class test can be form:
~ test()
{
}
The destructor destroys everything created by the class automatically. You do not need to
call it explicitly.
A destructor never takes any arguments and does not return any value.
The objects are destroyed in the reverse order of their creation.
Example (Destructor)
#include<iostream.h>
#include<conio.h>
int count = 0;
class A
{
public:
A()
{
count++;
cout<<"Object created: " << count << endl;
}
main()
{
clrscr();
A a1,a2,a3;
{
cout<<"Enter block 1"<<endl;
A a4;
}
{
cout<<"Enter block 2"<<endl;
A a5;
}
getch();
return 0;
}
Output:
Object created: 1
Object created: 2
Object created: 3
Enter block 1
Object created: 4
Object destroyed: 4
Enter block 2
Object created: 5
Object destroyed: 5
Object destroyed: 5
Object destroyed: 5