SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1
Haresh Jaiswal
Rising Technologies, Jalna.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2
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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3
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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4
Constructor Example
 When a class contains a
constructor, it is guaranteed
that an object created by the
class will be initialized
automatically.
 add a ;
 Above declaration not only
creates the object a of type
add, but also initializes its data
members m and n to zero.
class add
{
int m, n ;
public :
add ();
};
add :: add ()
{
m = 0;
n = 0;
}
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5
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 ( )
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6
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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7
Characteristics of Constructor
 We can not refer to their addresses.
 An object with a constructor (or destructor) can not be used as a
member of a union.
 They make ‘implicit calls’ to the operators new and delete when
memory allocation is required.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8
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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9
Constructor
class sample
{
int someDataMember;
public:
void someFunction ()
{
..
..
}
};
class sample
{
int someDataMember;
public :
sample()
{
}
void someFunction ()
{
..
..
}
};
After
Compilation
Compiler has implicitly added a constructor to the class, which has
empty body, because compiler is not supposed to put any logic in that.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10
Types of Constructor
 Default Constructor/Non-Arg Constructor
 Parameterized Constructor
 Copy Constructor
 Dynamic Constructor
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11
Default Constructor
 A constructor without any parameter is known as non-arg
constructor or simply default constructor.
 If no constructor is defined in a class, then compiler implicitly
provides an empty body constructor which is called as default
constructor.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12
Non-Arg Constructor Example
 In example beside, the
constructor function takes no
argument, and simply initializes
radius to zero.
 Non-arg constructor is also
called as default constructor.
class circle
{
float radius;
public:
circle()
{
radius = 0;
}
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13
Default Constructor Example
 In example beside, we have not
defined any constructor, so
compiler will provide an empty
body constructor to the class,
which is called as default
constructor.
class circle
{
float radius;
public:
};
class circle
{
float radius;
public:
circle()
{
}
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14
Parameterised Constructors
 Sometimes it becomes necessary to initialize the various data
elements of an objects with different values when they are
created.
 This is achieved by passing arguments to the constructor
function when the objects are created.
 The constructors that can take arguments are called
parameterized constructors.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15
Parameterised Constructors
class circle
{
float radius;
public:
circle()
{
radius = 0;
}
circle(float r)
{
radius = r;
}
};
Non-Arg (Default) constructor,
which takes no arguments
Parametirised constructor, which
takes 1 arguments as radius.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16
Parameterised Constructors
 When a constructor is
parameterized, we must pass the
arguments to the constructor
function when an object is
declared.
 Consider following declaration
circle firstObject;
circle secondObject(15);
class circle
{
float radius;
public:
circle()
{
radius = 0;
}
circle(float r)
{
radius = r;
}
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17
Two Ways of calling a Constructor
o Implicit call (shorthand method)
 circle ob(7.6);
o Explicit call
 circle ob;
 ob = circle(7.6);
class circle
{
float radius;
public:
circle()
{
radius = 0;
}
circle(float r)
{
radius = r;
}
};
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18
Multiple Constructors in a Class
 C + + permits to use more than one constructors in a single class.
 Add( ) ; // No arguments
 Add (int, int) ; // Two arguments
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19
class add
{
int m, n ;
public :
add ( ) {m = 0 ; n = 0 ;}
add (int a, int b)
{m = a ; n = b ;}
add (add & i)
{m = i.m ; n = i.n ;}
};
 The first constructor receives no
arguments.
 The second constructor receives
two integer arguments.
 The third constructor receives one
add object as an argument.
Multiple Constructors in a Class
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20
Multiple Constructors in a Class
class add
{
int m, n ;
public :
add ( ) {m = 0 ; n = 0 ;}
add (int a, int b)
{m = a ; n = b ;}
add (add & i)
{m = i.m ; n = i.n ;}
};
 Add a1;
 Would automatically invoke the
first constructor and set both m
and n of a1 to zero.
 Add a2(10,20);
 Would call the second constructor
which will initialize the data
members m and n of a2 to 10 and
20 respectively.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21
Multiple Constructors in a Class
class add
{
int m, n ;
public :
add ( ) {m = 0 ; n = 0 ;}
add (int a, int b)
{m = a ; n = b ;}
add (add & i)
{m = i.m ; n = i.n ;}
};
 Add a3(a2);
 Would invoke the third
constructor which copies the
values of a2 into a3.
 This type of constructor is called
the “copy constructor”.
 Construction Overloading
 More than one constructor
function is defined in a class.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22
Multiple Constructors in a Class
class complex
{
float x, y ;
public :
complex ( ) { }
complex (float a)
{ x = y = a ; }
complex (float r, float i)
{ x = r ; y = i }
------
};
 complex ( ) { }
 This contains the empty body and
does not do anything.
 This is used to create objects
without any initial values.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 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.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25
Constructors with Default Arguments
 A : : A ( )  Default constructor
 A : : A (int = 0)  Default argument constructor
 The default argument constructor can be called with either one
argument or no arguments.
 When called with no arguments, it becomes a default
constructor.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26
Dynamic Initialization of Objects
 Providing initial value to objects at run time.
 Advantage – We can provide various initialization
formats, using overloaded constructors.
This provides the flexibility of using
different format of data at run time
depending upon the situation.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27
Copy Constructor
A copy constructor is used to declare and initialize an object
from another object.
integer (integer & i) ;
integer I 2 ( I 1 ) ; or integer I 2 = I 1 ;
The process of initializing through a copy constructor is known as
copy initialization.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28
Copy Constructor
The statement
I 2 = I 1;
will not invoke the copy constructor.
If I 1 and I 2 are objects, this statement is legal and assigns the
values of I 1 to I 2, member-by-member.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29
Copy Constructor
 A reference variable has been used as an argument to the copy
constructor.
 We cannot pass the argument by value to a copy constructor.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30
Dynamic Constructors
 The constructors can also be used to allocate memory while
creating objects.
 This will enable the system to allocate the right amount of
memory for each object when the objects are not of the same
size.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31
Dynamic Constructors
 Allocation of memory to objects at the time of their
construction is known as dynamic construction of objects.
 The memory is created with the help of the new operator.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32
Destructors
 A destructor is used to destroy the objects that have been
created by a constructor.
 Like constructor, the destructor is a member function whose
name is the same as the class name but is preceded by a tilde.
eg: ~ integer ( ) { }
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33
Destructors
 A destructor never takes any argument nor does it return any
value.
 It will be invoked implicitly by the compiler upon exit from the
program – or block or function as the case may be – to clean
up storage that is no longer accessible.
Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34
Destructors
 It is a good practice to declare destructors in a program since it
releases memory space for further use.
 Whenever new is used to allocate memory in the constructor,
we should use delete to free that memory.

Weitere ähnliche Inhalte

Was ist angesagt?

Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in pythonTMARAGATHAM
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlibPiyush rai
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 

Was ist angesagt? (20)

Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
structure and union
structure and unionstructure and union
structure and union
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
07 java collection
07 java collection07 java collection
07 java collection
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Python set
Python setPython set
Python set
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 

Andere mochten auch

Andere mochten auch (16)

constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++
 
03. oop concepts
03. oop concepts03. oop concepts
03. oop concepts
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory Methods
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructor
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Ähnlich wie 04. constructor & destructor

constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to classHaresh Jaiswal
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsKuntal Bhowmick
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsKuntal Bhowmick
 
C questions
C questionsC questions
C questionsparm112
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxSurajgroupsvideo
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 

Ähnlich wie 04. constructor & destructor (20)

constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 
C questions
C questionsC questions
C questions
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptx
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 

04. constructor & destructor

  • 1. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 1 Haresh Jaiswal Rising Technologies, Jalna.
  • 2. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 2 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.
  • 3. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 3 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.
  • 4. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 4 Constructor Example  When a class contains a constructor, it is guaranteed that an object created by the class will be initialized automatically.  add a ;  Above declaration not only creates the object a of type add, but also initializes its data members m and n to zero. class add { int m, n ; public : add (); }; add :: add () { m = 0; n = 0; }
  • 5. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 5 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 ( )
  • 6. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 6 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.
  • 7. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 7 Characteristics of Constructor  We can not refer to their addresses.  An object with a constructor (or destructor) can not be used as a member of a union.  They make ‘implicit calls’ to the operators new and delete when memory allocation is required.
  • 8. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 8 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. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 9 Constructor class sample { int someDataMember; public: void someFunction () { .. .. } }; class sample { int someDataMember; public : sample() { } void someFunction () { .. .. } }; After Compilation Compiler has implicitly added a constructor to the class, which has empty body, because compiler is not supposed to put any logic in that.
  • 10. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 10 Types of Constructor  Default Constructor/Non-Arg Constructor  Parameterized Constructor  Copy Constructor  Dynamic Constructor
  • 11. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 11 Default Constructor  A constructor without any parameter is known as non-arg constructor or simply default constructor.  If no constructor is defined in a class, then compiler implicitly provides an empty body constructor which is called as default constructor.
  • 12. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 12 Non-Arg Constructor Example  In example beside, the constructor function takes no argument, and simply initializes radius to zero.  Non-arg constructor is also called as default constructor. class circle { float radius; public: circle() { radius = 0; } };
  • 13. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 13 Default Constructor Example  In example beside, we have not defined any constructor, so compiler will provide an empty body constructor to the class, which is called as default constructor. class circle { float radius; public: }; class circle { float radius; public: circle() { } };
  • 14. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 14 Parameterised Constructors  Sometimes it becomes necessary to initialize the various data elements of an objects with different values when they are created.  This is achieved by passing arguments to the constructor function when the objects are created.  The constructors that can take arguments are called parameterized constructors.
  • 15. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 15 Parameterised Constructors class circle { float radius; public: circle() { radius = 0; } circle(float r) { radius = r; } }; Non-Arg (Default) constructor, which takes no arguments Parametirised constructor, which takes 1 arguments as radius.
  • 16. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 16 Parameterised Constructors  When a constructor is parameterized, we must pass the arguments to the constructor function when an object is declared.  Consider following declaration circle firstObject; circle secondObject(15); class circle { float radius; public: circle() { radius = 0; } circle(float r) { radius = r; } };
  • 17. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 17 Two Ways of calling a Constructor o Implicit call (shorthand method)  circle ob(7.6); o Explicit call  circle ob;  ob = circle(7.6); class circle { float radius; public: circle() { radius = 0; } circle(float r) { radius = r; } };
  • 18. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 18 Multiple Constructors in a Class  C + + permits to use more than one constructors in a single class.  Add( ) ; // No arguments  Add (int, int) ; // Two arguments
  • 19. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 19 class add { int m, n ; public : add ( ) {m = 0 ; n = 0 ;} add (int a, int b) {m = a ; n = b ;} add (add & i) {m = i.m ; n = i.n ;} };  The first constructor receives no arguments.  The second constructor receives two integer arguments.  The third constructor receives one add object as an argument. Multiple Constructors in a Class
  • 20. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 20 Multiple Constructors in a Class class add { int m, n ; public : add ( ) {m = 0 ; n = 0 ;} add (int a, int b) {m = a ; n = b ;} add (add & i) {m = i.m ; n = i.n ;} };  Add a1;  Would automatically invoke the first constructor and set both m and n of a1 to zero.  Add a2(10,20);  Would call the second constructor which will initialize the data members m and n of a2 to 10 and 20 respectively.
  • 21. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 21 Multiple Constructors in a Class class add { int m, n ; public : add ( ) {m = 0 ; n = 0 ;} add (int a, int b) {m = a ; n = b ;} add (add & i) {m = i.m ; n = i.n ;} };  Add a3(a2);  Would invoke the third constructor which copies the values of a2 into a3.  This type of constructor is called the “copy constructor”.  Construction Overloading  More than one constructor function is defined in a class.
  • 22. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 22 Multiple Constructors in a Class class complex { float x, y ; public : complex ( ) { } complex (float a) { x = y = a ; } complex (float r, float i) { x = r ; y = i } ------ };  complex ( ) { }  This contains the empty body and does not do anything.  This is used to create objects without any initial values.
  • 23. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 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. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 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. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 25 Constructors with Default Arguments  A : : A ( )  Default constructor  A : : A (int = 0)  Default argument constructor  The default argument constructor can be called with either one argument or no arguments.  When called with no arguments, it becomes a default constructor.
  • 26. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 26 Dynamic Initialization of Objects  Providing initial value to objects at run time.  Advantage – We can provide various initialization formats, using overloaded constructors. This provides the flexibility of using different format of data at run time depending upon the situation.
  • 27. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 27 Copy Constructor A copy constructor is used to declare and initialize an object from another object. integer (integer & i) ; integer I 2 ( I 1 ) ; or integer I 2 = I 1 ; The process of initializing through a copy constructor is known as copy initialization.
  • 28. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 28 Copy Constructor The statement I 2 = I 1; will not invoke the copy constructor. If I 1 and I 2 are objects, this statement is legal and assigns the values of I 1 to I 2, member-by-member.
  • 29. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 29 Copy Constructor  A reference variable has been used as an argument to the copy constructor.  We cannot pass the argument by value to a copy constructor.
  • 30. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 30 Dynamic Constructors  The constructors can also be used to allocate memory while creating objects.  This will enable the system to allocate the right amount of memory for each object when the objects are not of the same size.
  • 31. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 31 Dynamic Constructors  Allocation of memory to objects at the time of their construction is known as dynamic construction of objects.  The memory is created with the help of the new operator.
  • 32. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 32 Destructors  A destructor is used to destroy the objects that have been created by a constructor.  Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde. eg: ~ integer ( ) { }
  • 33. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 33 Destructors  A destructor never takes any argument nor does it return any value.  It will be invoked implicitly by the compiler upon exit from the program – or block or function as the case may be – to clean up storage that is no longer accessible.
  • 34. Rising Technologies, Jalna (MH). + 91 9423156065, http://www.RisingTechnologies.in 34 Destructors  It is a good practice to declare destructors in a program since it releases memory space for further use.  Whenever new is used to allocate memory in the constructor, we should use delete to free that memory.