SlideShare a Scribd company logo
2
Most read
14
Most read
15
Most read
CONSTRUCTORS
AND
DESTRUCTORS
Harinder Singh
Sukhpal Singh
Thapar University, Patiala
Batch: 2011-2013
WHAT IS A CONSTRUCTOR ?
 In c++ , a constructor is a ‘special’ member
function whose task is to initialize the objects
of its class.
 It is special because its name is the same as
the class name.
 It is called constructor because it constructs
the value of data members of the class.
 The constructor is invoked whenever an object
of its associated class is created.
ssgill©tupatiala
CLASS WITH A CONSTRUCTOR
Class integer {
int m, n;
public:
integer(void); // constructor declared
};
Integer : : integer (void) // constructor defined
{
m=0; n=0;
}
Object declartion:
Integer int1; // object int1 created
ssgill©tupatiala
CHARACTERISTICS OF THE CONSTRUCTOR
 They should be declared in public section.
 They are invoked automatically when the objects are
created.
 They don't have return types, cannot return values.
 A constructor that accepts no parameters is called
default constructor. The default constructor for class
A is A:: A() . The statement A a; invokes the default
constructor of the compiler to create the object a.
ssgill©tupatiala
PARAMETERIZED CONSTRUCTOR
The constructor that can take arguments.
Class integer {
int m, n;
public:
integer(int x, int y); //parameterized constructor
};
Integer : : integer (int x, int y)
{
m=x; n=y;
}
Object declaration statement “integer int1 ; “ do not work
here.
ssgill©tupatiala
CALL IN PARAMETERIZED CONSTRUCTOR
 We must pass the initial values as arguments to
the constructor function when object is created.
This can be done in two ways:
 By calling the constructor explicitly.
integer int1 = integer(0, 100);
 By calling the constructor implicitly.
integer int1(0, 100);
This method is also called shorthand method.
ssgill©tupatiala
EXPLICIT/IMPLICIT CALL
int main()
{
integer int1(0, 100); //implicit call
integer int2 = integer(25, 75); //explicit call
cout<<“object 1:”
int1.function1();
cout<<“object 2:”
int2.function1();
return 0;
}
Output
object1: m=0 and n= 100
object2: m=25 and n= 75
ssgill©tupatiala
MULTIPLE CONSTRUCTOR IN A CLASS
Class integer
{
int m, n;
public:
integer() //default constructor
{m=0; n=0;}
integer(int a, int b) //parameterized constructor
{m=a; n=b;}
integer(integer & i) //copy constructor
{m= i.m; n= i.n;}
};
ssgill©tupatiala
CONSTRUCTORS WITH DEFAULT ARGUMENTS
complex(float real , float imag=0)
{
}
 complex c1(5.0);
 complex c2(2.0,3.0);
 Difference b/w A::A() and A::A(int a=0).
ssgill©tupatiala
DYNAMIC INITIALIZATION OF OBJECTS
Provide various initialization formats using
overloaded constructors.
 Fixed_deposit() { }
 Fixed_deposit(long int p, int y ,float r=0.12);
 Fixed_deposit( long int p, int y, int r);
ssgill©tupatiala
COPY CONSTRUCTOR
 It is used to declare and initialize an object
from another object.
e.g
• code() { }
• code( int a) {id = a;}
• code( code &x){ id = x.id; } //copy constructor.
 And various calls for them are:
• code A(100); // object A is Created and initialized.
• code B(A); // copy constructor called.
• code C = A; // copy constructor called again.
• code D; // object D is created , not initialized
D=A; // copy constructor is not called.
ssgill©tupatiala
DYNAMIC CONSTRUCTORS
 It is used to allocate memory while creating
objects.
• Enable the system to allocate right amount of
memory for each object when objects are not of
same size.
• Thus resulting in saving of memory.
ssgill©tupatiala
DYNAMIC CONSTRUCTORS
class String
{
char *name;
int length;
public:
String ()
{ length = 0;
name = new char[length+1];
}
String (char *s)
{ length = strlen(s);
name = new char[length+1];
strcpy(name, s);
}
};
main()
{
Char *first = “ME_SE_CR” ;
String name1( first);
String name2(“ Rupinder”);
.
.
.
}
ssgill©tupatiala
DESTRUCTORS
 ~ integer() { }
 As new is used to allocate memory in
dynamic constructors, here we use delete to
free that memory.
 Never take any argument nor does it return any
value.
 It will be invoked implicitly by compiler upon
exit from program or a block or a function.
ssgill©tupatiala
DESTRUCTORS
Int count = 0;
class test1
{
public :
~test1()
{
count ++;
cout <<“n no. of object created “
<< count ;
}
~test1 ()
{ cout <<“n no. of object destroyed”
<< count ;
Count - - ;
}
};
main()
{
cout << “n Enter Main n”;
test1 t1, t2, t3, t4;
{
cout <<“n Entered in block1” ;
test1 t5;
}
{
cout <<“n Entered in block2” ;
test1 t6;
}
cout << “n Re-Enter Main n”;
return 0;
}
ssgill©tupatiala
OUT PUT
 Enter Main
No. of object created 1
No. of object created 2
No. of object created 3
No. of object created 4
 Enter block 1
No. of object created 5
No. of object destroyed 5
 Enter block 2
No. of object created 5
No. of object destroyed 5
 Re - Enter Main
No. of object destroyed 4
No. of object destroyed 3
No. of object destroyed 2
No. of object destroyed 1
ssgill©tupatiala
THANK YOU
ssgill©tupatiala

More Related Content

PPTX
Templates in c++
PPT
Basics of C programming
PPTX
Inheritance in C++
PPTX
Storage class in C Language
PPTX
Addressing modes
PPTX
Children’s Day Celebration
PPTX
Templates in C++
PPTX
Ghani khan, The Pashtun Poet and Philosopher
Templates in c++
Basics of C programming
Inheritance in C++
Storage class in C Language
Addressing modes
Children’s Day Celebration
Templates in C++
Ghani khan, The Pashtun Poet and Philosopher

What's hot (20)

PPTX
Oop c++class(final).ppt
PPTX
PDF
Function overloading ppt
PPTX
classes and objects in C++
PPTX
Static Data Members and Member Functions
PDF
Object oriented programming c++
PPTX
Constructors in C++
PPT
friend function(c++)
PPTX
Inheritance in c++
PPTX
Functions in c++
PPTX
C# classes objects
PPTX
Inheritance in c++
PPTX
JAVA AWT
PPT
C by balaguruswami - e.balagurusamy
PPTX
Interface in java
PPT
Constructor
PPTX
Object Oriented Programming Using C++
PPTX
Polymorphism in c++(ppt)
PPT
Operator Overloading
PDF
Classes and objects
Oop c++class(final).ppt
Function overloading ppt
classes and objects in C++
Static Data Members and Member Functions
Object oriented programming c++
Constructors in C++
friend function(c++)
Inheritance in c++
Functions in c++
C# classes objects
Inheritance in c++
JAVA AWT
C by balaguruswami - e.balagurusamy
Interface in java
Constructor
Object Oriented Programming Using C++
Polymorphism in c++(ppt)
Operator Overloading
Classes and objects
Ad

Viewers also liked (20)

PPTX
Constructors & destructors
PPTX
Constructor ppt
PPT
constructor and destructor-object oriented programming
PPTX
Constructor and destructor in c++
PDF
Constructor and Destructor
PPT
Constructor & Destructor
PPT
Constructor and Destructor PPT
PPTX
Constructor in java
PPTX
Constructor in java
PPTX
Java constructors
PPT
Oop lec 5-(class objects, constructor & destructor)
PPT
Java lec constructors
PPTX
Methods and constructors in java
PPT
Java lec class, objects and constructors
PPTX
constructor & destructor in cpp
PPT
Oops ppt
PPTX
Class object method constructors in java
PPT
Object Oriented Programming Concepts
PDF
Constructor & destructor
PPTX
Constructor & destructor
Constructors & destructors
Constructor ppt
constructor and destructor-object oriented programming
Constructor and destructor in c++
Constructor and Destructor
Constructor & Destructor
Constructor and Destructor PPT
Constructor in java
Constructor in java
Java constructors
Oop lec 5-(class objects, constructor & destructor)
Java lec constructors
Methods and constructors in java
Java lec class, objects and constructors
constructor & destructor in cpp
Oops ppt
Class object method constructors in java
Object Oriented Programming Concepts
Constructor & destructor
Constructor & destructor
Ad

Similar to Constructors and Destructors (20)

PPTX
Constructors and Destructors
PPTX
Constructors and Destructors in C++.pptx
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPT
classes and constructors lec 03 & 04.ppt
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
Constructor and destructor
PPTX
C++ Unit-III Lecture-3a-C++ Programming Concepts
PPTX
Constructors in C++.pptx
PDF
classes and objects.pdfggggggggffffffffgggf
PPT
Constructor,destructors cpp
PPTX
C++Constructors
PDF
Constructors destructors
PPTX
constructors and destructors
PPTX
Operator overload rr
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PDF
chapter-9-constructors.pdf
PDF
Constructors & Destructors [Compatibility Mode].pdf
PPTX
Constructor in c++
Constructors and Destructors
Constructors and Destructors in C++.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
classes and constructors lec 03 & 04.ppt
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Constructor and destructor
C++ Unit-III Lecture-3a-C++ Programming Concepts
Constructors in C++.pptx
classes and objects.pdfggggggggffffffffgggf
Constructor,destructors cpp
C++Constructors
Constructors destructors
constructors and destructors
Operator overload rr
constructocvbcvbcvbcvbr-Destructor (1).pptx
chapter-9-constructors.pdf
Constructors & Destructors [Compatibility Mode].pdf
Constructor in c++

More from Dr Sukhpal Singh Gill (19)

PDF
RESEARCH METHODOLOGY: A PRACTITIONER APPROACH
PDF
Cloud Data Centers and the Challenge of Sustainable Energy
PDF
If you know nothing about HTML, this is where you can start !!
PDF
Software Requirement Specification
PDF
Introduction to RDF
PDF
Network Topologies
PDF
How to Write an Effective Research Paper
PDF
GREEN CLOUD COMPUTING-A Data Center Approach
PDF
End-to-End Security in Mobile-Cloud Computing
PDF
Java.NET: Integration of Java and .NET
PDF
Software Verification, Validation and Testing
PDF
Software Requirements Specification (SRS) for Online Tower Plotting System (O...
PDF
Reduction of Blocking Artifacts In JPEG Compressed Image
PDF
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
PDF
Case Study Based Software Engineering Project Development: State of Art
PDF
Reduction of Blocking Artifacts In JPEG Compressed Image
PDF
Reusability Framework for Cloud Computing
PDF
The reuse capability model
PDF
Topological methods
RESEARCH METHODOLOGY: A PRACTITIONER APPROACH
Cloud Data Centers and the Challenge of Sustainable Energy
If you know nothing about HTML, this is where you can start !!
Software Requirement Specification
Introduction to RDF
Network Topologies
How to Write an Effective Research Paper
GREEN CLOUD COMPUTING-A Data Center Approach
End-to-End Security in Mobile-Cloud Computing
Java.NET: Integration of Java and .NET
Software Verification, Validation and Testing
Software Requirements Specification (SRS) for Online Tower Plotting System (O...
Reduction of Blocking Artifacts In JPEG Compressed Image
Workshop on Basics of Software Engineering (DFD, UML and Project Culture)
Case Study Based Software Engineering Project Development: State of Art
Reduction of Blocking Artifacts In JPEG Compressed Image
Reusability Framework for Cloud Computing
The reuse capability model
Topological methods

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Computing-Curriculum for Schools in Ghana
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
Weekly quiz Compilation Jan -July 25.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
Complications of Minimal Access Surgery at WLH
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
O7-L3 Supply Chain Operations - ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
Computing-Curriculum for Schools in Ghana
A systematic review of self-coping strategies used by university students to ...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

Constructors and Destructors

  • 2. WHAT IS A CONSTRUCTOR ?  In c++ , a constructor is a ‘special’ member function whose task is to initialize the objects of its class.  It is special because its name is the same as the class name.  It is called constructor because it constructs the value of data members of the class.  The constructor is invoked whenever an object of its associated class is created. ssgill©tupatiala
  • 3. CLASS WITH A CONSTRUCTOR Class integer { int m, n; public: integer(void); // constructor declared }; Integer : : integer (void) // constructor defined { m=0; n=0; } Object declartion: Integer int1; // object int1 created ssgill©tupatiala
  • 4. CHARACTERISTICS OF THE CONSTRUCTOR  They should be declared in public section.  They are invoked automatically when the objects are created.  They don't have return types, cannot return values.  A constructor that accepts no parameters is called default constructor. The default constructor for class A is A:: A() . The statement A a; invokes the default constructor of the compiler to create the object a. ssgill©tupatiala
  • 5. PARAMETERIZED CONSTRUCTOR The constructor that can take arguments. Class integer { int m, n; public: integer(int x, int y); //parameterized constructor }; Integer : : integer (int x, int y) { m=x; n=y; } Object declaration statement “integer int1 ; “ do not work here. ssgill©tupatiala
  • 6. CALL IN PARAMETERIZED CONSTRUCTOR  We must pass the initial values as arguments to the constructor function when object is created. This can be done in two ways:  By calling the constructor explicitly. integer int1 = integer(0, 100);  By calling the constructor implicitly. integer int1(0, 100); This method is also called shorthand method. ssgill©tupatiala
  • 7. EXPLICIT/IMPLICIT CALL int main() { integer int1(0, 100); //implicit call integer int2 = integer(25, 75); //explicit call cout<<“object 1:” int1.function1(); cout<<“object 2:” int2.function1(); return 0; } Output object1: m=0 and n= 100 object2: m=25 and n= 75 ssgill©tupatiala
  • 8. MULTIPLE CONSTRUCTOR IN A CLASS Class integer { int m, n; public: integer() //default constructor {m=0; n=0;} integer(int a, int b) //parameterized constructor {m=a; n=b;} integer(integer & i) //copy constructor {m= i.m; n= i.n;} }; ssgill©tupatiala
  • 9. CONSTRUCTORS WITH DEFAULT ARGUMENTS complex(float real , float imag=0) { }  complex c1(5.0);  complex c2(2.0,3.0);  Difference b/w A::A() and A::A(int a=0). ssgill©tupatiala
  • 10. DYNAMIC INITIALIZATION OF OBJECTS Provide various initialization formats using overloaded constructors.  Fixed_deposit() { }  Fixed_deposit(long int p, int y ,float r=0.12);  Fixed_deposit( long int p, int y, int r); ssgill©tupatiala
  • 11. COPY CONSTRUCTOR  It is used to declare and initialize an object from another object. e.g • code() { } • code( int a) {id = a;} • code( code &x){ id = x.id; } //copy constructor.  And various calls for them are: • code A(100); // object A is Created and initialized. • code B(A); // copy constructor called. • code C = A; // copy constructor called again. • code D; // object D is created , not initialized D=A; // copy constructor is not called. ssgill©tupatiala
  • 12. DYNAMIC CONSTRUCTORS  It is used to allocate memory while creating objects. • Enable the system to allocate right amount of memory for each object when objects are not of same size. • Thus resulting in saving of memory. ssgill©tupatiala
  • 13. DYNAMIC CONSTRUCTORS class String { char *name; int length; public: String () { length = 0; name = new char[length+1]; } String (char *s) { length = strlen(s); name = new char[length+1]; strcpy(name, s); } }; main() { Char *first = “ME_SE_CR” ; String name1( first); String name2(“ Rupinder”); . . . } ssgill©tupatiala
  • 14. DESTRUCTORS  ~ integer() { }  As new is used to allocate memory in dynamic constructors, here we use delete to free that memory.  Never take any argument nor does it return any value.  It will be invoked implicitly by compiler upon exit from program or a block or a function. ssgill©tupatiala
  • 15. DESTRUCTORS Int count = 0; class test1 { public : ~test1() { count ++; cout <<“n no. of object created “ << count ; } ~test1 () { cout <<“n no. of object destroyed” << count ; Count - - ; } }; main() { cout << “n Enter Main n”; test1 t1, t2, t3, t4; { cout <<“n Entered in block1” ; test1 t5; } { cout <<“n Entered in block2” ; test1 t6; } cout << “n Re-Enter Main n”; return 0; } ssgill©tupatiala
  • 16. OUT PUT  Enter Main No. of object created 1 No. of object created 2 No. of object created 3 No. of object created 4  Enter block 1 No. of object created 5 No. of object destroyed 5  Enter block 2 No. of object created 5 No. of object destroyed 5  Re - Enter Main No. of object destroyed 4 No. of object destroyed 3 No. of object destroyed 2 No. of object destroyed 1 ssgill©tupatiala