SlideShare a Scribd company logo
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
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.
 When we need to initialize the variable of object with
the values of variable of another object of same type,
then we use concept of copy constructor.
 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;
}
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

Similar to constructor in object oriented program.pptx (20)

Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
रमन सनौरिया
 
Oops
OopsOops
Oops
Gayathri Ganesh
 
constructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxixconstructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxix
reetanarula4
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
constructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigigconstructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigig
SILENTGAMER45
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
Constructors or destructors unit(II).pdf
Constructors or destructors unit(II).pdfConstructors or destructors unit(II).pdf
Constructors or destructors unit(II).pdf
malviyatanishk8
 
.NET with C# - Destructor .pptx
.NET with C# -          Destructor .pptx.NET with C# -          Destructor .pptx
.NET with C# - Destructor .pptx
sujithangam
 
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
 
[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
 
Constructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptxConstructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptx
vijayaazeem
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdfConst-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
mohitsinha7739289047
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
Praveen M Jigajinni
 
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
 
Constructors & Destructors in C++ Simplified
Constructors & Destructors in C++ SimplifiedConstructors & Destructors in C++ Simplified
Constructors & Destructors in C++ Simplified
TaseenTariq1
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
Niti Arora
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
constructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxixconstructors shailee.pptxhhhtyygdxixixxxxix
constructors shailee.pptxhhhtyygdxixixxxxix
reetanarula4
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
constructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigigconstructor-.pptcucfkifkficuvguvucufjfugugigig
constructor-.pptcucfkifkficuvguvucufjfugugigig
SILENTGAMER45
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
Constructors or destructors unit(II).pdf
Constructors or destructors unit(II).pdfConstructors or destructors unit(II).pdf
Constructors or destructors unit(II).pdf
malviyatanishk8
 
.NET with C# - Destructor .pptx
.NET with C# -          Destructor .pptx.NET with C# -          Destructor .pptx
.NET with C# - Destructor .pptx
sujithangam
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
study material
 
[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
 
Constructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptxConstructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptx
vijayaazeem
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdfConst-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
mohitsinha7739289047
 
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 in C++ Simplified
Constructors & Destructors in C++ SimplifiedConstructors & Destructors in C++ Simplified
Constructors & Destructors in C++ Simplified
TaseenTariq1
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
Niti Arora
 

More from urvashipundir04 (20)

stack in python using different datatypes.pptx
stack in python using different datatypes.pptxstack in python using different datatypes.pptx
stack in python using different datatypes.pptx
urvashipundir04
 
Game Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptxGame Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptx
urvashipundir04
 
extended modelling in dbms using different.pptx
extended modelling in dbms using different.pptxextended modelling in dbms using different.pptx
extended modelling in dbms using different.pptx
urvashipundir04
 
PRODUCTION SYSTEM in data science .pptx
PRODUCTION SYSTEM in data science  .pptxPRODUCTION SYSTEM in data science  .pptx
PRODUCTION SYSTEM in data science .pptx
urvashipundir04
 
Presentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptxPresentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptx
urvashipundir04
 
Dependency modelling in data mining.pptx
Dependency modelling in data mining.pptxDependency modelling in data mining.pptx
Dependency modelling in data mining.pptx
urvashipundir04
 
INTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptxINTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptx
urvashipundir04
 
SOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptxSOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptx
urvashipundir04
 
datamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptxdatamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptx
urvashipundir04
 
datamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptxdatamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptx
urvashipundir04
 
Underfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptxUnderfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptx
urvashipundir04
 
introduction values and best practices in
introduction values and best practices inintroduction values and best practices in
introduction values and best practices in
urvashipundir04
 
ppt on different topics of circular.pptx
ppt on different topics of circular.pptxppt on different topics of circular.pptx
ppt on different topics of circular.pptx
urvashipundir04
 
list in python and traversal of list.pptx
list in python and traversal of list.pptxlist in python and traversal of list.pptx
list in python and traversal of list.pptx
urvashipundir04
 
ermodelN in database management system.ppt
ermodelN in database management system.pptermodelN in database management system.ppt
ermodelN in database management system.ppt
urvashipundir04
 
libraries in python using different .pptx
libraries in python using different .pptxlibraries in python using different .pptx
libraries in python using different .pptx
urvashipundir04
 
tuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptxtuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptx
urvashipundir04
 
ANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptxANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptx
urvashipundir04
 
dispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptxdispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptx
urvashipundir04
 
loopin gstatement in python using .pptx
loopin gstatement in python using  .pptxloopin gstatement in python using  .pptx
loopin gstatement in python using .pptx
urvashipundir04
 
stack in python using different datatypes.pptx
stack in python using different datatypes.pptxstack in python using different datatypes.pptx
stack in python using different datatypes.pptx
urvashipundir04
 
Game Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptxGame Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptx
urvashipundir04
 
extended modelling in dbms using different.pptx
extended modelling in dbms using different.pptxextended modelling in dbms using different.pptx
extended modelling in dbms using different.pptx
urvashipundir04
 
PRODUCTION SYSTEM in data science .pptx
PRODUCTION SYSTEM in data science  .pptxPRODUCTION SYSTEM in data science  .pptx
PRODUCTION SYSTEM in data science .pptx
urvashipundir04
 
Presentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptxPresentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptx
urvashipundir04
 
Dependency modelling in data mining.pptx
Dependency modelling in data mining.pptxDependency modelling in data mining.pptx
Dependency modelling in data mining.pptx
urvashipundir04
 
INTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptxINTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptx
urvashipundir04
 
SOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptxSOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptx
urvashipundir04
 
datamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptxdatamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptx
urvashipundir04
 
datamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptxdatamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptx
urvashipundir04
 
Underfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptxUnderfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptx
urvashipundir04
 
introduction values and best practices in
introduction values and best practices inintroduction values and best practices in
introduction values and best practices in
urvashipundir04
 
ppt on different topics of circular.pptx
ppt on different topics of circular.pptxppt on different topics of circular.pptx
ppt on different topics of circular.pptx
urvashipundir04
 
list in python and traversal of list.pptx
list in python and traversal of list.pptxlist in python and traversal of list.pptx
list in python and traversal of list.pptx
urvashipundir04
 
ermodelN in database management system.ppt
ermodelN in database management system.pptermodelN in database management system.ppt
ermodelN in database management system.ppt
urvashipundir04
 
libraries in python using different .pptx
libraries in python using different .pptxlibraries in python using different .pptx
libraries in python using different .pptx
urvashipundir04
 
tuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptxtuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptx
urvashipundir04
 
ANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptxANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptx
urvashipundir04
 
dispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptxdispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptx
urvashipundir04
 
loopin gstatement in python using .pptx
loopin gstatement in python using  .pptxloopin gstatement in python using  .pptx
loopin gstatement in python using .pptx
urvashipundir04
 
Ad

Recently uploaded (20)

Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Ad

constructor in object oriented program.pptx

  • 1. 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.
  • 2. 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.
  • 3. Constructors • Types of constructor: 1. Default constructor 2. Parameterized constructor 3. Copy constructor.
  • 4. 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.
  • 5. 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( ); }
  • 6. Constructors 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
  • 7. Constructors • Copy Constructor:  It is used to create a new object as a copy of an existing object.  When we need to initialize the variable of object with the values of variable of another object of same type, then we use concept of copy constructor.  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; }
  • 8. 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
  • 9. 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; } };
  • 10. 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
  • 11. 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.
  • 12. 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; }
  • 13. Destructor int main() { book b; b.display(); } //destructor invoked Output: Price is:200 Constructor Destructor
  • 14. 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..
  • 15. References:  E Balagurusamy, “ Object-Oriented Programming with C++”, Tata McGraw-Hill Education, 7th edition.  Schildt Herbert ,”C++: The Complete Reference”, 5th edition.
  翻译: