SlideShare a Scribd company logo
Constructors and Destructor in C++
Mr. Sarang Anil Saoji
Information Technology Department
International Institute of Information Technology, I²IT
www.isquareit.edu.in
Constructors
• Characteristics:
1. The constructor is a special member function of a
class.
2. The constructor name is same as the class name.
3. The constructor is invoked automatically whenever
an object of its associated class is created.
4. It is called constructor because it constructs the
values of the data member of the class.
Constructors
• Characteristics:
1. The constructor do not have return type even void.
2. The constructor must be declared in the public
section.
3. The constructor cannot be virtual.
4. When we do not create any constructor in our class,
C++ compiler generates a default constructor and
insert it into our code.
Constructors
• Types of constructor:
1. Default constructor
2. Parameterized constructor
3. Copy constructor.
Constructors
• Default constructor:
 It is the constructor which doesn’t take any
argument. It has no parameters.
• Parameterized constructor:
 It is the constructor which has parameters. It
allows us to pass arguments while object creation.
Constructors
• Example:
class addition
{
int num;
public:
addition(); // default constructor
addition(int); // parameterized constructor
void sum( addition, addition );
void display();
};
int main()
{
addition a(10), b(20); // parameterized constructor invoked
addition c; // default constructor invoked
c.sum(a,b);
c.display( );
}
Constructors
• Example:
addition::addition() //Definition of default constructor
{
num=0;
}
addition::addition(int x) //Definition of parameterized constructor
{
num=x;
}
void addition::sum(addition m, addition n)
{
num=m.num+n.num;
}
void addition::display()
{
cout<<“nAddition is:”<<num;
}
Output - Addition is:30
Constructors
• Copy Constructor:
 It is used to create a new object as a copy of an
existing object.
 The copy constructor is invoked if:
a) Pass an object as an parameter to a call-by-value
function:
void addition::sum(addition m, addition n)
{
num=m.num+n.num;
}
c.sum(a,b); //copy constructor
Constructors
• Copy Constructor:
 The copy constructor is invoked if:
a) Return an object from a function:
friend addition sum( addition, addition ); //declaration
addition sum(addition x, addition y) //definition
{
addition temp;
temp.num=x.num+y.num;
return temp; //copy constructor invoked
}
c=sum(a,b); //calling
Constructors
• Copy Constructor:
 The copy constructor is invoked if:
a) Initialize an object from another object of the same
type
class item
{
int num;
public:
item() {num=10;}
item( item &x) { num=x.num} //copy constructor declaration and definition
void display() { cout<<“n Number is:”<<num; }
};
Constructors
• Copy Constructor:
int main()
{
item a;
item b(a); // copy constructor invoked
item c=b; // copy constructor invoked
a.display();
b.display();
c.display();
}
Output:
Number is:10
Number is:10
Number is:10
Destructor
 A destructor is a special member function that
destroy (or delete) the object.
 A destructor is called automatically when
 The program finished execution.
 A scope (the { } parenthesis) containing object ends.
 Call the delete operator.
Destructor
class book
{
int price;
public:
book()
~book(); //destructor declaration
void display();
};
book::book()
{
price=200;
cout<<“nConstructor”;
}
Book::~book() //destructor definition
{
cout<<“nDestructor”;
}
Void book::display()
{
cout<<“nPrice is:”<<price;
}
Destructor
int main()
{
book b;
b.display();
} //destructor invoked
Output:
Price is:200
Constructor
Destructor
Assignments:
 Write a class complex as follows:
Data members: Real and Imaginary members.
Member functions: get data (use constructor), show
data, add, subtract, multiply and divide.
Use the concept of constructors and destructor.
 A bag consists of zero or more objects of the same type. Each
object can be described by its color and weight. Design C++
program to create a new object. This can be done in two ways.
If the user provides information about color and/or weight of
the object to be created then this information will be used to
create the object otherwise the object will be created using
default values for these attributes. Provide a facility to keep
track of total number of objects and total weight of object from
a bag. Use the concept of constructors and destructor..
References:
 E Balagurusamy, “ Object-Oriented Programming with
C++”, Tata McGraw-Hill Education, 7th edition.
 Schildt Herbert ,”C++: The Complete Reference”, 5th
edition.
Thank You
http://www.isquareit.edu.in/
Ad

More Related Content

What's hot (20)

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 

Similar to Constructors and Destructor in C++ (20)

constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
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
 
constructocvbcvbcvbcvbr-Destructor (1).pptx
constructocvbcvbcvbcvbr-Destructor (1).pptxconstructocvbcvbcvbcvbr-Destructor (1).pptx
constructocvbcvbcvbcvbr-Destructor (1).pptx
WrushabhShirsat3
 
Constructors and Destructors in C++.pptx
Constructors and Destructors in C++.pptxConstructors and Destructors in C++.pptx
Constructors and Destructors in C++.pptx
shivanigarg18041
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
constructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxixconstructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxix
reetanarula4
 
constructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigigconstructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigig
SILENTGAMER45
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
रमन सनौरिया
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
Oops
OopsOops
Oops
Gayathri Ganesh
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
dharawagh9999
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
Praveen M Jigajinni
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
constructocvbcvbcvbcvbr-Destructor (1).pptx
constructocvbcvbcvbcvbr-Destructor (1).pptxconstructocvbcvbcvbcvbr-Destructor (1).pptx
constructocvbcvbcvbcvbr-Destructor (1).pptx
WrushabhShirsat3
 
Constructors and Destructors in C++.pptx
Constructors and Destructors in C++.pptxConstructors and Destructors in C++.pptx
Constructors and Destructors in C++.pptx
shivanigarg18041
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
constructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxixconstructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxix
reetanarula4
 
constructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigigconstructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigig
SILENTGAMER45
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming ConceptsC++ Unit-III Lecture-3a-C++ Programming Concepts
C++ Unit-III Lecture-3a-C++ Programming Concepts
dharawagh9999
 
Ad

More from International Institute of Information Technology (I²IT) (20)

Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
International Institute of Information Technology (I²IT)
 
Understanding Natural Language Processing
Understanding Natural Language ProcessingUnderstanding Natural Language Processing
Understanding Natural Language Processing
International Institute of Information Technology (I²IT)
 
What Is Smart Computing?
What Is Smart Computing?What Is Smart Computing?
What Is Smart Computing?
International Institute of Information Technology (I²IT)
 
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
International Institute of Information Technology (I²IT)
 
Writing Skills: Importance of Writing Skills
Writing Skills: Importance of Writing SkillsWriting Skills: Importance of Writing Skills
Writing Skills: Importance of Writing Skills
International Institute of Information Technology (I²IT)
 
Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself
International Institute of Information Technology (I²IT)
 
Servlet: A Server-side Technology
Servlet: A Server-side TechnologyServlet: A Server-side Technology
Servlet: A Server-side Technology
International Institute of Information Technology (I²IT)
 
What Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It WorksWhat Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It Works
International Institute of Information Technology (I²IT)
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
International Institute of Information Technology (I²IT)
 
Hypothesis-Testing
Hypothesis-TestingHypothesis-Testing
Hypothesis-Testing
International Institute of Information Technology (I²IT)
 
Data Science, Big Data, Data Analytics
Data Science, Big Data, Data AnalyticsData Science, Big Data, Data Analytics
Data Science, Big Data, Data Analytics
International Institute of Information Technology (I²IT)
 
Types of Artificial Intelligence
Types of Artificial Intelligence Types of Artificial Intelligence
Types of Artificial Intelligence
International Institute of Information Technology (I²IT)
 
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
International Institute of Information Technology (I²IT)
 
Sentiment Analysis in Machine Learning
Sentiment Analysis in  Machine LearningSentiment Analysis in  Machine Learning
Sentiment Analysis in Machine Learning
International Institute of Information Technology (I²IT)
 
What Is Cloud Computing?
What Is Cloud Computing?What Is Cloud Computing?
What Is Cloud Computing?
International Institute of Information Technology (I²IT)
 
Introduction To Design Pattern
Introduction To Design PatternIntroduction To Design Pattern
Introduction To Design Pattern
International Institute of Information Technology (I²IT)
 
Importance of Theory of Computations
Importance of Theory of ComputationsImportance of Theory of Computations
Importance of Theory of Computations
International Institute of Information Technology (I²IT)
 
Java as Object Oriented Programming Language
Java as Object Oriented Programming LanguageJava as Object Oriented Programming Language
Java as Object Oriented Programming Language
International Institute of Information Technology (I²IT)
 
What Is High Performance-Computing?
What Is High Performance-Computing?What Is High Performance-Computing?
What Is High Performance-Computing?
International Institute of Information Technology (I²IT)
 
Data Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BIData Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BI
International Institute of Information Technology (I²IT)
 
Ad

Recently uploaded (20)

DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
Lynda Kane
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Automation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From AnywhereAutomation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From Anywhere
Lynda Kane
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Buckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug LogsBuckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug Logs
Lynda Kane
 
"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko
Fwdays
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Hands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordDataHands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordData
Lynda Kane
 
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5..."Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
Fwdays
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.
gregtap1
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
Lynda Kane
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Automation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From AnywhereAutomation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From Anywhere
Lynda Kane
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Buckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug LogsBuckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug Logs
Lynda Kane
 
"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko
Fwdays
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Hands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordDataHands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordData
Lynda Kane
 
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5..."Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
"Client Partnership — the Path to Exponential Growth for Companies Sized 50-5...
Fwdays
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.
gregtap1
 

Constructors and Destructor in C++

  • 1. Constructors and Destructor in C++ Mr. Sarang Anil Saoji Information Technology Department International Institute of Information Technology, I²IT www.isquareit.edu.in
  • 2. Constructors • Characteristics: 1. The constructor is a special member function of a class. 2. The constructor name is same as the class name. 3. The constructor is invoked automatically whenever an object of its associated class is created. 4. It is called constructor because it constructs the values of the data member of the class.
  • 3. Constructors • Characteristics: 1. The constructor do not have return type even void. 2. The constructor must be declared in the public section. 3. The constructor cannot be virtual. 4. When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code.
  • 4. Constructors • Types of constructor: 1. Default constructor 2. Parameterized constructor 3. Copy constructor.
  • 5. Constructors • Default constructor:  It is the constructor which doesn’t take any argument. It has no parameters. • Parameterized constructor:  It is the constructor which has parameters. It allows us to pass arguments while object creation.
  • 6. Constructors • Example: class addition { int num; public: addition(); // default constructor addition(int); // parameterized constructor void sum( addition, addition ); void display(); }; int main() { addition a(10), b(20); // parameterized constructor invoked addition c; // default constructor invoked c.sum(a,b); c.display( ); }
  • 7. Constructors • Example: addition::addition() //Definition of default constructor { num=0; } addition::addition(int x) //Definition of parameterized constructor { num=x; } void addition::sum(addition m, addition n) { num=m.num+n.num; } void addition::display() { cout<<“nAddition is:”<<num; } Output - Addition is:30
  • 8. Constructors • Copy Constructor:  It is used to create a new object as a copy of an existing object.  The copy constructor is invoked if: a) Pass an object as an parameter to a call-by-value function: void addition::sum(addition m, addition n) { num=m.num+n.num; } c.sum(a,b); //copy constructor
  • 9. Constructors • Copy Constructor:  The copy constructor is invoked if: a) Return an object from a function: friend addition sum( addition, addition ); //declaration addition sum(addition x, addition y) //definition { addition temp; temp.num=x.num+y.num; return temp; //copy constructor invoked } c=sum(a,b); //calling
  • 10. Constructors • Copy Constructor:  The copy constructor is invoked if: a) Initialize an object from another object of the same type class item { int num; public: item() {num=10;} item( item &x) { num=x.num} //copy constructor declaration and definition void display() { cout<<“n Number is:”<<num; } };
  • 11. Constructors • Copy Constructor: int main() { item a; item b(a); // copy constructor invoked item c=b; // copy constructor invoked a.display(); b.display(); c.display(); } Output: Number is:10 Number is:10 Number is:10
  • 12. Destructor  A destructor is a special member function that destroy (or delete) the object.  A destructor is called automatically when  The program finished execution.  A scope (the { } parenthesis) containing object ends.  Call the delete operator.
  • 13. Destructor class book { int price; public: book() ~book(); //destructor declaration void display(); }; book::book() { price=200; cout<<“nConstructor”; } Book::~book() //destructor definition { cout<<“nDestructor”; } Void book::display() { cout<<“nPrice is:”<<price; }
  • 14. Destructor int main() { book b; b.display(); } //destructor invoked Output: Price is:200 Constructor Destructor
  • 15. Assignments:  Write a class complex as follows: Data members: Real and Imaginary members. Member functions: get data (use constructor), show data, add, subtract, multiply and divide. Use the concept of constructors and destructor.  A bag consists of zero or more objects of the same type. Each object can be described by its color and weight. Design C++ program to create a new object. This can be done in two ways. If the user provides information about color and/or weight of the object to be created then this information will be used to create the object otherwise the object will be created using default values for these attributes. Provide a facility to keep track of total number of objects and total weight of object from a bag. Use the concept of constructors and destructor..
  • 16. References:  E Balagurusamy, “ Object-Oriented Programming with C++”, Tata McGraw-Hill Education, 7th edition.  Schildt Herbert ,”C++: The Complete Reference”, 5th edition.