You are on page 1of 15

BCA-III C++ and Object Oriented Programming

CHAP-5 CONSTRUCTOR and DESTRUCTOR

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

Constructors name is same as class name


Constructor should be declared in public section
Constructors are invoked automatically when the objects are created
They do not have return type, not even void and therefore, they cannot return values.
Like other C++ functions, they can have default arguments.
We cannot refer the address of constructors.

There are many types of constructors are available in C++ and they are
explained below:

DEFAULT CONSTRUCTOR (NO ARGUMENT CONSTRUCTOR)

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
}

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Example of default constructor which passes no argument.

#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

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

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 ;
}

When we call parameterized constructor, we have to pass the initial values as an


arguments of objects.
Parameterized constructor call using two methods:

(i) Implicit Call


(ii) Explicit Call

Explicit Calling of Constructor

For example class test having a parameterized constructor with two arguments can be

explicitly call as:

test t1 = test (100 , 200); //explicit call

Implicit Calling of Constructor

For example class test having a parameterized constructor with two arguments can be

implicitly call as:

test t1 (100 , 200); //implicit call

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

The example of parameterized constructor is shown below:

#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) ;

//constructor called explicitly


test t2 = test (25, 45) ;

cout << "using object1 t1 " << endl;


t1.display () ;

cout << "using object2 t2 " << endl;


t2.display () ;

getch () ;
return 0 ;
}
output:
using object1 t1
a = 100
b = 200
using object2 t2
a = 25
b = 45

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

MULTIPLE CONSTRUCTOR IN A CLASS

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 :

test () // default constructor


{
a = 0 ;
b = 0 ;
}

test (int x) // one argument constructor


{
a = x ;
b = 0 ;
}

test (int x, int y) // two argument constructor


{
a = x ;
b = y ;
}

void display ()
{
cout << "a=" << a << endl ;
cout << "b=" << b << endl ;
}

};

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

int main ()
{
clrscr();

test t1 ;
test t2 (10) ;
test t3 (25, 35) ;

cout << "using object t1" << endl ;


t1.display () ;

cout << "using object t2" << endl ;


t2.display () ;

cout << "using object t3" << endl ;


t3.display () ;

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

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

CONSTRUCTOR WITH DEFAULT ARGUMENTS

It is possible to define constructor with default arguments, means in parameterized


constructor we can assign value to the variable.

Example (Constructors with default arguments)

#include<iostream.h>
#include<conio.h>

class maximum
{
public:

// parameterized constructor with default argument


maximum (int a, int b = 10)
{
cout << "Value of a: "<<a<<" Value of b: "<<b<<endl;
if ( a > b )
cout << "a is max" ;
else
cout << "b is max" ;
}
};

void main()
{
clrscr();
maximum m1(5);
getch();
}

Output:

Value of a is: 5 Value of b is: 10


b is max

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

COPY CONSTRUCTOR

A copy constructor is a special constructor that can be called to copy an object.


A copy constructor takes a reference to an object of the same class as itself as an
argument.
For example if we create a copy constructor for class test that can be form:
test ( test & t1 )
For calling a copy constructor there are two methods are available

(1) test t1 ;
test t2 (t1) ;
or
(2) test t1 ;
test t2 = t1 ;

Let us consider a simple example of copy constructor as shown in below program.

Example (Copy Constructors)

#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 ;
}
};

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

main()
{
test t1 ;
test t2 (t1) ; //copies the value of t1 into t2
test t3 = t1 ; //copies the value of t1 into t3

cout << "id of t1:" ;


t1.display () ;

cout << "id of t2:" ;


t2.display () ;

cout << "id of t3:" ;


t3.display () ;

getch () ;
return 0 ;
}

Output:

id of t1 : 100
id of t2 : 100
id of t3 : 100

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

DYNAMIC INITIALIZATION OF OBJECTs USING CONSTRUCTOR

A class object with a constructor must be explicitly initialized or have a default


constructor.
Class objects can be initialized dynamically too means initial value of an object may be
provided during run time.
One advantage of dynamic initialization is that we can provide various initialization
formats, using overloaded constructors.
The following example shows the declaration and use of constructor that dynamically
initialize class objects.
Example (Dynamic Initialization of Objects Using Constructors)

#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 ;
}

};

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

main()
{
clrscr();
A a1;
int p,q;
cout <<"Enter two values: ";
cin>>p>>q;

A a2(p,q); // dynamic initialization of objects


a1.show();
a2.show();

getch();
return 0;
}

Output:

Enter two values: 10


20
Value of a is:= 0
Value of b is:= 0
Value of a is:= 10
Value of b is:= 20

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.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

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.

Example (Dynamic Constructor)

#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);
}

};

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

void main()
{
clrscr();
DynCos d1, d2(9);
cout<<"The value of object d1's p is: ";
cout<<d1.disp()<< endl;

cout<<"The value of object d2's p is:"<<d2.disp();


getch();
}

Output:
The value of object d1s p is: 10
The value of object d1s p is: 9

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

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;
}

~A() // destructor same as class name with ~ sign


{
cout<<"Object destroyed: "<<count<< endl;
}
};

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

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

Prepared by | Hemang R. Chath

You might also like