SlideShare a Scribd company logo
WEL COME
PRAVEEN M JIGAJINNI
PGT (Computer Science)
MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA,
Dc. Sc. & Engg.
email: praveenkumarjigajinni@yahoo.co.in
CHAPTER 5
Constructors
&
Destructors
Constructors
A Member function with the same name as its
classis called Constructor and it is used to
initilize the objects of that class type with a
legal value.
A Constructor is a special member function of a
class that is called automatically when an
object of class is created.
Example :
class Student
{
int rollno;
float marks;
public:
student( ) //Constructor
{
rollno=0;
marks=0.0;
}
//other public members
};
// Implicit Call
Student ob1;
// Explicit Call
Student ob1=student();
TYPES OF CONSRUCTORS
1. Default Constructor: A constructor that accepts no
parameter is called the Default Constructor. If you don't
declare a constructor or a destructor, the compiler makes
one for you. The default constructor and destructor take
no arguments and do nothing.
2. Parameterized Constructors: A constructor that accepts
parameters for its invocation is known as parameterized
Constructors ,also called as Regular Constructors
DESTRUCTORS
A destructor is also a member function whose
name is the same as the class name but is
preceded by tilde(“~”).It is automatically by the
compiler when an object is destroyed.
Destructors are usually used to deallocate
memory and do other cleanup for a class object
and its class members when the object is
destroyed.
A destructor is called for a class object when that
object passes out of scope or is explicitly deleted.
DESTRUCTORS
Example :
class TEST
{ int Regno,Max,Min,Score;
Public:
TEST( ) // Default Constructor
{ }
TEST (int Pregno,int Pscore) // Parameterized Constructor
{
Regno = Pregno ;Max=100;Max=100;Min=40;Score=Pscore;
}
~ TEST ( ) // Destructor
{ Cout<<”TEST Over”<<endl;}
};
The following points apply to constructors and
destructors:
Constructors and destructors do not have return type, not
even void nor can they return values.
References and pointers cannot be used on constructors
and destructors because their addresses cannot be taken.
Constructors cannot be declared with the keyword virtual
Constructors and destructors cannot be declared static,
const, or volatile.
 Unions cannot contain class objects that have
constructors or destructors.
The following points apply to constructors and
destructors:
The compiler automatically calls constructors when
defining class objects and calls destructors when class
objects go out of scope
Derived classes do not inherit constructors or destructors
from their base classes, but they do call the constructor and
destructor of base classes.
The default destructor calls the destructors of the base
class and members of the derived class.
The following points apply to Constructors and
Destructors:
The destructors of base classes and members are called in
the reverse order of the completion of their constructor:
The destructor for a class object is called before
destructors for members and bases are called.
You can have only one destructor for a class.
Destructors can’t be overloaded.
They can not be inherited.
No argument can be provided to destructors.
Copy Constructor
A copy constructor is a special constructor in the C++
programming language used to create a new object as a
copy of an existing object.
A copy constructor is a constructor of the form classname
(classname &).The compiler will use the copy constructors
whenever you initialize an instance using values of another
instance of the same type.
Copying of objects is achieved by the use of a copy
constructor and a assignment operator.
Copy Constructor Example :
class Sample{ int i, j;
public:
Sample(int a, int b) // constructor
{ i=a;j=b;}
Sample (Sample & s) //copy constructor
{ j=s.j ; i=s.j;
cout <<”n Copy constructor working n”;
}
void print (void)
{cout <<i<< j<< ”n”;}
};
Note : The argument to a copy constructor is passed by reference, the
reason being that when an argument is passed by value, a copy of it is
constructed. But the copy constructor is creating a copy of the object for
itself, thus ,it calls itself. Again the called copy constructor requires another
copy so again it is called. In fact it calls itself again and again until the
compiler runs out of the memory .so, in the copy constructor, the argument
must be passed by reference.
CALLING COPY CONSTRUCTOR
Sample s2(s1); // s1 is copied to s2. Copy constructor is called.
Sample s3=s1; // s1 is copied to s3. copy constructor called
again
When Copy Constructor is called?
When an object is passed by value to a function:
The pass by value method requires a copy of the passed
argument to be created for the function to operate upon .Thus
to create the copy of the passed object, copy constructor is
Invoked If a function with the following prototype :
void cpyfunc(Sample ); // Sample is a class then for the
following function call
cpyfunc(obj1); // obj1 is an object of Sample type the copy
constructor would be invoked to create a copy of the obj1
object for use by cpyfunc().
When Copy Constructor is called?
When a function returns an object :
When an object is returned by a function the copy constructor
is invoked
Sample cpyfunc(); // Sample is a class and it is return type
of cpyfunc()
If func cpyfunc() is called by the following statement
obj2 = cpyfunc();
Then the copy constructor would be invoked to create a copy
of the value returned by cpyfunc() and its value would be
assigned to obj2. The copy constructor creates a temporary
object to hold the return value of a function returning an
object.
CBSE QUESTION PATTERN
RELATED
TO
CONSTRUCTORS
AND
DESTRUCTORS
2 MARKS
QNO 2 ( b )
2 (b) Answer the questions (i) and (ii) after going
through the following class : Delhi 2006
class Interview
{
int month;
public:
Interview(int y) {month=y;} //Constructor 1
Interview(Interview&t); //Constructor 2
};
(i) Create an object, such that it invokes
Constructor 1 1
(ii) Write complete definition for Constructor 2 1
(b) Interview Ob1(5);
OR
int N=5; Interview Ob1(N);
(1 mark for proper declaration of Object)
Interview(Interview &t)
{
month = t.month;
}
(1 mark for writing proper statements inside definition of Constructor
2)
OR
(1 mark for writing the conceptual definition of the copy constructor)
OR
(Only ½ mark for mentioning the term: copy constructor)
Note: Any valid statement in C++ working with t as an object of
Interview must be accepted while defining the constructor.
(b) Answer the questions (i) and (ii) after going
through the following class : Outside Delhi 2006
class Exam
{
int year;
public:
Exam(int y) { year=y;} //Constructor 1
Exam(Exam & t); //Constructor 2
};
(i) Create an object, such that it invokes
Constructor 1. 1
(ii) Write complete definition for Constructor 2. 1
(i) Exam Ob1(2015);
(ii) Exam (Exam &t)
{year = t.year;}
OR
Copy constructor: It is an overloaded constructor, in which
object of the same class is passed as parameter.
(1 mark for writing proper statements inside definition of
Constructor 2)
OR
(1 mark for any valid statement in C++ working with t as an
object of Exam)
OR
(1 mark for writing the definition/explanation of the concept of
copy constructor)
OR
(1/2 mark for mentioning only the term copy constructor)
?Any Questions Please
Thank You
Very Much

More Related Content

PPTX
Constructor & destructor
PPTX
Constructor and destructor in c++
PPTX
Constructor and desturctor
PPTX
Constructor and destructor in oop
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PPT
vb.net Constructor and destructor
PPT
Constructors and destructors in C++ part 2
PPT
constructor and destructor-object oriented programming
Constructor & destructor
Constructor and destructor in c++
Constructor and desturctor
Constructor and destructor in oop
Oop Constructor Destructors Constructor Overloading lecture 2
vb.net Constructor and destructor
Constructors and destructors in C++ part 2
constructor and destructor-object oriented programming

What's hot (20)

PPTX
Constructor and destructor
PDF
Constructors and destructors
PPTX
Java Constructors | Java Course
PPTX
Constructor in java
PDF
Constructor & destructor
PPTX
Constructor and Destructor
PPT
Constructor
PPT
Oop lec 5-(class objects, constructor & destructor)
PDF
Constructors and Destructors
PPTX
Constructor&amp; destructor
PPTX
C# Constructors
PPT
Dot net introduction
PPTX
Constructor and Destructor in c++
PPTX
Java Constructors
PPTX
C# lecture 1: Introduction to Dot Net Framework
PPTX
Constructors in java
PPTX
Constructor destructor.ppt
PPTX
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
PPSX
Object oriented concepts & programming (2620003)
Constructor and destructor
Constructors and destructors
Java Constructors | Java Course
Constructor in java
Constructor & destructor
Constructor and Destructor
Constructor
Oop lec 5-(class objects, constructor & destructor)
Constructors and Destructors
Constructor&amp; destructor
C# Constructors
Dot net introduction
Constructor and Destructor in c++
Java Constructors
C# lecture 1: Introduction to Dot Net Framework
Constructors in java
Constructor destructor.ppt
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
Object oriented concepts & programming (2620003)
Ad

Viewers also liked (16)

PDF
Embarcadero C++Builder XE3 Datasheet
PPT
La malaria CMC IES Griñon
DOC
2016 Resume
PDF
Actividades fim de semana 12 e 13 maio
PDF
Viral and bacterial disease research
PDF
A.G.INDUSTRIES BROCHURE
DOCX
PPTX
Apple Fun Facts
PPTX
Fun Facts Disney
DOCX
Upstream A2 Term Exam
PDF
Digitale Transformation in der öffentlichen Verwaltung
PDF
Propuestatrabajo2 1516
PPTX
20160927 Promotie maken - Zingem
PPTX
20170113 digitale tools
PDF
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
 
Embarcadero C++Builder XE3 Datasheet
La malaria CMC IES Griñon
2016 Resume
Actividades fim de semana 12 e 13 maio
Viral and bacterial disease research
A.G.INDUSTRIES BROCHURE
Apple Fun Facts
Fun Facts Disney
Upstream A2 Term Exam
Digitale Transformation in der öffentlichen Verwaltung
Propuestatrabajo2 1516
20160927 Promotie maken - Zingem
20170113 digitale tools
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
 
Ad

Similar to 5 Constructors and Destructors (20)

PDF
Constructor and Destructor.pdf
PPTX
Constructors and destructors
PPT
ConsTRUCTION AND DESTRUCTION
PPTX
Constructors in C++.pptx
PPTX
PPTX
Constructors & Destructors
PPTX
constructor in object oriented program.pptx
PPTX
Constructors and Destructors
PPT
Constructor and destructor in C++
PPT
Constructor & Destructor
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
C++Constructors
PPTX
constructor & destructor in cpp
PPTX
constructor & destructor in cpp
PPTX
constructors and destructors
PDF
Constructors or destructors unit(II).pdf
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PDF
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
PPTX
OOP-Lecture-05 (Constructor_Destructor).pptx
PDF
Constructors and destructors
Constructor and Destructor.pdf
Constructors and destructors
ConsTRUCTION AND DESTRUCTION
Constructors in C++.pptx
Constructors & Destructors
constructor in object oriented program.pptx
Constructors and Destructors
Constructor and destructor in C++
Constructor & Destructor
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
C++Constructors
constructor & destructor in cpp
constructor & destructor in cpp
constructors and destructors
Constructors or destructors unit(II).pdf
constructocvbcvbcvbcvbr-Destructor (1).pptx
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
OOP-Lecture-05 (Constructor_Destructor).pptx
Constructors and destructors

More from Praveen M Jigajinni (20)

PPTX
Chapter 09 design and analysis of algorithms
PPTX
Chapter 08 data file handling
PPTX
Chapter 07 inheritance
PPTX
Chapter 06 constructors and destructors
PPTX
Chapter 05 classes and objects
PPTX
Chapter 04 object oriented programming
PPTX
Chapter 03 python libraries
PPTX
Chapter 02 functions -class xii
PPTX
Unit 3 MongDB
PPTX
Chapter 17 Tuples
PPTX
Chapter 15 Lists
PPTX
Chapter 14 strings
PPTX
Chapter 13 exceptional handling
PPTX
Chapter 10 data handling
PPTX
Chapter 9 python fundamentals
PPTX
Chapter 8 getting started with python
PPTX
Chapter 7 basics of computational thinking
PPTX
Chapter 6 algorithms and flow charts
PPTX
Chapter 5 boolean algebra
PPTX
Chapter 4 number system
Chapter 09 design and analysis of algorithms
Chapter 08 data file handling
Chapter 07 inheritance
Chapter 06 constructors and destructors
Chapter 05 classes and objects
Chapter 04 object oriented programming
Chapter 03 python libraries
Chapter 02 functions -class xii
Unit 3 MongDB
Chapter 17 Tuples
Chapter 15 Lists
Chapter 14 strings
Chapter 13 exceptional handling
Chapter 10 data handling
Chapter 9 python fundamentals
Chapter 8 getting started with python
Chapter 7 basics of computational thinking
Chapter 6 algorithms and flow charts
Chapter 5 boolean algebra
Chapter 4 number system

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Types and Its function , kingdom of life
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
01-Introduction-to-Information-Management.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
VCE English Exam - Section C Student Revision Booklet
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Cell Types and Its function , kingdom of life
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Structure & Organelles in detailed.
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
O5-L3 Freight Transport Ops (International) V1.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Pharma ospi slides which help in ospi learning
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
01-Introduction-to-Information-Management.pdf
Weekly quiz Compilation Jan -July 25.pdf

5 Constructors and Destructors

  • 1. WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg. email: praveenkumarjigajinni@yahoo.co.in
  • 3. Constructors A Member function with the same name as its classis called Constructor and it is used to initilize the objects of that class type with a legal value. A Constructor is a special member function of a class that is called automatically when an object of class is created.
  • 4. Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members }; // Implicit Call Student ob1; // Explicit Call Student ob1=student();
  • 5. TYPES OF CONSRUCTORS 1. Default Constructor: A constructor that accepts no parameter is called the Default Constructor. If you don't declare a constructor or a destructor, the compiler makes one for you. The default constructor and destructor take no arguments and do nothing. 2. Parameterized Constructors: A constructor that accepts parameters for its invocation is known as parameterized Constructors ,also called as Regular Constructors
  • 6. DESTRUCTORS A destructor is also a member function whose name is the same as the class name but is preceded by tilde(“~”).It is automatically by the compiler when an object is destroyed. Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
  • 7. DESTRUCTORS Example : class TEST { int Regno,Max,Min,Score; Public: TEST( ) // Default Constructor { } TEST (int Pregno,int Pscore) // Parameterized Constructor { Regno = Pregno ;Max=100;Max=100;Min=40;Score=Pscore; } ~ TEST ( ) // Destructor { Cout<<”TEST Over”<<endl;} };
  • 8. The following points apply to constructors and destructors: Constructors and destructors do not have return type, not even void nor can they return values. References and pointers cannot be used on constructors and destructors because their addresses cannot be taken. Constructors cannot be declared with the keyword virtual Constructors and destructors cannot be declared static, const, or volatile.  Unions cannot contain class objects that have constructors or destructors.
  • 9. The following points apply to constructors and destructors: The compiler automatically calls constructors when defining class objects and calls destructors when class objects go out of scope Derived classes do not inherit constructors or destructors from their base classes, but they do call the constructor and destructor of base classes. The default destructor calls the destructors of the base class and members of the derived class.
  • 10. The following points apply to Constructors and Destructors: The destructors of base classes and members are called in the reverse order of the completion of their constructor: The destructor for a class object is called before destructors for members and bases are called. You can have only one destructor for a class. Destructors can’t be overloaded. They can not be inherited. No argument can be provided to destructors.
  • 11. Copy Constructor A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. A copy constructor is a constructor of the form classname (classname &).The compiler will use the copy constructors whenever you initialize an instance using values of another instance of the same type. Copying of objects is achieved by the use of a copy constructor and a assignment operator.
  • 12. Copy Constructor Example : class Sample{ int i, j; public: Sample(int a, int b) // constructor { i=a;j=b;} Sample (Sample & s) //copy constructor { j=s.j ; i=s.j; cout <<”n Copy constructor working n”; } void print (void) {cout <<i<< j<< ”n”;} }; Note : The argument to a copy constructor is passed by reference, the reason being that when an argument is passed by value, a copy of it is constructed. But the copy constructor is creating a copy of the object for itself, thus ,it calls itself. Again the called copy constructor requires another copy so again it is called. In fact it calls itself again and again until the compiler runs out of the memory .so, in the copy constructor, the argument must be passed by reference.
  • 13. CALLING COPY CONSTRUCTOR Sample s2(s1); // s1 is copied to s2. Copy constructor is called. Sample s3=s1; // s1 is copied to s3. copy constructor called again
  • 14. When Copy Constructor is called? When an object is passed by value to a function: The pass by value method requires a copy of the passed argument to be created for the function to operate upon .Thus to create the copy of the passed object, copy constructor is Invoked If a function with the following prototype : void cpyfunc(Sample ); // Sample is a class then for the following function call cpyfunc(obj1); // obj1 is an object of Sample type the copy constructor would be invoked to create a copy of the obj1 object for use by cpyfunc().
  • 15. When Copy Constructor is called? When a function returns an object : When an object is returned by a function the copy constructor is invoked Sample cpyfunc(); // Sample is a class and it is return type of cpyfunc() If func cpyfunc() is called by the following statement obj2 = cpyfunc(); Then the copy constructor would be invoked to create a copy of the value returned by cpyfunc() and its value would be assigned to obj2. The copy constructor creates a temporary object to hold the return value of a function returning an object.
  • 17. 2 (b) Answer the questions (i) and (ii) after going through the following class : Delhi 2006 class Interview { int month; public: Interview(int y) {month=y;} //Constructor 1 Interview(Interview&t); //Constructor 2 }; (i) Create an object, such that it invokes Constructor 1 1 (ii) Write complete definition for Constructor 2 1
  • 18. (b) Interview Ob1(5); OR int N=5; Interview Ob1(N); (1 mark for proper declaration of Object) Interview(Interview &t) { month = t.month; } (1 mark for writing proper statements inside definition of Constructor 2) OR (1 mark for writing the conceptual definition of the copy constructor) OR (Only ½ mark for mentioning the term: copy constructor) Note: Any valid statement in C++ working with t as an object of Interview must be accepted while defining the constructor.
  • 19. (b) Answer the questions (i) and (ii) after going through the following class : Outside Delhi 2006 class Exam { int year; public: Exam(int y) { year=y;} //Constructor 1 Exam(Exam & t); //Constructor 2 }; (i) Create an object, such that it invokes Constructor 1. 1 (ii) Write complete definition for Constructor 2. 1
  • 20. (i) Exam Ob1(2015); (ii) Exam (Exam &t) {year = t.year;} OR Copy constructor: It is an overloaded constructor, in which object of the same class is passed as parameter. (1 mark for writing proper statements inside definition of Constructor 2) OR (1 mark for any valid statement in C++ working with t as an object of Exam) OR (1 mark for writing the definition/explanation of the concept of copy constructor) OR (1/2 mark for mentioning only the term copy constructor)