SlideShare a Scribd company logo
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(?:)
Most read
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);
Most read
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;
Most read
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 (20)

Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
 
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
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
Hitesh Kumar
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 

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

Operator overloading
Operator overloadingOperator overloading
Operator overloading
ramya marichamy
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
Amogh Kalyanshetti
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 
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 overloadng
Operator overloadngOperator overloadng
Operator overloadng
preethalal
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
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
 
22 scheme OOPs with C++ BCS306B_module3.pdf
22 scheme  OOPs with C++ BCS306B_module3.pdf22 scheme  OOPs with C++ BCS306B_module3.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
Polymorphism and Type Conversion.pdf pot
Polymorphism and Type Conversion.pdf potPolymorphism and Type Conversion.pdf pot
Polymorphism and Type Conversion.pdf pot
e13225064
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Padma Kannan
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
B.sc CSIT 2nd semester C++ Unit4
B.sc CSIT  2nd semester C++ Unit4B.sc CSIT  2nd semester C++ Unit4
B.sc CSIT 2nd semester C++ Unit4
Tekendra Nath Yogi
 
Lec 8.pdf a
Lec 8.pdf                                           aLec 8.pdf                                           a
Lec 8.pdf a
aliashraf9689
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
NabeelaNousheen
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
 
Oops
OopsOops
Oops
ankush_kumar
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
Amogh Kalyanshetti
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 
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 overloadng
Operator overloadngOperator overloadng
Operator overloadng
preethalal
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
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
 
22 scheme OOPs with C++ BCS306B_module3.pdf
22 scheme  OOPs with C++ BCS306B_module3.pdf22 scheme  OOPs with C++ BCS306B_module3.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
Polymorphism and Type Conversion.pdf pot
Polymorphism and Type Conversion.pdf potPolymorphism and Type Conversion.pdf pot
Polymorphism and Type Conversion.pdf pot
e13225064
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
B.sc CSIT 2nd semester C++ Unit4
B.sc CSIT  2nd semester C++ Unit4B.sc CSIT  2nd semester C++ Unit4
B.sc CSIT 2nd semester C++ Unit4
Tekendra Nath Yogi
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
NabeelaNousheen
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Ad

More from rajshreemuthiah (20)

oracle
oracleoracle
oracle
rajshreemuthiah
 
quality
qualityquality
quality
rajshreemuthiah
 
bigdata
bigdatabigdata
bigdata
rajshreemuthiah
 
polymorphism
polymorphismpolymorphism
polymorphism
rajshreemuthiah
 
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
 
Ad

Recently uploaded (20)

LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 

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;