SlideShare a Scribd company logo
1 of 7
Destructors in C++
Dr. Mirza Waseem Hussain 1
I. Destructors:
๏ƒ˜A destructor is a special member function that works just opposite to
constructor.
๏ƒ˜Destructor is a member function which destructs or deletes an object.
๏ƒ˜They are called when the class object goes out of scope such as when the
function ends, the program ends, a delete variable is called etc.
A. Special Characteristics of destructors:
a) Name should begin with tilde sign(~) and must match class name.
b) Unlike constructors that can have parameters, destructors do not allow any
parameter.
c) They do not have any return type, just like constructors.
d) There cannot be more than one destructor in a class.
e) When you do not specify any destructor in a class, compiler generates a
default destructor and inserts it into your code.
Dr. Mirza Waseem Hussain 2
B. Declaration and Definition of destructors
Syntax:
class class_name{
public: // Access Specifier.
class_name(); // Declaration of constructor.
~class_name(); //Delaration of Destructor.
};
class_name::class_name(){ //Definition of constructor
}
class_name:: ~class_name(){ //Definition of destructor
}
int main()
{ class_name obj;
return 0;
}
Dr. Mirza Waseem Hussain 3
โ€ข E.g.:1
#include <iostream>
using namespace std;
class student{
public: // Access Specifier.
student(); // Declaration of constructor.
~student(); //Delaration of Destructor.
};
student::student(){ //Definition of constructor
cout<<"Constructor called"<<endl; }
student:: ~student(){ //Definition of destructor
cout<<"Destructor called"<<endl; }
int main(){ student obj,obj1,obj2; return 0;}Dr. Mirza Waseem Hussain 4
Dr. Mirza Waseem Hussain 5
Constructor
of obj
Constructor
of obj1
Constructor
of obj2
Destructor of
obj2
Destructor of
obj1
Destructor of
obj
Order of Execution
Source:
1. https://www.geeksforgeeks.org/constructors-c/
2. https://www.w3schools.com/cpp/cpp_constructors.asp
3. https://beginnersbook.com/2017/08/cpp-constructors/
4. https://www.tutorialspoint.com/cplusplus/cpp_constructor_destru
ctor.htm
5. http://codelack.com/constructorincplusplus/
6. https://www.studytonight.com/cpp/constructors-and-destructors-
in-cpp.php
Notes Prepared by Dr. Mirza Waseem Hussain 6
Notes Prepared by Dr. Mirza Waseem Hussain 7

More Related Content

What's hot

Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
Abed Bukhari
ย 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
Niit Care
ย 
Ppl home assignment_unit3
Ppl home assignment_unit3Ppl home assignment_unit3
Ppl home assignment_unit3
Akshay Nagpurkar
ย 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
ย 
B1ce9 assignmentbsc
B1ce9 assignmentbscB1ce9 assignmentbsc
B1ce9 assignmentbsc
Mukund Trivedi
ย 

What's hot (19)

Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
ย 
inheritance
inheritanceinheritance
inheritance
ย 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
ย 
Inheritance
InheritanceInheritance
Inheritance
ย 
(4) c sharp introduction_object_orientation_part_i
(4) c sharp introduction_object_orientation_part_i(4) c sharp introduction_object_orientation_part_i
(4) c sharp introduction_object_orientation_part_i
ย 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
ย 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
ย 
7 class objects
7 class objects7 class objects
7 class objects
ย 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
ย 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
ย 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
ย 
C# interview questions
C# interview questionsC# interview questions
C# interview questions
ย 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
ย 
Ppl home assignment_unit3
Ppl home assignment_unit3Ppl home assignment_unit3
Ppl home assignment_unit3
ย 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
ย 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
ย 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
ย 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
ย 
B1ce9 assignmentbsc
B1ce9 assignmentbscB1ce9 assignmentbsc
B1ce9 assignmentbsc
ย 

Similar to Destructors

Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
Samad Qazi
ย 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
ย 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
Somnath Kulkarni
ย 

Similar to Destructors (20)

Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
ย 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
ย 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
ย 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor 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
ย 
26c
26c26c
26c
ย 
26csharp
26csharp26csharp
26csharp
ย 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
ย 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
ย 
C++
C++C++
C++
ย 
C++
C++C++
C++
ย 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
ย 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
ย 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
ย 
Destructors
DestructorsDestructors
Destructors
ย 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
ย 
Constructors
ConstructorsConstructors
Constructors
ย 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
ย 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
ย 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
ย 

Recently uploaded

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
sivaprakash250
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
ย 

Recently uploaded (20)

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
ย 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
ย 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
ย 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
ย 

Destructors

  • 1. Destructors in C++ Dr. Mirza Waseem Hussain 1
  • 2. I. Destructors: ๏ƒ˜A destructor is a special member function that works just opposite to constructor. ๏ƒ˜Destructor is a member function which destructs or deletes an object. ๏ƒ˜They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc. A. Special Characteristics of destructors: a) Name should begin with tilde sign(~) and must match class name. b) Unlike constructors that can have parameters, destructors do not allow any parameter. c) They do not have any return type, just like constructors. d) There cannot be more than one destructor in a class. e) When you do not specify any destructor in a class, compiler generates a default destructor and inserts it into your code. Dr. Mirza Waseem Hussain 2
  • 3. B. Declaration and Definition of destructors Syntax: class class_name{ public: // Access Specifier. class_name(); // Declaration of constructor. ~class_name(); //Delaration of Destructor. }; class_name::class_name(){ //Definition of constructor } class_name:: ~class_name(){ //Definition of destructor } int main() { class_name obj; return 0; } Dr. Mirza Waseem Hussain 3
  • 4. โ€ข E.g.:1 #include <iostream> using namespace std; class student{ public: // Access Specifier. student(); // Declaration of constructor. ~student(); //Delaration of Destructor. }; student::student(){ //Definition of constructor cout<<"Constructor called"<<endl; } student:: ~student(){ //Definition of destructor cout<<"Destructor called"<<endl; } int main(){ student obj,obj1,obj2; return 0;}Dr. Mirza Waseem Hussain 4
  • 5. Dr. Mirza Waseem Hussain 5 Constructor of obj Constructor of obj1 Constructor of obj2 Destructor of obj2 Destructor of obj1 Destructor of obj Order of Execution
  • 6. Source: 1. https://www.geeksforgeeks.org/constructors-c/ 2. https://www.w3schools.com/cpp/cpp_constructors.asp 3. https://beginnersbook.com/2017/08/cpp-constructors/ 4. https://www.tutorialspoint.com/cplusplus/cpp_constructor_destru ctor.htm 5. http://codelack.com/constructorincplusplus/ 6. https://www.studytonight.com/cpp/constructors-and-destructors- in-cpp.php Notes Prepared by Dr. Mirza Waseem Hussain 6
  • 7. Notes Prepared by Dr. Mirza Waseem Hussain 7