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

What's hot

Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java Course
RAKESH P
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
C++
C++C++
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
chauhankapil
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
Prem Kumar Badri
 
Dot net introduction
Dot net introductionDot net introduction
Dot net introduction
Dr.Neeraj Kumar Pandey
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
aleenaguen
 
Java Constructors
Java ConstructorsJava Constructors
Java Constructors
Saumya Som
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
Dr.Neeraj Kumar Pandey
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
chauhankapil
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
Karthik Sekar
 
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
It Academy
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 

What's hot (20)

Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java Course
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructor
ConstructorConstructor
Constructor
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
C++
C++C++
C++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
Dot net introduction
Dot net introductionDot net introduction
Dot net introduction
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Java Constructors
Java ConstructorsJava Constructors
Java Constructors
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
 
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 

Viewers also liked

Embarcadero C++Builder XE3 Datasheet
Embarcadero C++Builder XE3 DatasheetEmbarcadero C++Builder XE3 Datasheet
Embarcadero C++Builder XE3 Datasheet
Embarcadero Technologies
 
La malaria CMC IES Griñon
La malaria CMC IES Griñon La malaria CMC IES Griñon
La malaria CMC IES Griñon albacynthia
 
Actividades fim de semana 12 e 13 maio
Actividades fim de semana  12 e 13 maioActividades fim de semana  12 e 13 maio
Actividades fim de semana 12 e 13 maioSofia Cabral
 
Viral and bacterial disease research
Viral and bacterial disease researchViral and bacterial disease research
Viral and bacterial disease research
mmsims
 
A.G.INDUSTRIES BROCHURE
A.G.INDUSTRIES BROCHUREA.G.INDUSTRIES BROCHURE
A.G.INDUSTRIES BROCHUREAchin Gupta
 
Jornadas Murcia
Jornadas MurciaJornadas Murcia
Apple Fun Facts
Apple Fun FactsApple Fun Facts
Apple Fun Facts
Drishti94
 
Fun Facts Disney
Fun Facts DisneyFun Facts Disney
Fun Facts Disney
Drishti94
 
Upstream A2 Term Exam
Upstream A2 Term ExamUpstream A2 Term Exam
Upstream A2 Term Exam
Sawsan Ali
 
Digitale Transformation in der öffentlichen Verwaltung
Digitale Transformation in der öffentlichen VerwaltungDigitale Transformation in der öffentlichen Verwaltung
Digitale Transformation in der öffentlichen Verwaltung
Sven Ruoss
 
Propuestatrabajo2 1516
Propuestatrabajo2 1516Propuestatrabajo2 1516
Propuestatrabajo2 1516
Manuel Jesús Fernández Naranjo
 
20160927 Promotie maken - Zingem
20160927 Promotie maken - Zingem20160927 Promotie maken - Zingem
20160927 Promotie maken - Zingem
I Like Media
 
20170113 digitale tools
20170113 digitale tools20170113 digitale tools
20170113 digitale tools
I Like Media
 
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
DNX
 

Viewers also liked (16)

Embarcadero C++Builder XE3 Datasheet
Embarcadero C++Builder XE3 DatasheetEmbarcadero C++Builder XE3 Datasheet
Embarcadero C++Builder XE3 Datasheet
 
La malaria CMC IES Griñon
La malaria CMC IES Griñon La malaria CMC IES Griñon
La malaria CMC IES Griñon
 
2016 Resume
2016 Resume2016 Resume
2016 Resume
 
Actividades fim de semana 12 e 13 maio
Actividades fim de semana  12 e 13 maioActividades fim de semana  12 e 13 maio
Actividades fim de semana 12 e 13 maio
 
Viral and bacterial disease research
Viral and bacterial disease researchViral and bacterial disease research
Viral and bacterial disease research
 
A.G.INDUSTRIES BROCHURE
A.G.INDUSTRIES BROCHUREA.G.INDUSTRIES BROCHURE
A.G.INDUSTRIES BROCHURE
 
Jornadas Murcia
Jornadas MurciaJornadas Murcia
Jornadas Murcia
 
Resume
ResumeResume
Resume
 
Apple Fun Facts
Apple Fun FactsApple Fun Facts
Apple Fun Facts
 
Fun Facts Disney
Fun Facts DisneyFun Facts Disney
Fun Facts Disney
 
Upstream A2 Term Exam
Upstream A2 Term ExamUpstream A2 Term Exam
Upstream A2 Term Exam
 
Digitale Transformation in der öffentlichen Verwaltung
Digitale Transformation in der öffentlichen VerwaltungDigitale Transformation in der öffentlichen Verwaltung
Digitale Transformation in der öffentlichen Verwaltung
 
Propuestatrabajo2 1516
Propuestatrabajo2 1516Propuestatrabajo2 1516
Propuestatrabajo2 1516
 
20160927 Promotie maken - Zingem
20160927 Promotie maken - Zingem20160927 Promotie maken - Zingem
20160927 Promotie maken - Zingem
 
20170113 digitale tools
20170113 digitale tools20170113 digitale tools
20170113 digitale tools
 
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
DNX Workshop ★ Nur Mut! Gestalte dein Leben wie es dir gefällt - Yvonne Rundio
 

Similar to 5 Constructors and Destructors

Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
Pranali Chaudhari
 
C++ interview questions
C++ interview questionsC++ interview questions
C++ interview questions
arjavi
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
dharawagh9999
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
murugeswariSenthilku
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
Nusrat Gulbarga
 
Constructor & destructor based question- cbse cs class 12th
Constructor & destructor based question-  cbse cs class 12thConstructor & destructor based question-  cbse cs class 12th
Constructor & destructor based question- cbse cs class 12th
AAKASH KUMAR
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptx
DrKalkaDubey1
 
C++
C++C++

Similar to 5 Constructors and Destructors (20)

Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
C++ interview questions
C++ interview questionsC++ interview questions
C++ interview questions
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
 
Constructor & destructor based question- cbse cs class 12th
Constructor & destructor based question-  cbse cs class 12thConstructor & destructor based question-  cbse cs class 12th
Constructor & destructor based question- cbse cs class 12th
 
CST 203 Lecture 7.pptx
CST 203 Lecture 7.pptxCST 203 Lecture 7.pptx
CST 203 Lecture 7.pptx
 
C++
C++C++
C++
 

More from Praveen M Jigajinni

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
Praveen M Jigajinni
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
Praveen M Jigajinni
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
Praveen M Jigajinni
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
Praveen M Jigajinni
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
Praveen M Jigajinni
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Praveen M Jigajinni
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
Praveen M Jigajinni
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
Praveen M Jigajinni
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
Praveen M Jigajinni
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
Praveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
Praveen M Jigajinni
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
Praveen M Jigajinni
 

More from Praveen M Jigajinni (20)

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
 

Recently uploaded

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 

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)