Constructors and Destructors Extra
Constructors and Destructors Extra
Introduction
A constructor is a special member function whose task is to
initialize the data members of an objects of its class.
It is special because it has same name as its class name.
It invokes automatically whenever a new object of its associated
class is created.
It is called constructor because it constructs the initial values of
data members and build your programmatic object.
2
Introduction
It is very common for some part of an object to require
initialization before it can be used.
Suppose you are working on 100's of objects and the default
value of a particular data member is needed to be zero.
Initialising all objects manually will be very tedious job.
Instead, you can define a constructor function which initialises
that data member to zero. Then all you have to do is declare
object and constructor will initialise object automatically.
3
Constructor Example
class add Whe a class contains
{ n
constructor, it a
int m, n ; that an object created guarantee
by
public : the
class is be d initialized
add (); will
}; automatically
.Above declaration only
add :: add () not
addcreates
a ;
{ the
m = 0; object butaalso of
add, initializes itstype
n = 0; data
} members m and n to zero.
4
Constructors
There is no need to write any statement to invoke the constructor
function.
If a ‘normal’ member function is defined for initialization, we
need to invoke that function for each and every objects
separately.
A constructor that accepts no parameters is called the
default
constructor.
The default constructor for class A will be A : : A ( )
5
Characteristics of Constructor
They must be declared in the public scope.
They are invoked automatically when the objects are created.
They do not have return types, not even void and they cannot
return values.
They cannot be inherited, though a derived class can call the base
class constructor.
Like other C++ functions, Constructors can have default
arguments.
Constructors can not be virtual.
6
Characteristics of Constructor
We can not refer to their addresses.
They make ‘implicit calls’ to the operators new and delete when
memory allocation is required.
7
Types of Constructor
Default Constructor/Non-Arg Constructor
Parameterized Constructor
Copy Constructor
Dynamic Constructor
10
Constructor
The constructor function is responsible for creation of object.
But in previous examples, we have not defined any constructor in
class, so how come the objects were created of those classes?
The answer is, If no constructor is defined in the class in such
situation the compiler implicitly provides a constructor, which is
called as default constructor.
9
Constructor
class sample class sample
{ {
int someDataMember; int someDataMember;
public: public :
void someFunction () sample()
{ {
.. After }
Compilati
.. on
void someFunction ()
} {
}; ..
..
}
};
11
Non-Arg Constructor Example
class circle In beside,
{ example
constructor function th
float radius;
public: takes
argument, e
and simply initializes
circle() radius to zero. no
{
radius = 0;
Non- constructor
} arg
}; is
also
called as default constructor.
12
Default Constructor Example
class circle In example beside, we have not
{ defined any constructor, so
float radius;
public: compiler will provide an empty
body constructor to the class,
}; which is called as default
constructor.
class circle
{
float radius;
public:
circle()
{
}
};
13
#include<iostream.h>
class rect
{
private:
int l,b,a;
public:
rect()
{ l = 50;
b = 60;
}
void getarea()
{
a= l*b;
cout<< “area of rectangle is “<<a<endl;
}
};
void main()
{
rect k;
k.getarea();
}
2.Parameterised Constructors
Sometimes it becomes necessary to initialize the various data
elements of an objects with different values when they are
created.
Thi is achieved by passing arguments to the constructor
sfunction when the objects are created.
Th constructors that can take arguments are
e
parameterized
called constructors.
15
Parameterised Constructors
class circle
{
float radius; Non-Arg (Default)
public: constructor,
circle() which takes no arguments
{
radius = 0;
}
circle(float r)
{ Parametirised constructor,
radius = r; which
} takes 1 arguments as
}; radius.
16
Parameterised Constructors
class circle Whe a constructor is
{
float radius; nparameterized, we must pass the
public: arguments to
circle() the
{ function
radius = 0; declared. constructor
} when
circle(float r)
an
{ object is
radius = r;
}
}; Consider following
declaration circle
Using this Constructor you can provide different values to data members of different
firstObject; circle
objects, by passing the appropriate values as argument.
secondObject(15); 17
#include<iostream.h>
class rect
{
private:
int l, b, a;
public:
rect( int x , int y)
{
l = x; b = y;
}
void getarea()
{ a= l*b;
cout<< “area of rectangle is “<<a<endl;
} };
void main()
{
rect k (40,50);
k.getarea();
rect k1 (60,70);
k1.getarea();
}
Two Ways of calling a Constructor
class circle o Implicit call (shorthand method)
{
float radius; circle ob(7.6);
public:
circle()
o Explicit call
{ circle ob;
radius = 0;
ob = circle(7.6);
}
circle(float r)
{
radius = r;
}
};
19
Multiple Constructors in a Class
C + + permits to use more than one constructors in a single
class.
Add( ) ; // No arguments
Add (int, // Two arguments
int) ;
20
Multiple Constructors in a Class
class add The first constructor receives no
{ arguments.
int m, n ;
public :
add ( ) {m = 0 ; n = 0 ;} The second constructor receives
add (int a, int b) two integer arguments.
{m = a ; n = b ;}
add (add & i) The third constructor receives one
{m = i.m ; n = i.n ;}
add object as an argument.
};
21
Multiple Constructors in a Class
class add Add a1;
{ Would automatically invoke the
int m, n ; first constructor and set both
public : m and n of a1 to zero.
add ( ) {m = 0 ; n = 0 ;} Add a2(10,20);
add (int a, int b) Would call the second constructor
{m = a ; n = b ;} which will initialize the data
add (add & i) members m and n of a2 to 10 and
{m = i.m ; n = 20 respectively.
i.n ;}
};
20
Multiple Constructors in a Class
class add Add a3(a2);
{ Would invoke the third
int m, n ; constructor which copies the
public : values of a2 into a3.
add ( ) {m = 0 ; n = 0 ;} This type of constructor is called
add (int a, int b) the “copy constructor”.
{m = a ; n = b ;} Construction Overloading
add (add & i) More than one constructor
{m = i.m ; n = function is defined in a class.
i.n ;}
};
23
Multiple Constructors in a Class
C + + compiler has an implicit constructor which creates
objects,
even though it was not defined in the class.
This works well as long as we do not use any other constructor in
the class.
However, once we define a constructor, we must also define the
“do-nothing” implicit constructor.
24
Constructors with Default Arguments
It is possible to define constructors with default arguments.
Consider complex (float real, float imag = 0);
The default value of the argument imag is zero.
complex C1 (5.0) assigns the value 5.0 to the real variable and 0.0 to
imag.
complex C2(2.0,3.0) assigns the value 2.0 to real and 3.0 to imag.
25
Constructors with Default Arguments
A::A() Default constructor
A : : A (int = 0) Default argument constructor
26
Dynamic Initialization of Objects
Providing initial value to objects at run
time.
Advantage We can provide various initialization
– formats, using overloaded constructors.
27
Copy Constructor
A copy constructor is used to declare and initialize an object
from another object.
• classes own object can be passed as a reference
parameter.
28
Copy Constructor
The statement
I 2 = I 1;
will not invoke the copy constructor.
30
Dynamic Constructors
Allocation of memory to objects at the time of their
construction is known as dynamic construction of objects.
31
Destructors
A destructor is used to destroy the objects that have been
created by a constructor.
32
Destructors
A destructor never takes any argument nor does it return any
value.
33
Destructors
It is a good practice to declare destructors in a program since it
releases memory space for further use.
34
#include<iostream.h>
class rect
{
private:
int l,b,a;
public:
rect()
{
l = 50; b = 60;
}
void getarea()
{
a= l*b; cout<< “area of rectangle is “<<a<endl;
}
}
~ rect()
{
cout<<”out of scope”;
}
};
void main()
{
rect k;
k.getarea();
}
Any Questions