
</>
G.H PATEL COLLEGE OF
ENGINEERING AND TECHNOLOGY
TOPIC : CONSTRUCTOR AND ITS TYPES
SUBJECT : OBJECT ORIENTED PROGRAMMING WITH C++ (2140705)
PREPARED BY : TEJOY VACHHRAJANI (160110116057)
BHAVIK VASHI (160110116061)
KHYATI VALERA (160110116059)
SUBMITTED TO : PROF. BHARGESH PATEL
CLASS : B.E. IT (2ND YEAR)
BATCH : 1D16

2
CONSTRUCTOR FUNCTION
Everthought that what kind of
problems cananobject create if it’s
not initialised.
It’s similar togiving your vehiclethe final gear
just during thestartup.
TheObject
What if user
didn’t assignme
any value initially
(by chance he
forget) andfinally
askme myvalue?
I’ll behelpless.
Soto makea
program
robust the
object should
be initialised
every time.
And in classwe do this byconstructors...
•Well! It is aspecial member function that
is used for initialization of objects (data
members).
•A constructor function is called
whenever anobject is created.
•Constructors are also called when an
object is created as a part of another
object.
What is “ Constructor ” !!!

 Constructor Functions have “same name” as
that ofclassname.
 Theydo not havereturn types, not even void.
 May or maynothave parameters.
C++has default constructors that are called
whenever a class is declared even if no
function with the samenameexists
Some of it’s characteristics
Mind It!

Theyshould be declared in“public section”.
Theyare invoked “automatically”when
objects arecreated.
We cannot call it for the already
constructed object.
Some of it’s characteristics

 THERE ARE 3 TYPES OF CONSTRUCTORS:-
 1. DEFAULT CONSTRUCTOR
 2. PARAMETER CONSTRUCTOR
 3. COPY CONSTRUCTOR
TYPES OF CONSTRUCTORS

DEFAULT CONSTRUCTOR
 Becausein C++default constructor isthere.
 Thisconstructor hasno argumentsin it.
 Default Constructor is also called as no
argumentconstructor.

DEFAULT CONSTRUCTOR
 Default argument is an argument to a function that a
programmer isnot required to specify.
 C++ allow the programmer to specify default arguments
that always have avalue, even if one isnot specified when
calling the function.
 For example, in the following function
declaration:
i n t MyFunc (int a, int b, int c=12) ;


PARAMETER CONSTRUCTOR
 A parameterized constructor is just one that
hasparameters specified in it.
 We can pass the arguments to constructor
function when objectare created.
 A constructor that can take arguments are
called parameterizedconstructors.



COPY CONSTRUCTOR
 CopyConstructor is used to declare and
initializeanobject from another object.
 For example thestatement:
abc c2(c1);
 would define the object c2and at thesame time
initializeit to the valueof c1.
 Theprocess of initializing through acopy
constructor isknown ascopy initialization.

SOMETHING IMPORTANT
 Automatically called an objectis
when created.
 Wecandefine our ownconstructors
 A constructor takes the same name as the class
name.
 Wecan’t define a constructor in the
private section.
Types of Constructor in C++

Types of Constructor in C++

  • 1.
     </> G.H PATEL COLLEGEOF ENGINEERING AND TECHNOLOGY TOPIC : CONSTRUCTOR AND ITS TYPES SUBJECT : OBJECT ORIENTED PROGRAMMING WITH C++ (2140705) PREPARED BY : TEJOY VACHHRAJANI (160110116057) BHAVIK VASHI (160110116061) KHYATI VALERA (160110116059) SUBMITTED TO : PROF. BHARGESH PATEL CLASS : B.E. IT (2ND YEAR) BATCH : 1D16
  • 2.
  • 3.
    Everthought that whatkind of problems cananobject create if it’s not initialised. It’s similar togiving your vehiclethe final gear just during thestartup.
  • 4.
    TheObject What if user didn’tassignme any value initially (by chance he forget) andfinally askme myvalue? I’ll behelpless. Soto makea program robust the object should be initialised every time. And in classwe do this byconstructors...
  • 5.
    •Well! It isaspecial member function that is used for initialization of objects (data members). •A constructor function is called whenever anobject is created. •Constructors are also called when an object is created as a part of another object. What is “ Constructor ” !!!
  • 6.
      Constructor Functionshave “same name” as that ofclassname.  Theydo not havereturn types, not even void.  May or maynothave parameters. C++has default constructors that are called whenever a class is declared even if no function with the samenameexists Some of it’s characteristics Mind It!
  • 7.
     Theyshould be declaredin“public section”. Theyare invoked “automatically”when objects arecreated. We cannot call it for the already constructed object. Some of it’s characteristics
  • 8.
      THERE ARE3 TYPES OF CONSTRUCTORS:-  1. DEFAULT CONSTRUCTOR  2. PARAMETER CONSTRUCTOR  3. COPY CONSTRUCTOR TYPES OF CONSTRUCTORS
  • 9.
     DEFAULT CONSTRUCTOR  BecauseinC++default constructor isthere.  Thisconstructor hasno argumentsin it.  Default Constructor is also called as no argumentconstructor.
  • 10.
     DEFAULT CONSTRUCTOR  Defaultargument is an argument to a function that a programmer isnot required to specify.  C++ allow the programmer to specify default arguments that always have avalue, even if one isnot specified when calling the function.  For example, in the following function declaration: i n t MyFunc (int a, int b, int c=12) ;
  • 11.
  • 12.
     PARAMETER CONSTRUCTOR  Aparameterized constructor is just one that hasparameters specified in it.  We can pass the arguments to constructor function when objectare created.  A constructor that can take arguments are called parameterizedconstructors.
  • 13.
  • 14.
  • 15.
     COPY CONSTRUCTOR  CopyConstructoris used to declare and initializeanobject from another object.  For example thestatement: abc c2(c1);  would define the object c2and at thesame time initializeit to the valueof c1.  Theprocess of initializing through acopy constructor isknown ascopy initialization.
  • 18.
     SOMETHING IMPORTANT  Automaticallycalled an objectis when created.  Wecandefine our ownconstructors  A constructor takes the same name as the class name.  Wecan’t define a constructor in the private section.