SlideShare a Scribd company logo
Constructors and destructors
Introduction:
Constructors:
A class constructor is a special member function of a class that is executed
whenever we create new objects of that class.
There is no return type for the constructors and infinite number of
constructors can be used in the particular class. Constructors will have the
same name as class. It is very useful for setting initial values to objects.
It can be called infinite times in a class through objects. Constructors play
a vital role in object oriented programming.
Theory:
There are five different types of constructors.
1. Parametrized constructors
2. Default constructors
3. Copy constructors
4. Conversion constructors
5. Move constructors
1. Parametrized constructors:
Constructors that can take at least one argument are termed as
parameterized constructors.
When an object is declared in a parameterized constructor, the initial
values have to be passed as arguments to the constructor function.
The normal way of object declaration may not work. The constructors
can be called explicitly or implicitly. The method of calling the
constructor implicitly is also called the shorthand method.
2. Default constructors:
If the programmer does not supply a constructor for an instantiable
class, most languages will provide a default constructor.
The behaviour of the default constructor is language dependent. It
may initialize data members to zero or other same values, or it may
do nothing at all.
3. Copy constructors
Copy constructors define the actions performed by the compiler when
copying class objects. A copy constructor has one formal parameter
that is the type of the class
4. Conversion constructors
Conversion constructors provide a means for the compiler to
implicitly create an object belonging to one class based on an object
of a different type. These constructors are usually invoked implicitly
to convert arguments or operands to an appropriate type, but they
may also be called explicitly.
5. Move constructors
In C++, move constructors take a value reference to an object of the
class, and are used to implement ownership transfer of the parameter
object's resources.
Syntax of constructors:
1. Default constructors:
#include <iostream>
using namespace std;
class default{
public:
default(){
int a, b;
a=0
b=0; //default constructor
}
};
int main(){
default o1,o2;
}
2. Parametrized constructors:
#include <iostream>
using namespace std;
class parameter{
public:
parameter(int a,int b){
cout<<”parametrized consrtructor is called”;
}
};
int main(){
parameter o1(1,2) , o2(3,4);
return 0;
}
3. Copy constructors:
#include<iostream>
Using namespace std;
Class copy{
Public:
Copy(copy &o1){
Cout<<”copy constructor called”;
}
};
Int main(){
Copy o1;
o2=o1;
}
Real time applications of constructors:
Real-time locating systems (RTLSs) are considered an
effective way to identify and track the location of an object in
both indoor and outdoor environments. Various RTLSs have
been developed and made commercially available in recent
years. Research into RTLSs in the construction sector is
ubiquitous, and results have been published in many
construction-related academic journals over the past decade.
A succinct and systematic review of current applications
would help academics, researchers, and industry practitioners
in identifying existing research deficiencies and therefore
future research directions.
Constructors are used in data transmission systems in real life.
Destructors:
Introduction:
"Destructor" functions are the inverse of constructor functions.
They are called when objects are destroyed (deallocated) .
Designate a function as a class's destructor by preceding the class
name with a tilde (~).
Destructors are called only once in the whole program.
Theory:
Destructors are functions with the same name as the class but preceded by
a tilde .The first form of the syntax is used for destructors declared or
defined inside a class declaration; the second form is used for destructors
defined outside a class declaration.
Several rules govern the declaration of destructors. Destructors:
 Do not accept arguments.
 Cannot specify any return type (including void ).
 Cannot return a value using the return statement.
 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.
Syntax of destructors:
#include<iostream>
Using namespace std;
Class destructor(){
~destructor(){
Cout<<” destructor is called”;
}
};
Int main(){
Destructor o1;
Return 0;
}
Real time application of destructors:
Destructors are very important pieces of the RAII (Resource Allocation Is
Initialization) idiom. Linking the acquisition of resources (files, memory,
sockets, other class objects) to the lifetime of an object is a very powerful tool.
When an object goes out of scope, either through normal execution or due to
an exception, having the object destructor called allows the class to clean up
its resources properly, without having to burden the code using an object with
lots of extra finalization steps.

More Related Content

PPT
constructor and destructor-object oriented programming
PPTX
Constructor in java
PPTX
Constructor and desturctor
PPTX
Constructor in java
PPT
Classes & objects new
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PDF
Constructors and destructors
PPTX
Constructor and destructor in oop
constructor and destructor-object oriented programming
Constructor in java
Constructor and desturctor
Constructor in java
Classes & objects new
Oop Constructor Destructors Constructor Overloading lecture 2
Constructors and destructors
Constructor and destructor in oop

What's hot (20)

PDF
Constructor & destructor
PPTX
PDF
Constructors and Destructors
PPT
Oop lec 5-(class objects, constructor & destructor)
PPTX
constructor & destructor in cpp
PPTX
constructors in java ppt
PPTX
Java Constructors | Java Course
PPTX
Classes and Objects in C#
PDF
Function overloading
PDF
Inheritance
PPTX
C# classes objects
PPTX
Constructor in java
PPTX
Method overloading and constructor overloading in java
PPT
vb.net Constructor and destructor
PPT
Constructor & Destructor
PPT
Constructors and destructors in C++ part 2
PPTX
Constructor and Destructor
PPTX
Structure in c#
PDF
Constructors destructors
PPTX
Inheritance
Constructor & destructor
Constructors and Destructors
Oop lec 5-(class objects, constructor & destructor)
constructor & destructor in cpp
constructors in java ppt
Java Constructors | Java Course
Classes and Objects in C#
Function overloading
Inheritance
C# classes objects
Constructor in java
Method overloading and constructor overloading in java
vb.net Constructor and destructor
Constructor & Destructor
Constructors and destructors in C++ part 2
Constructor and Destructor
Structure in c#
Constructors destructors
Inheritance
Ad

Viewers also liked (17)

PPTX
Sistemas operativos
PPTX
168. por nuestra escuela
PPTX
Luke Young Rhee: Hashtag-Hashtag Presentation
PDF
128170_0_merged_1464739238
DOC
Bridget Evans-Acquah CV
DOCX
Hoja de vida deisy
DOC
Personal Information.WAQAS ASHRAF.
PPTX
точное земледелие
PDF
7 Steps to Migrating to Your Learning Management System (LMS)
PPTX
Sladeshare никитина
DOCX
2015-3 Resume Chiquita Finance-up
PDF
Deficit de atencao_ou_tdah-1modulo_22-47
DOCX
Leyes de la dialectica
PPTX
Formativa tercero (6)
PDF
24ESV-000453 Mitigating Drowsiness Linking Detection to Mitigation
PDF
Atenção farmacêutica teoria e prática um diálogo possível
Sistemas operativos
168. por nuestra escuela
Luke Young Rhee: Hashtag-Hashtag Presentation
128170_0_merged_1464739238
Bridget Evans-Acquah CV
Hoja de vida deisy
Personal Information.WAQAS ASHRAF.
точное земледелие
7 Steps to Migrating to Your Learning Management System (LMS)
Sladeshare никитина
2015-3 Resume Chiquita Finance-up
Deficit de atencao_ou_tdah-1modulo_22-47
Leyes de la dialectica
Formativa tercero (6)
24ESV-000453 Mitigating Drowsiness Linking Detection to Mitigation
Atenção farmacêutica teoria e prática um diálogo possível
Ad

Similar to C++ (20)

PPTX
Constructors and Destructors
PPTX
constructors and destructors
PPTX
Constructors in C++.pptx
DOCX
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
PPTX
Constructor & destructor
PPTX
object oriented programming with C++ Constructors_Destructors.pptx
PDF
Constructors or destructors unit(II).pdf
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPT
ConsTRUCTION AND DESTRUCTION
PPTX
OOP-Lecture-05 (Constructor_Destructor).pptx
PPTX
An introduction to Constructors and destructors in c++ .pptx
PDF
Constructors & Destructors [Compatibility Mode].pdf
PPT
Constructor and destructor in C++
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PPTX
Constructors and Destructors
PPT
3. constructors and destructor
PPT
Constructors and destructors in C++
PPT
Constructor,destructors cpp
PPTX
DHARANI.pptx
PPTX
C++ Unit-III Lecture-3a-C++ Programming Concepts
Constructors and Destructors
constructors and destructors
Constructors in C++.pptx
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
Constructor & destructor
object oriented programming with C++ Constructors_Destructors.pptx
Constructors or destructors unit(II).pdf
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
ConsTRUCTION AND DESTRUCTION
OOP-Lecture-05 (Constructor_Destructor).pptx
An introduction to Constructors and destructors in c++ .pptx
Constructors & Destructors [Compatibility Mode].pdf
Constructor and destructor in C++
constructocvbcvbcvbcvbr-Destructor (1).pptx
Constructors and Destructors
3. constructors and destructor
Constructors and destructors in C++
Constructor,destructors cpp
DHARANI.pptx
C++ Unit-III Lecture-3a-C++ Programming Concepts

Recently uploaded (20)

PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
communication and presentation skills 01
PPTX
Management Information system : MIS-e-Business Systems.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPT
Total quality management ppt for engineering students
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Artificial Intelligence
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PDF
Design Guidelines and solutions for Plastics parts
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Feature types and data preprocessing steps
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
communication and presentation skills 01
Management Information system : MIS-e-Business Systems.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
III.4.1.2_The_Space_Environment.p pdffdf
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Total quality management ppt for engineering students
R24 SURVEYING LAB MANUAL for civil enggi
Artificial Intelligence
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Categorization of Factors Affecting Classification Algorithms Selection
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Design Guidelines and solutions for Plastics parts
Fundamentals of safety and accident prevention -final (1).pptx
Feature types and data preprocessing steps
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
"Array and Linked List in Data Structures with Types, Operations, Implementat...

C++

  • 1. Constructors and destructors Introduction: Constructors: A class constructor is a special member function of a class that is executed whenever we create new objects of that class. There is no return type for the constructors and infinite number of constructors can be used in the particular class. Constructors will have the same name as class. It is very useful for setting initial values to objects. It can be called infinite times in a class through objects. Constructors play a vital role in object oriented programming. Theory: There are five different types of constructors. 1. Parametrized constructors 2. Default constructors 3. Copy constructors 4. Conversion constructors 5. Move constructors 1. Parametrized constructors: Constructors that can take at least one argument are termed as parameterized constructors. When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method. 2. Default constructors: If the programmer does not supply a constructor for an instantiable class, most languages will provide a default constructor. The behaviour of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all.
  • 2. 3. Copy constructors Copy constructors define the actions performed by the compiler when copying class objects. A copy constructor has one formal parameter that is the type of the class 4. Conversion constructors Conversion constructors provide a means for the compiler to implicitly create an object belonging to one class based on an object of a different type. These constructors are usually invoked implicitly to convert arguments or operands to an appropriate type, but they may also be called explicitly. 5. Move constructors In C++, move constructors take a value reference to an object of the class, and are used to implement ownership transfer of the parameter object's resources. Syntax of constructors: 1. Default constructors: #include <iostream> using namespace std; class default{ public: default(){ int a, b; a=0 b=0; //default constructor } }; int main(){
  • 3. default o1,o2; } 2. Parametrized constructors: #include <iostream> using namespace std; class parameter{ public: parameter(int a,int b){ cout<<”parametrized consrtructor is called”; } }; int main(){ parameter o1(1,2) , o2(3,4); return 0; } 3. Copy constructors: #include<iostream> Using namespace std; Class copy{ Public: Copy(copy &o1){ Cout<<”copy constructor called”; } }; Int main(){ Copy o1;
  • 4. o2=o1; } Real time applications of constructors: Real-time locating systems (RTLSs) are considered an effective way to identify and track the location of an object in both indoor and outdoor environments. Various RTLSs have been developed and made commercially available in recent years. Research into RTLSs in the construction sector is ubiquitous, and results have been published in many construction-related academic journals over the past decade. A succinct and systematic review of current applications would help academics, researchers, and industry practitioners in identifying existing research deficiencies and therefore future research directions. Constructors are used in data transmission systems in real life. Destructors: Introduction: "Destructor" functions are the inverse of constructor functions. They are called when objects are destroyed (deallocated) . Designate a function as a class's destructor by preceding the class name with a tilde (~). Destructors are called only once in the whole program. Theory: Destructors are functions with the same name as the class but preceded by a tilde .The first form of the syntax is used for destructors declared or defined inside a class declaration; the second form is used for destructors defined outside a class declaration. Several rules govern the declaration of destructors. Destructors:  Do not accept arguments.  Cannot specify any return type (including void ).  Cannot return a value using the return statement.  Cannot be declared as const, volatile, or static. However, they can be invoked for the destruction of objects declared as const, volatile, or static.
  • 5.  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. Syntax of destructors: #include<iostream> Using namespace std; Class destructor(){ ~destructor(){ Cout<<” destructor is called”; } }; Int main(){ Destructor o1; Return 0; } Real time application of destructors: Destructors are very important pieces of the RAII (Resource Allocation Is Initialization) idiom. Linking the acquisition of resources (files, memory, sockets, other class objects) to the lifetime of an object is a very powerful tool. When an object goes out of scope, either through normal execution or due to an exception, having the object destructor called allows the class to clean up its resources properly, without having to burden the code using an object with lots of extra finalization steps.