SlideShare a Scribd company logo
D.SHANMUGAPRIYA
I- M.Sc(IT)
OPERATOR OVERLOADING AND
TYPE CONVERSION
OPERATOR OVERLOADING AND
TYPE CONVERSION
 INTRODUCTION
 DEFINING OPERATOR OVERLOADING
 OVERLOADING UNARY OPERATORS
 OVERLOADI NG BINARY OPERATORS
 OVERLOADING BINARY OPERATORS USING
FRIENDS
 SOME OTHER OPERATOR OVERLOADING
EXAMPLES
 RULES FOR OVERLOADING OPERATORS
 TYPE CONVERSION
INTRODUCTION
Operator overloading is one of the many
exciting features of the C++ language.
Operator overloading provides a flexible option
for the creation of new definition for most of the
C++ operators.
Class member access operator(.,.*)
Scope resolution operator(::)
Size operator(size of)
Conditional operator(?:)
DEFINNING OPERATOR
OVERLOADING
We must specify what it means in relation to the class
to which the operator is applied.
Syntax:
Return type classname::operator op(arglist)
{
function body//task defined
}
 Operator functions must be either member functions or friend
functions:
 vector operator+(vector);//vectoraddition
 Vector operator-(vector)://unary minus
CONTINUE….
 Friend vector operator+(vector;vector); //vector addition
 Friend vector operator-(vector); //unary minus
 Vector operator-(vector&a); //subtraction
 Int operator==(vector,vector); //comparison
 The process of overloading involves the following steps:
 Create a class that defines the datatype that is to be used in the
overloading operation.
 Declare the operator functions operator op() in the public part of the
class.
 UNARY OPERATOR=X OP Y
 BINARY OPERATOR=OPERATOR OP(X)
OVERLOADING UNARY
OPERATORS
 A minus operator when use as a unary takes just one
operand.
 Remember a statement like
 It is possible to overload a unary minus operator using a friend
function as follows:
Friend void operator-(space&s);
void operator-(space&s)
S2=-S1;
S.X=-S.X;
S.Y=-S.Y;
S.Z=-S.Z;
Example:
OVERLOADING BINARY
OPERATORS
 The same mechanism can be used to overload a binary
operator.
 C1 takes the responsibility of invoking the function
and c2 plays the role of an argument that is passed to
the function.
 Both the objects are available for the function.
C=SUM(A,B);
C3=C1.OPERATOR+(C2);
TEMP.X=X+C.X;
Example:
IMPLEMENTED OF THE OVERLOAD BINARY
OPERATOR:
OVERLOADING BINARY
OPERATORS USING FRIENDS
 Friend functions may be used in the place of member functions
for overloading a binary operator.
1) Replace the member function declartion by the friend
function declaration
2) [friend complex operation+(complex,complex);
3) Redefine the operator function as followa:
[complex opearor+(complex a,complex b)
{
return complex((a.x+b.x),(a.y+b.y));
}
In this case the statement
C3=C1+C2;
C3=OPERATOR+(C1+C2);
MANIPULATION OF STRINGS
USING OPERATORS
 ANSI C implements strings using character arrays,pointers and string
functions.
 EXAMPLE:
 Thus we must create string objects that we can hold these two pieces of
information.
Class string
{
Char*p;
Int len;
Public:
}
String3=string1+string2;
If(string 1>=string2)string=string1;
MATHEMATICAL OPERATIONS ON
STRING:
SUB TOPICS
OVERLOADING THE SUBSCRIPT OPERATOR
o The subscript operator is normally used to access and
modify a specific element in an array.
OVERLOADING THE POINTER TO MEMBER -
>OPERATOR:
o The pointer to member operator is normally used to
conjunction with an object pointer to access any of the
objects member.
RULES FOR OVERLOADING
OPERATOR
 There are certain restrictions and limitations in
overloading them,some of them are listed below
Only existing operators can be overloaded
Overloaded operators follow the syntax rules of the
original operators.
There are some operators that cannot be overloaded.
SIZE OF SIZE OF OPERATOR
. Membership operator
.* pointer to member operator
:: scope resolution operator
?: conditional operator
TYPE CONVERSION
 The type of data to the right of an assignment
operator is automatically converted to the type of
the variable on the left.
BASIC TO CLASS TYPE:
1) It may be recalled that the use of constructor was
illustrated in a number of examples to initialize
objects.
int m;
float x=3.14159;
m=x;
Continue…
 Following constructor:
string::string(char*a)
{
length=strlength(a);
p=new char[length+1];
strcpy(p,a);
}
The statement
s1=string(name 1);
First converts name1 from char* type and then values the
assign the strings type values to the objects S1
S2=Name 2;
T
H
A
N
K
y
O
U

More Related Content

What's hot

Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
Sachin Yadav
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
Rai University
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 

What's hot (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Friend functions
Friend functions Friend functions
Friend functions
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
Class and object
Class and objectClass and object
Class and object
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similar to Operator overloading and type conversion in cpp

Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Garima Singh Makhija
 
C++ language
C++ languageC++ language
C++ language
Hamza Asif
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
Amogh Kalyanshetti
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
Ilio Catallo
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
shubhra chauhan
 

Similar to Operator overloading and type conversion in cpp (20)

Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ language
C++ languageC++ language
C++ language
 
Oops
OopsOops
Oops
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Bc0037
Bc0037Bc0037
Bc0037
 
Lecture5
Lecture5Lecture5
Lecture5
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 

More from rajshreemuthiah

oracle
oracleoracle
quality
qualityquality
bigdata
bigdatabigdata
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
rajshreemuthiah
 
interface
interfaceinterface
interface
rajshreemuthiah
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
rajshreemuthiah
 
concurrency control
concurrency controlconcurrency control
concurrency control
rajshreemuthiah
 
Education
EducationEducation
Education
rajshreemuthiah
 
Formal verification
Formal verificationFormal verification
Formal verification
rajshreemuthiah
 
Transaction management
Transaction management Transaction management
Transaction management
rajshreemuthiah
 
Multi thread
Multi threadMulti thread
Multi thread
rajshreemuthiah
 
System testing
System testingSystem testing
System testing
rajshreemuthiah
 
software maintenance
software maintenancesoftware maintenance
software maintenance
rajshreemuthiah
 
exception handling
exception handlingexception handling
exception handling
rajshreemuthiah
 
e governance
e governancee governance
e governance
rajshreemuthiah
 
recovery management
recovery managementrecovery management
recovery management
rajshreemuthiah
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
rajshreemuthiah
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
rajshreemuthiah
 
os linux
os linuxos linux
os linux
rajshreemuthiah
 

More from rajshreemuthiah (20)

oracle
oracleoracle
oracle
 
quality
qualityquality
quality
 
bigdata
bigdatabigdata
bigdata
 
polymorphism
polymorphismpolymorphism
polymorphism
 
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
 
interface
interfaceinterface
interface
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
 
concurrency control
concurrency controlconcurrency control
concurrency control
 
Education
EducationEducation
Education
 
Formal verification
Formal verificationFormal verification
Formal verification
 
Transaction management
Transaction management Transaction management
Transaction management
 
Multi thread
Multi threadMulti thread
Multi thread
 
System testing
System testingSystem testing
System testing
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
exception handling
exception handlingexception handling
exception handling
 
e governance
e governancee governance
e governance
 
recovery management
recovery managementrecovery management
recovery management
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
 
os linux
os linuxos linux
os linux
 

Recently uploaded

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

Operator overloading and type conversion in cpp

  • 2. OPERATOR OVERLOADING AND TYPE CONVERSION  INTRODUCTION  DEFINING OPERATOR OVERLOADING  OVERLOADING UNARY OPERATORS  OVERLOADI NG BINARY OPERATORS  OVERLOADING BINARY OPERATORS USING FRIENDS  SOME OTHER OPERATOR OVERLOADING EXAMPLES  RULES FOR OVERLOADING OPERATORS  TYPE CONVERSION
  • 3. INTRODUCTION Operator overloading is one of the many exciting features of the C++ language. Operator overloading provides a flexible option for the creation of new definition for most of the C++ operators. Class member access operator(.,.*) Scope resolution operator(::) Size operator(size of) Conditional operator(?:)
  • 4. DEFINNING OPERATOR OVERLOADING We must specify what it means in relation to the class to which the operator is applied. Syntax: Return type classname::operator op(arglist) { function body//task defined }  Operator functions must be either member functions or friend functions:  vector operator+(vector);//vectoraddition  Vector operator-(vector)://unary minus
  • 5. CONTINUE….  Friend vector operator+(vector;vector); //vector addition  Friend vector operator-(vector); //unary minus  Vector operator-(vector&a); //subtraction  Int operator==(vector,vector); //comparison  The process of overloading involves the following steps:  Create a class that defines the datatype that is to be used in the overloading operation.  Declare the operator functions operator op() in the public part of the class.  UNARY OPERATOR=X OP Y  BINARY OPERATOR=OPERATOR OP(X)
  • 6. OVERLOADING UNARY OPERATORS  A minus operator when use as a unary takes just one operand.  Remember a statement like  It is possible to overload a unary minus operator using a friend function as follows: Friend void operator-(space&s); void operator-(space&s) S2=-S1; S.X=-S.X; S.Y=-S.Y; S.Z=-S.Z;
  • 8. OVERLOADING BINARY OPERATORS  The same mechanism can be used to overload a binary operator.  C1 takes the responsibility of invoking the function and c2 plays the role of an argument that is passed to the function.  Both the objects are available for the function. C=SUM(A,B); C3=C1.OPERATOR+(C2); TEMP.X=X+C.X;
  • 10. IMPLEMENTED OF THE OVERLOAD BINARY OPERATOR:
  • 11. OVERLOADING BINARY OPERATORS USING FRIENDS  Friend functions may be used in the place of member functions for overloading a binary operator. 1) Replace the member function declartion by the friend function declaration 2) [friend complex operation+(complex,complex); 3) Redefine the operator function as followa: [complex opearor+(complex a,complex b) { return complex((a.x+b.x),(a.y+b.y)); } In this case the statement C3=C1+C2; C3=OPERATOR+(C1+C2);
  • 12. MANIPULATION OF STRINGS USING OPERATORS  ANSI C implements strings using character arrays,pointers and string functions.  EXAMPLE:  Thus we must create string objects that we can hold these two pieces of information. Class string { Char*p; Int len; Public: } String3=string1+string2; If(string 1>=string2)string=string1;
  • 14. SUB TOPICS OVERLOADING THE SUBSCRIPT OPERATOR o The subscript operator is normally used to access and modify a specific element in an array. OVERLOADING THE POINTER TO MEMBER - >OPERATOR: o The pointer to member operator is normally used to conjunction with an object pointer to access any of the objects member.
  • 15. RULES FOR OVERLOADING OPERATOR  There are certain restrictions and limitations in overloading them,some of them are listed below Only existing operators can be overloaded Overloaded operators follow the syntax rules of the original operators. There are some operators that cannot be overloaded. SIZE OF SIZE OF OPERATOR . Membership operator .* pointer to member operator :: scope resolution operator ?: conditional operator
  • 16. TYPE CONVERSION  The type of data to the right of an assignment operator is automatically converted to the type of the variable on the left. BASIC TO CLASS TYPE: 1) It may be recalled that the use of constructor was illustrated in a number of examples to initialize objects. int m; float x=3.14159; m=x;
  • 17. Continue…  Following constructor: string::string(char*a) { length=strlength(a); p=new char[length+1]; strcpy(p,a); } The statement s1=string(name 1); First converts name1 from char* type and then values the assign the strings type values to the objects S1 S2=Name 2;