SlideShare a Scribd company logo
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors
Constructors
Classes control object initialization by defining one or more
special member functions known as constructors.
The job of a constructor is to initialize the data members of a
class object.
A constructor is run whenever an object of a class type is
created.
Constructors have the same name as the class.
Unlike other functions, constructors have no return type.
Constructors can not be virtual.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors
•A class can have multiple constructors.
•Like any other overloaded function, the constructors
must differ from each other in the number or types of
their parameters.
•Constructors are the special type of member functions
that initializes the object automatically when it is
created.
•Compiler identifies that the given member function is a
constructor by its name
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors
•A class can have multiple constructors.
•Like any other overloaded function, the constructors
must differ from each other in the number or types of
their parameters.
•Constructors are the special type of member functions
that initializes the object automatically when it is
created.
•Compiler identifies that the given member function is a
constructor by its name
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructor - Syntax
class classname {
public:
//Declaring the default constructor.
classname() {
}
};
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Types of constructors
•There are several forms in which a constructor can
take its shape namely:
•Default Constructor
•Parameterized Constructor
•Copy Constructor
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Default constructor
•Classes control default initialization by defining a
special constructor, known as the default constructor.
The default constructor is one that takes no arguments.
•If the class does not explicitly define any constructors,
the compiler will implicitly define the default
constructor.
•The compiler-generated constructor is known as the
synthesized default constructor.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Default constructor – Example1
int main()
{
math o;
o.add();
return 0;
}
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
class math
{
private:
int a,b,c;
public:
math()
{
a=0;
b=0;
}
void add()
{
c=a+b;
cout<<"Total : "<<c;
}
};
Default constructor -Example
#include <iostream>
using namespace std;
class Base {
public: int square;
Base(int x = 5) // Default constructor with a default value.
square = x * x;
}
void show() {
cout << "Square of number =" << square << endl;
}
};
int main() {
Base b;
b.show();
return 0; }
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor
•A parameterized constructor is the one that has
parameters or arguments specified in it.
•We can pass the arguments to constructor function
when object is created.
•A constructor that can take arguments is called
parameterized constructor.
•When a constructor is parameterized, we must pass
the initial values as arguments to the constructor
function when an object is declared.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor -Syntax
class classname {
public:
//Declaring the default constructor.
classname( int, int ) {
}
};
Classname objname(list of parameters);
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor -example
• class math
• {
• private:
• int a,b,c;
• public:
• math(int x,int y)
• {
• a=x;
• b=y;
• }
• void add()
• {
• c=a+b;
• cout<<"Total : "<<c;
• }
• };
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
• int main()
• {
• math o(10,25);
• o.add();
• return 0;
• }
Copy constructor
• A copy constructor is a member function which initializes an object
using another object of the same class. It is used to declare and
initialize an object from another object.
• The most common form of copy constructor is shown here:
• classname (const classname &obj)
• { // body of constructor }
• Here, obj is a reference to an object that is being used to initialize
another object.
• Copy constructor object creation:
• integer (integer & i) ; (or)
• integer I 2 ( I 1 ) ; (or)
• integer I 2 = I 1 ;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Copy constructor
• The copy constructor creates an object by initializing it with an object
of the same class, which has been created previously.
• The copy constructor is used to:
• Initialize one object from another of the same type.
• Copy an object to pass it as an argument to a function.
• Copy an object to return it from a function.
• The process of initializing through a copy constructor is known as copy
initialization.
• A reference variable has been used as an argument to the copy
constructor as we cannot pass the argument by value to a copy
constructor
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Copy Constructor Example
• class math
• {
• private:
• int a,b,c;
• public:
• math(int x,int y)
• {
• a=x;
• b=y;
• }
• math(math &x1)
• {
• a=x1.a;
• b=x1.b;
• }
• void add()
• {
• c=a+b;
• cout<<"Total : "<<c<<endl;
• }
• int main()
• {
• math o(10,25);
• math o1(o);
• o.add();
• o1.add();
• return 0;
• }
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
Destructors
• A destructor is a special member function of a class that is executed
whenever an object of it's class goes out of scope.
• A destructor will have exact same name as the class prefixed with a
tilde (~).
• It can neither return a value nor can it take any parameters.
• Destructor can be very useful for releasing resources before coming
out of the program like closing files, releasing memories etc.
• The destructor is commonly used to "clean up" when an object is no
longer necessary
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Destructors
•Destructors are called when one of the following
events occurs:
•An object allocated using the new operator is explicitly
deallocated using the delete operator.
•A local (automatic) object with block scope goes out of
scope.
•The lifetime of a temporary object ends.
•A program ends and global or static objects exist.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Destructor syntax
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Destructor Example
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Ad

More Related Content

Similar to 4.Constructor.pptx for oops programming language (20)

Constructors
ConstructorsConstructors
Constructors
Puneet tiwari
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
Karthik Sekar
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
C++ Presen. tation.pptx
C++ Presen.                   tation.pptxC++ Presen.                   tation.pptx
C++ Presen. tation.pptx
mohitsinha7739289047
 
Constructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptxConstructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptx
vijayaazeem
 
[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
 
Oops
OopsOops
Oops
Gayathri Ganesh
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
KavitaHegde4
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
KavitaHegde4
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
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
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
myrajendra
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
AbhimanyuKumarYadav3
 
Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptx
SouravKrishnaBaul
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
Epsiba1
 
Constructor destructor.ppt
Constructor destructor.pptConstructor destructor.ppt
Constructor destructor.ppt
Karthik Sekar
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
Constructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptxConstructors and Destructor_detilaed contents.pptx
Constructors and Destructor_detilaed contents.pptx
vijayaazeem
 
[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
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
KavitaHegde4
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
KavitaHegde4
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
myrajendra
 
Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptx
SouravKrishnaBaul
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
Saharsh Anand
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
Epsiba1
 

More from sanketkashyap2023 (8)

18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx
sanketkashyap2023
 
19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh
sanketkashyap2023
 
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
sanketkashyap2023
 
15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg
sanketkashyap2023
 
9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp
sanketkashyap2023
 
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
sanketkashyap2023
 
3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language
sanketkashyap2023
 
2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps
sanketkashyap2023
 
18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx
sanketkashyap2023
 
19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh
sanketkashyap2023
 
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
sanketkashyap2023
 
15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg
sanketkashyap2023
 
9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp
sanketkashyap2023
 
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
sanketkashyap2023
 
3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language
sanketkashyap2023
 
2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps
sanketkashyap2023
 
Ad

Recently uploaded (20)

Process Mining at Deutsche Bank - Journey
Process Mining at Deutsche Bank - JourneyProcess Mining at Deutsche Bank - Journey
Process Mining at Deutsche Bank - Journey
Process mining Evangelist
 
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
Taqyea
 
Time series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdfTime series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdf
asmaamahmoudsaeed
 
problem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursingproblem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursing
vishnudathas123
 
Lagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdfLagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdf
benuju2016
 
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
muhammed84essa
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
Automation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success storyAutomation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success story
Process mining Evangelist
 
How to Set Up Process Mining in a Decentralized Organization?
How to Set Up Process Mining in a Decentralized Organization?How to Set Up Process Mining in a Decentralized Organization?
How to Set Up Process Mining in a Decentralized Organization?
Process mining Evangelist
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
Automated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptxAutomated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptx
handrymaharjan23
 
Voice Control robotic arm hggyghghgjgjhgjg
Voice Control robotic arm hggyghghgjgjhgjgVoice Control robotic arm hggyghghgjgjhgjg
Voice Control robotic arm hggyghghgjgjhgjg
4mg22ec401
 
Sets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledgeSets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledge
saumyasl2020
 
Agricultural_regionalisation_in_India(Final).pptx
Agricultural_regionalisation_in_India(Final).pptxAgricultural_regionalisation_in_India(Final).pptx
Agricultural_regionalisation_in_India(Final).pptx
mostafaahammed38
 
文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询
文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询
文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询
Taqyea
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
OlhaTatokhina1
 
What is ETL? Difference between ETL and ELT?.pdf
What is ETL? Difference between ETL and ELT?.pdfWhat is ETL? Difference between ETL and ELT?.pdf
What is ETL? Difference between ETL and ELT?.pdf
SaikatBasu37
 
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdfTOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
NhiV747372
 
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
Taqyea
 
Time series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdfTime series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdf
asmaamahmoudsaeed
 
problem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursingproblem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursing
vishnudathas123
 
Lagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdfLagos School of Programming Final Project Updated.pdf
Lagos School of Programming Final Project Updated.pdf
benuju2016
 
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
muhammed84essa
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
Automation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success storyAutomation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success story
Process mining Evangelist
 
How to Set Up Process Mining in a Decentralized Organization?
How to Set Up Process Mining in a Decentralized Organization?How to Set Up Process Mining in a Decentralized Organization?
How to Set Up Process Mining in a Decentralized Organization?
Process mining Evangelist
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
Automated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptxAutomated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptx
handrymaharjan23
 
Voice Control robotic arm hggyghghgjgjhgjg
Voice Control robotic arm hggyghghgjgjhgjgVoice Control robotic arm hggyghghgjgjhgjg
Voice Control robotic arm hggyghghgjgjhgjg
4mg22ec401
 
Sets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledgeSets theories and applications that can used to imporve knowledge
Sets theories and applications that can used to imporve knowledge
saumyasl2020
 
Agricultural_regionalisation_in_India(Final).pptx
Agricultural_regionalisation_in_India(Final).pptxAgricultural_regionalisation_in_India(Final).pptx
Agricultural_regionalisation_in_India(Final).pptx
mostafaahammed38
 
文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询
文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询
文凭证书美国SDSU文凭圣地亚哥州立大学学生证学历认证查询
Taqyea
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
OlhaTatokhina1
 
What is ETL? Difference between ETL and ELT?.pdf
What is ETL? Difference between ETL and ELT?.pdfWhat is ETL? Difference between ETL and ELT?.pdf
What is ETL? Difference between ETL and ELT?.pdf
SaikatBasu37
 
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdfTOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
NhiV747372
 
Ad

4.Constructor.pptx for oops programming language

  • 1. Dr.J.saira banu , Associate Professor, SCOPE, VIT University Constructors
  • 2. Constructors Classes control object initialization by defining one or more special member functions known as constructors. The job of a constructor is to initialize the data members of a class object. A constructor is run whenever an object of a class type is created. Constructors have the same name as the class. Unlike other functions, constructors have no return type. Constructors can not be virtual. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 3. Constructors •A class can have multiple constructors. •Like any other overloaded function, the constructors must differ from each other in the number or types of their parameters. •Constructors are the special type of member functions that initializes the object automatically when it is created. •Compiler identifies that the given member function is a constructor by its name Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 4. Constructors •A class can have multiple constructors. •Like any other overloaded function, the constructors must differ from each other in the number or types of their parameters. •Constructors are the special type of member functions that initializes the object automatically when it is created. •Compiler identifies that the given member function is a constructor by its name Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 5. Constructor - Syntax class classname { public: //Declaring the default constructor. classname() { } }; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 6. Types of constructors •There are several forms in which a constructor can take its shape namely: •Default Constructor •Parameterized Constructor •Copy Constructor Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 7. Default constructor •Classes control default initialization by defining a special constructor, known as the default constructor. The default constructor is one that takes no arguments. •If the class does not explicitly define any constructors, the compiler will implicitly define the default constructor. •The compiler-generated constructor is known as the synthesized default constructor. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 8. Default constructor – Example1 int main() { math o; o.add(); return 0; } Dr.J.saira banu , Associate Professor, SCOPE, VIT University class math { private: int a,b,c; public: math() { a=0; b=0; } void add() { c=a+b; cout<<"Total : "<<c; } };
  • 9. Default constructor -Example #include <iostream> using namespace std; class Base { public: int square; Base(int x = 5) // Default constructor with a default value. square = x * x; } void show() { cout << "Square of number =" << square << endl; } }; int main() { Base b; b.show(); return 0; } Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 10. Parameterized constructor •A parameterized constructor is the one that has parameters or arguments specified in it. •We can pass the arguments to constructor function when object is created. •A constructor that can take arguments is called parameterized constructor. •When a constructor is parameterized, we must pass the initial values as arguments to the constructor function when an object is declared. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 11. Parameterized constructor -Syntax class classname { public: //Declaring the default constructor. classname( int, int ) { } }; Classname objname(list of parameters); Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 12. Parameterized constructor -example • class math • { • private: • int a,b,c; • public: • math(int x,int y) • { • a=x; • b=y; • } • void add() • { • c=a+b; • cout<<"Total : "<<c; • } • }; Dr.J.saira banu , Associate Professor, SCOPE, VIT University • int main() • { • math o(10,25); • o.add(); • return 0; • }
  • 13. Copy constructor • A copy constructor is a member function which initializes an object using another object of the same class. It is used to declare and initialize an object from another object. • The most common form of copy constructor is shown here: • classname (const classname &obj) • { // body of constructor } • Here, obj is a reference to an object that is being used to initialize another object. • Copy constructor object creation: • integer (integer & i) ; (or) • integer I 2 ( I 1 ) ; (or) • integer I 2 = I 1 ; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 14. Copy constructor • The copy constructor creates an object by initializing it with an object of the same class, which has been created previously. • The copy constructor is used to: • Initialize one object from another of the same type. • Copy an object to pass it as an argument to a function. • Copy an object to return it from a function. • The process of initializing through a copy constructor is known as copy initialization. • A reference variable has been used as an argument to the copy constructor as we cannot pass the argument by value to a copy constructor Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 15. Copy Constructor Example • class math • { • private: • int a,b,c; • public: • math(int x,int y) • { • a=x; • b=y; • } • math(math &x1) • { • a=x1.a; • b=x1.b; • } • void add() • { • c=a+b; • cout<<"Total : "<<c<<endl; • } • int main() • { • math o(10,25); • math o1(o); • o.add(); • o1.add(); • return 0; • } Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 16. Destructors • A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope. • A destructor will have exact same name as the class prefixed with a tilde (~). • It can neither return a value nor can it take any parameters. • Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc. • The destructor is commonly used to "clean up" when an object is no longer necessary Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 17. Destructors •Destructors are called when one of the following events occurs: •An object allocated using the new operator is explicitly deallocated using the delete operator. •A local (automatic) object with block scope goes out of scope. •The lifetime of a temporary object ends. •A program ends and global or static objects exist. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 18. Destructor syntax Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 19. Destructor Example Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  翻译: