SlideShare une entreprise Scribd logo
1  sur  30
Constructor and Destructor in C++
Constructor and Destructor
 Constructor and Destructor in C++ Constructor and
Destructor are the special member functions of the
class which are created by the C++ compiler or can be
defined by the user. Constructor is used to initialize
the object of the class while destructor is called by
the compiler when the object is destroyed.
Constructor
 A constructor is a special type of member function that is called automatically
when an object is created.
 It is used to initialize the data members of new object generally.
 The return type of the constructor is the class types.
For example,
class LNCT
{
public:
// create a constructor
LNCT()
{
// code
}
};
 Here, the function LNCT() is a constructor of the
class LNCT. Notice that the constructor
 has the same name as the class,
 does not have a return type, and
 is public
 To create a constructor, use the same name as
the class, followed by parentheses ():
 In class-based object-oriented programming, The
constructor is a special method that is used to
initialize a newly created object and is called just
after the memory is allocated for the object.
class MyClass // The class
{
public: // Access specifier
MyClass() // Constructor
{
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
Note: The constructor has the same name as the class, it is always public, and it does not have
any return value.
OUTPUT :
Hello World!
Types of constructors in C++
 Default Constructor.
 Parameterized Constructor.
 Copy Constructor.
Default constructor:
 A constructor without any arguments or with the
default value for every argument is said to be the
default constructor. They are used to create
objects, which do not have any specific initial
value. If no constructors are explicitly declared in
the class, a default constructor is provided
automatically.
#include <bits/stdc++.h>
using namespace std;
class construct
{
public:
int x, y;
construct() // Default Constructor with default values
{
x = 5; y = 10;
}
};
int main()
{
// Default constructor called automatically
// whenever the object is created
construct z;
cout << "x is: " << z.x << "n" << "y is: " << z.y;
return 0;
}
Parameterized constructor:
 A constructor which takes arguments as input is
called a parameterized constructor. It is used to
provide different values to distinct objects. When
we call the constructor (by creating an object of
the class), we pass parameters to the constructor,
which will set the value of the corresponding
attributes to the same
#include <bits/stdc++.h>
using namespace std;
class values
{
private: int a, b;
public: // Parameterized Constructor
values(int x, int y)
{
a = x; b = y;
}
int getA()
{
return a; }
int getB()
{
return b;
} };
int main()
{
// Constructor called
values v(5, 10); // values assigned by constructor
cout << "v1.x = " << v.getA() << "n" << "v2.y = " << v.getB();
return 0; }
v1.x = 5
v2.y = 10
Copy constructor
 : A copy constructor is a member function that
initializes an object using another object of the
same class. A copy constructor initializes an
object by copying the member values from an
object of the same type. If your class members
are all simple types such as normal values, the
compiler-generated copy constructor is sufficient
and you don’t need to define your own.
 A Copy constructor is an overloaded constructor
used to declare and initialize an object from
another object.
copy constructor
 A constructor that is used to copy or initialize the
value of one object into another object is called
copy constructor .
 SYNTAX:- class_name (class_name &ref)
{
// code to executed
}
Copy Constructor is of two types:
 Default Copy constructor: The compiler defines
the default copy constructor. If the user defines
no copy constructor, compiler supplies its
constructor.
 User Defined constructor: The programmer
defines the user-defined constructor.
#include <iostream>
using namespace std;
class A
{
int a,b;
public:
A(int x, int y)
{
a=x; b=y;
}
A(A &m)
{
a=m.a;
b=m.b;
}
void show ()
{
cout<< a<<b;
}};
int main()
{
A ob(10,20);
A ob1=ob;
ob.show();
ob1.show();
return 0;
}
Syntax Of User-defined Copy Constructor:
Class_name(const class_name &ol
d_object);
class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int a) // parameterized constructor.
{
x=a;
}
A(A &i) // copy constructor
{
x = i.x;
}
};
int main()
{
A a1(20); // Calling the parameterized constructor.
A a2(a1); // Calling the copy constructor.
cout<<a2.x;
return 0;
}
#include<iostream>
using namespace std;
class Point {
private: int x, y;
public:
Point(int x1, int y1)
{
x = x1; y = y1;
} // Copy constructor
Point(const Point & p1)
{
x = p1.x; y = p1.y;
}
int getX()
{
return x;
}
int getY()
{
return y; } };
int main()
{
Point p1(10, 15);
// Normal constructor is called here Point
p2 = p1; // Copy constructor is called here //
Let us access values assigned by
constructors
cout << "p1.x = " << p1.getX() << ", p1.y =
" << p1.getY();
cout << "np2.x = " << p2.getX() << ", p2.y
= " << p2.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
• Write the program to calculate the area of circle
using Default Constructor?
•Write a program to exchange of variables values
using parametric constructor?
• Write a program to calculate the factorial a
giving number using default and parametric
constructor?
•Write a program to calculate the biggest value in
two non using copy and parametric ?
Exercise
What are destructors in C++?
 A destructor is a member function that is invoked automatically when the
object goes out of scope or is explicitly destroyed by a call to delete .
 A destructor has the same name as the class,
 preceded by a tilde ( ~ ).
 For example,
 the destructor for class String is declared: ~String() .
 If you do not define a destructor, the compiler will provide a default one;
for many classes this is sufficient. You only need to define a custom
destructor when the class stores handles to system resources that need
to be released, or pointers that own the memory they point to.
~ExampleClass()
{
--------
----------
}
Declaring destructors
 Destructors are functions with the same name as the class
but preceded by a tilde (~)
 Several rules govern the declaration of destructors.
Destructors:
 Do not accept arguments.
 Do not return a value (or void).
 Cannot be declared as const, volatile, or static. However,
they can be invoked for the destruction of objects declared
as const, volatile, or static.
 Can be declared as virtual. Using virtual destructors, you
can destroy objects without knowing their type — the
correct destructor for the object is invoked using the virtual
function mechanism. Note that destructors can also be
declared as pure virtual functions for abstract classes.
Using destructors
 Destructors are called when one of the following events occurs:
 A local (automatic) object with block scope goes out of scope.
 An object allocated using the new operator is explicitly
deallocated using delete.
 The lifetime of a temporary object ends.
 A program ends and global or static objects exist.
 The destructor is explicitly called using the destructor function's
fully qualified name.
 Destructors can freely call class member functions and access
class member data.
 There are two restrictions on the use of destructors:
 You cannot take its address.
 Derived classes do not inherit the destructor of their base class.
class class_name{
private: // private members
public: // declaring destructor
~class_name()
{
// destructor body
}
};
#include <iostream>
using namespace std;
class test
{
public:
test()
{
int n=10;
cout<<n;
}
~test()
{
cout<<"Destructor";
}
};
int main()
{
test t,t1,t2;
return 0;
}

Contenu connexe

Tendances

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 

Tendances (20)

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Constructor
ConstructorConstructor
Constructor
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
OOP C++
OOP C++OOP C++
OOP C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
File in C language
File in C languageFile in C language
File in C language
 
Strings
StringsStrings
Strings
 

Similaire à Constructors in C++.pptx

Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
Somnath Kulkarni
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
Niti Arora
 

Similaire à Constructors in C++.pptx (20)

Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
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 desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
C++
C++C++
C++
 
C++
C++C++
C++
 

Dernier

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Dernier (20)

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
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
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 

Constructors in C++.pptx

  • 2. Constructor and Destructor  Constructor and Destructor in C++ Constructor and Destructor are the special member functions of the class which are created by the C++ compiler or can be defined by the user. Constructor is used to initialize the object of the class while destructor is called by the compiler when the object is destroyed.
  • 3. Constructor  A constructor is a special type of member function that is called automatically when an object is created.  It is used to initialize the data members of new object generally.  The return type of the constructor is the class types. For example, class LNCT { public: // create a constructor LNCT() { // code } };
  • 4.  Here, the function LNCT() is a constructor of the class LNCT. Notice that the constructor  has the same name as the class,  does not have a return type, and  is public  To create a constructor, use the same name as the class, followed by parentheses ():  In class-based object-oriented programming, The constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. class MyClass // The class { public: // Access specifier MyClass() // Constructor { cout << "Hello World!"; } }; int main() { MyClass myObj; // Create an object of MyClass (this will call the constructor) return 0; } Note: The constructor has the same name as the class, it is always public, and it does not have any return value. OUTPUT : Hello World!
  • 11. Types of constructors in C++  Default Constructor.  Parameterized Constructor.  Copy Constructor.
  • 12.
  • 13. Default constructor:  A constructor without any arguments or with the default value for every argument is said to be the default constructor. They are used to create objects, which do not have any specific initial value. If no constructors are explicitly declared in the class, a default constructor is provided automatically.
  • 14. #include <bits/stdc++.h> using namespace std; class construct { public: int x, y; construct() // Default Constructor with default values { x = 5; y = 10; } }; int main() { // Default constructor called automatically // whenever the object is created construct z; cout << "x is: " << z.x << "n" << "y is: " << z.y; return 0; }
  • 15.
  • 16. Parameterized constructor:  A constructor which takes arguments as input is called a parameterized constructor. It is used to provide different values to distinct objects. When we call the constructor (by creating an object of the class), we pass parameters to the constructor, which will set the value of the corresponding attributes to the same
  • 17. #include <bits/stdc++.h> using namespace std; class values { private: int a, b; public: // Parameterized Constructor values(int x, int y) { a = x; b = y; } int getA() { return a; } int getB() { return b; } }; int main() { // Constructor called values v(5, 10); // values assigned by constructor cout << "v1.x = " << v.getA() << "n" << "v2.y = " << v.getB(); return 0; } v1.x = 5 v2.y = 10
  • 18. Copy constructor  : A copy constructor is a member function that initializes an object using another object of the same class. A copy constructor initializes an object by copying the member values from an object of the same type. If your class members are all simple types such as normal values, the compiler-generated copy constructor is sufficient and you don’t need to define your own.  A Copy constructor is an overloaded constructor used to declare and initialize an object from another object.
  • 19. copy constructor  A constructor that is used to copy or initialize the value of one object into another object is called copy constructor .  SYNTAX:- class_name (class_name &ref) { // code to executed }
  • 20. Copy Constructor is of two types:  Default Copy constructor: The compiler defines the default copy constructor. If the user defines no copy constructor, compiler supplies its constructor.  User Defined constructor: The programmer defines the user-defined constructor.
  • 21. #include <iostream> using namespace std; class A { int a,b; public: A(int x, int y) { a=x; b=y; } A(A &m) { a=m.a; b=m.b; } void show () { cout<< a<<b; }}; int main() { A ob(10,20); A ob1=ob; ob.show(); ob1.show(); return 0; }
  • 22. Syntax Of User-defined Copy Constructor: Class_name(const class_name &ol d_object); class A { A(A &x) // copy constructor. { // copyconstructor. } }
  • 23. #include <iostream> using namespace std; class A { public: int x; A(int a) // parameterized constructor. { x=a; } A(A &i) // copy constructor { x = i.x; } }; int main() { A a1(20); // Calling the parameterized constructor. A a2(a1); // Calling the copy constructor. cout<<a2.x; return 0; }
  • 24. #include<iostream> using namespace std; class Point { private: int x, y; public: Point(int x1, int y1) { x = x1; y = y1; } // Copy constructor Point(const Point & p1) { x = p1.x; y = p1.y; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1(10, 15); // Normal constructor is called here Point p2 = p1; // Copy constructor is called here // Let us access values assigned by constructors cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); cout << "np2.x = " << p2.getX() << ", p2.y = " << p2.getY(); return 0; } Output: p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 15
  • 25. • Write the program to calculate the area of circle using Default Constructor? •Write a program to exchange of variables values using parametric constructor? • Write a program to calculate the factorial a giving number using default and parametric constructor? •Write a program to calculate the biggest value in two non using copy and parametric ? Exercise
  • 26. What are destructors in C++?  A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete .  A destructor has the same name as the class,  preceded by a tilde ( ~ ).  For example,  the destructor for class String is declared: ~String() .  If you do not define a destructor, the compiler will provide a default one; for many classes this is sufficient. You only need to define a custom destructor when the class stores handles to system resources that need to be released, or pointers that own the memory they point to. ~ExampleClass() { -------- ---------- }
  • 27. Declaring destructors  Destructors are functions with the same name as the class but preceded by a tilde (~)  Several rules govern the declaration of destructors. Destructors:  Do not accept arguments.  Do not return a value (or void).  Cannot be declared as const, volatile, or static. However, they can be invoked for the destruction of objects declared as const, volatile, or static.  Can be declared as virtual. Using virtual destructors, you can destroy objects without knowing their type — the correct destructor for the object is invoked using the virtual function mechanism. Note that destructors can also be declared as pure virtual functions for abstract classes.
  • 28. Using destructors  Destructors are called when one of the following events occurs:  A local (automatic) object with block scope goes out of scope.  An object allocated using the new operator is explicitly deallocated using delete.  The lifetime of a temporary object ends.  A program ends and global or static objects exist.  The destructor is explicitly called using the destructor function's fully qualified name.  Destructors can freely call class member functions and access class member data.  There are two restrictions on the use of destructors:  You cannot take its address.  Derived classes do not inherit the destructor of their base class.
  • 29. class class_name{ private: // private members public: // declaring destructor ~class_name() { // destructor body } };
  • 30. #include <iostream> using namespace std; class test { public: test() { int n=10; cout<<n; } ~test() { cout<<"Destructor"; } }; int main() { test t,t1,t2; return 0; }