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

What's hot

Oops
OopsOops
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
kunal kishore
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java Course
RAKESH P
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
Adeel Rasheed
 
Function overloading
Function overloadingFunction overloading
Function overloading
Prof. Dr. K. Adisesha
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Madishetty Prathibha
 
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
baabtra.com - No. 1 supplier of quality freshers
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
suraj pandey
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
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
Lovely Professional University
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
Dr.Neeraj Kumar Pandey
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
Pranali Chaudhari
 
Inheritance
InheritanceInheritance
Inheritance
Jaya Kumari
 

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

Sistemas operativos
Sistemas operativosSistemas operativos
Sistemas operativos
.Gersson Sanchez
 
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 Presentation
Luke Rhee
 
Hoja de vida deisy
Hoja de vida deisyHoja de vida deisy
Hoja de vida deisy
sanlgp
 
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-47
Giordana Garcia
 
Leyes de la dialectica
Leyes de la dialecticaLeyes de la dialectica
Leyes de la dialectica
Mery 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ível
Nemesio 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].pdf
LadallaRajKumar
 
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++.pptx
Rassjb
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
Prem Kumar Badri
 
C questions
C questionsC questions
C questions
parm112
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
nirajmandaliya
 
DeclaringConstructir.pptx
DeclaringConstructir.pptxDeclaringConstructir.pptx
DeclaringConstructir.pptx
SinbadMagi
 
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
Danairat Thanabodithammachari
 
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 Types
Muhammad Hammad Waseem
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
akreyi
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
ismartshanker1
 
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.Jegan
talenttransform
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Java
JavaJava

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

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 

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.