SlideShare a Scribd company logo
1 of 5
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

What's hot (20)

Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Oops
OopsOops
Oops
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
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)
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java Course
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Inheritance
InheritanceInheritance
Inheritance
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
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
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Inheritance
InheritanceInheritance
Inheritance
 

Viewers also liked

168. por nuestra escuela
168. por nuestra escuela168. por nuestra escuela
168. por nuestra escueladec-admin
 
Luke Young Rhee: Hashtag-Hashtag Presentation
Luke Young Rhee: Hashtag-Hashtag PresentationLuke Young Rhee: Hashtag-Hashtag Presentation
Luke Young Rhee: Hashtag-Hashtag PresentationLuke Rhee
 
Hoja de vida deisy
Hoja de vida deisyHoja de vida deisy
Hoja de vida deisysanlgp
 
Personal Information.WAQAS ASHRAF.
Personal Information.WAQAS ASHRAF.Personal Information.WAQAS ASHRAF.
Personal Information.WAQAS ASHRAF.Waqas Ashraf
 
7 Steps to Migrating to Your Learning Management System (LMS)
7 Steps to Migrating to Your Learning Management System (LMS)7 Steps to Migrating to Your Learning Management System (LMS)
7 Steps to Migrating to Your Learning Management System (LMS)itslearning, inc.
 
Sladeshare никитина
Sladeshare никитинаSladeshare никитина
Sladeshare никитинаKate Nikitina
 
2015-3 Resume Chiquita Finance-up
2015-3 Resume Chiquita Finance-up2015-3 Resume Chiquita Finance-up
2015-3 Resume Chiquita Finance-upChiquita Barkley
 
Deficit de atencao_ou_tdah-1modulo_22-47
Deficit de atencao_ou_tdah-1modulo_22-47Deficit de atencao_ou_tdah-1modulo_22-47
Deficit de atencao_ou_tdah-1modulo_22-47Giordana Garcia
 
Leyes de la dialectica
Leyes de la dialecticaLeyes de la dialectica
Leyes de la dialecticaMery Eli
 
Formativa tercero (6)
Formativa tercero (6)Formativa tercero (6)
Formativa tercero (6)Natalia Rojas
 
24ESV-000453 Mitigating Drowsiness Linking Detection to Mitigation
24ESV-000453 Mitigating Drowsiness Linking Detection to Mitigation24ESV-000453 Mitigating Drowsiness Linking Detection to Mitigation
24ESV-000453 Mitigating Drowsiness Linking Detection to MitigationJulie J. Kang, Ph.D.
 
Atenção farmacêutica teoria e prática um diálogo possível
Atenção farmacêutica   teoria e prática um diálogo possívelAtenção farmacêutica   teoria e prática um diálogo possível
Atenção farmacêutica teoria e prática um diálogo possívelNemesio Silva
 

Viewers also liked (17)

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

Similar to C++

Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objectsPrem Kumar Badri
 
C questions
C questionsC questions
C questionsparm112
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
DeclaringConstructir.pptx
DeclaringConstructir.pptxDeclaringConstructir.pptx
DeclaringConstructir.pptxSinbadMagi
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructorrajshreemuthiah
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKPankaj Prateek
 
Evolution of c# - by K.Jegan
Evolution of c# - by K.JeganEvolution of c# - by K.Jegan
Evolution of c# - by K.Jegantalenttransform
 

Similar to C++ (20)

Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
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
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
C questions
C questionsC questions
C questions
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
DeclaringConstructir.pptx
DeclaringConstructir.pptxDeclaringConstructir.pptx
DeclaringConstructir.pptx
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITKAdvanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK
 
Evolution of c# - by K.Jegan
Evolution of c# - by K.JeganEvolution of c# - by K.Jegan
Evolution of c# - by K.Jegan
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Java
JavaJava
Java
 

Recently uploaded

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

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.