7/29/2019 10 Cons & desc
1/22
CONSTRUCTORS
&DESTRUCTORS
7/29/2019 10 Cons & desc
2/22
Constructor
Special Member Function used for Initialization -- Same Name as theClass Name
Does NOT Return a Value
Cannot be virtual and static
Implicitly Invoked When Objects are Created or Copied
Can Have Arguments
Cannot be Explicitly Called
Multiple Constructors are Allowed
Default Constructor -- No Arguments -- Explicit or Implicit
Copy Constructor -- Explicit or Implicit
7/29/2019 10 Cons & desc
3/22
What is a constructor?
It is a member function which initializes a class.
A constructor has:
(i) the same name as the class itself(ii) no return type
7/29/2019 10 Cons & desc
4/22
Constructor
Constructorwhats this?
method used for initializing objects (of certain class)
a recipe for creating object of a given class
Constructordo we need it?
do the class variables need initialization?
7/29/2019 10 Cons & desc
5/22
Constructoran unusual method
We cannot not specify the return value (even: no
void).
We cannot call it for the already constructedobject.
We cannot get its address.
It is not even visible in the class scope and has no
name (according to ANSI).
7/29/2019 10 Cons & desc
6/22
ConstructorExample 1
/* A Simple Date Class With Constructors */#define size 50
class date{
private:int day, month, year;
char *string_date;
public:
date(); //defaultdate(int ip_day, int ip_month, int ip_year);
date(char *ip_date);
};
7/29/2019 10 Cons & desc
7/22
Types of Constructors
Default Constructor : - A constructor that
accepts no arguments is called the default
constructor.
eg: - x::x()
If no such constructor is defined, then the
compiler supplies a default constructor
Parameterized Constructor: -The constructor
that can take arguments are called
parameterized constructor.
7/29/2019 10 Cons & desc
8/22
Example Default Constructor
class integer
{ int m,n;
public:integer( ); // default constructor declared
void getinfo( );
};
7/29/2019 10 Cons & desc
9/22
Simple Program
// default constructor
#includeUsing namespace std;
class integer
{
int m,n;public:
integer(void); // constructor declared
void getinfo( );
};
integer :: integer void( ) // constructor defined
{
m= 0;
n=0;
}
7/29/2019 10 Cons & desc
10/22
Example: parameterized
constructor
class ABC{ int i;
public:
float j; char k;ABC(int a, float b, char c) //parameterized
{ i=a;
j=b;k=c; }
:
};
7/29/2019 10 Cons & desc
11/22
Class integer
{
Int m, n;
Public :
Integer (int x, int y);
};
Integer :: integer(int x,int y)
{m= x;
n=y;
}
By Calling the Constructor explicitly
By Calling the consructor implicitly
7/29/2019 10 Cons & desc
12/22
We must pass the initial values as arguments
to the constructor function when an object is
declared. This can be done in two ways:
(1) by calling the function explicitly
eg: integer int1 = integer(0,100);
(2) by calling the function implicitly
eg: integer int1(0,100);
7/29/2019 10 Cons & desc
13/22
Copy Constructor
Used to declare & initialize object from
another object.
eg: integer L2(L1);
would define the object L2 & at the same
time initialize it to the values of L1.Another
form of this statement is
integer L2 = L1;
7/29/2019 10 Cons & desc
14/22
Copy Constructor
1. The syntax for a copy constructor declaration is
class_name :: class_name (class_name &ptr)2. The Copy Constructor may be used in the following
format also using a const keyword.
class_name :: class_name (const class_name & ptr)
3. Copy Constructor are always used when the compiler hasto create a temporary object of a class object.The copyconstructor are used in the following situations:-
4. The Initialization of an object by another object of thesame class.
5. Return of object as a function value.6. Stating the object as by value parameters of a function.
7/29/2019 10 Cons & desc
15/22
Copy Constructor Example with Program
//fibonacci series using copy constructor
#include
Using namespaces std ;
Class fibonacci {
Private :
Unsigned long int f0,f1,fib;
Public :
Fiboancci () // constructor
{F0=0;
F1=1;
Fib=f0+f1;
}
Fibonacci (fibonacci &ptr) // copy construtor
{
F0=ptr.f0;
F1=ptr.f1;
Fib=prt.fib;
}
Void increment ( )
{
F0=f1;
F1=fib;
Fib=f0+f1;}
Void display ( )
{
Cout
7/29/2019 10 Cons & desc
16/22
Dynamic Constructor
Used to allocate memory while creatingobjects. This will enable the system to
allocate the right amount of memory for each
object when the objects are not of the same
memory.
Allocation of memory to object at the time of
their construction is known as dynamic
construction of objects. The memory is allocated with the help of the
new operator.
7/29/2019 10 Cons & desc
17/22
Destructor
What is the destructor?
Easy to guess
When do we need it?
Even easier
7/29/2019 10 Cons & desc
18/22
Destructor
Special Member Function used for Clean-up -- Same Nameas the Class Name with a ~
One Single Destructor
Invoked When an Object Goes Out of Scope
Can be Explicitly Called -- Unusual
Cannot Accept Parameters and Does Not Return A Value
Cannot be Declared static, const or volatile
Can be virtual
7/29/2019 10 Cons & desc
19/22
DestructorExample 1
/* A Simple Date Class With a Destructor */
#define size 50
class date{
private:
int day, month, year; char *string_date;
public:
date(char *ip_date);~date(); //Destructor
};
7/29/2019 10 Cons & desc
20/22
Destructor
as opposed to constructor it is in the class scope
we may call it
destructor, if not defined by programmer, is
generated by compiler it has empty body, but
7/29/2019 10 Cons & desc
21/22
Order of calling constructors and
destructors
Constructors
1. base class (classes in order of declaration)
2. class members in order of declaration
3. constructors body
Destructorssimply opposite order
1. destructors body
2. destructors of class members (order opposite todeclaration)
3. destructor of the base class (classes in order opposite todeclaration)
7/29/2019 10 Cons & desc
22/22
Thank You!!!