SlideShare una empresa de Scribd logo
1 de 9
OPERATOR OVERLOADING
Ramish Suleman
(IT)
Operator Overloading
Each C# operator has a predefined meaning. Most of them are given
additional meaning through the concept called operator overloading.
Main idea behind operator overloading is to use C# operators with the
class objects.
E.g.
‘+’ can be used to add 2 ints, floats and doubles, the same ‘+’ can be
used to add 2 class objects, thereby ‘+’ gets overloaded.
When an operator is overloaded, its original meaning is not lost.
Operator Overloading
Operator overloading can overload operators to work with class and
struct types.
Use keyword operator.
Follow with symbol
overload+
struct Point
{
int x;
int y;
public static Point operator+(Point p, Point q)
{
return new Point(p.x + q.x, p.y + q.y);
}
...
}
Using Overloaded Operator
Overloaded operator used like operators for other types.
Compiler translates into method call
use operator+
Point a = new Point(1, 2);
Point b = new Point(3, 4);
Point c = a + b;
Types of Operator Overloading
Binary Operators:
Binary operator works with two
parameters.
binary +
binary -
struct Point
{
int x;
int y;
public static Point operator+(Point p, Point q)
{
return new Point(p.x + q.x, p.y + q.y);
}
public static Point operator-(Point p, Point q)
{
return new Point(p.x - q.x, p.y - q.y);
}
...
}
Types of Operator Overloading
Unary Operators:
Unary operator works with single
parameter.
unary +
unary -
struct Point
{
int x;
int y;
public static Point operator+(Point p)
{
return new Point(p.x, p.y);
}
public static Point operator-(Point p)
{
return new Point(-p.x, -p.y);
}
...
}
Advantages of Operator Overloading
With operator overloading readability of the code improves.
With operator overloading the code becomes explicit in nature,
looking at the code, fellow developers can easily guess what is
going on.
With operator overloading the code looks more conventional and
becomes easy to follow.
The method defining operator overloading, should always be
static and public. Otherwise compiler error out with message
"User defined operator must always be static and public".
Limitations
Only some operators can be overloaded
Unary:
+ -! ~ ++ --true false
Binary:
+ -* / % & | ^ << >> == != > < >= <=
Cannot
o Create new operators.
o Change precedence.
o Change associativity.
o Change number of arguments.
o Overload prefix/postfix versions separately.
Summary
There are different ways in which operators may be overloaded
within C#. There are numerous ways that overloading can be used
to your advantage in custom classes and structs. It is now up to you
to decide how to best put it to use in your applications, and
important to remember that not all languages support operator
overloading.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Data types in java
Data types in javaData types in java
Data types in java
 
OOP C++
OOP C++OOP C++
OOP C++
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 

Similar a Operator overloading

Similar a Operator overloading (20)

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
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
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
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Oops
OopsOops
Oops
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary 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
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Week7a.pptx
Week7a.pptxWeek7a.pptx
Week7a.pptx
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 

Último

AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfSkillCertProExams
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoKayode Fayemi
 
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Delhi Call girls
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaKayode Fayemi
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfSenaatti-kiinteistöt
 
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verifiedSector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verifiedDelhi Call girls
 
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...Pooja Nehwal
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lodhisaajjda
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar TrainingKylaCullinane
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Baileyhlharris
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Vipesco
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...amilabibi1
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIINhPhngng3
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatmentnswingard
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalFabian de Rijk
 
Causes of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCauses of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCamilleBoulbin1
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxraffaeleoman
 

Último (18)

ICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdfICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdf
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
 
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
 
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verifiedSector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
 
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of Drupal
 
Causes of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCauses of poverty in France presentation.pptx
Causes of poverty in France presentation.pptx
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 

Operator overloading

  • 2. Operator Overloading Each C# operator has a predefined meaning. Most of them are given additional meaning through the concept called operator overloading. Main idea behind operator overloading is to use C# operators with the class objects. E.g. ‘+’ can be used to add 2 ints, floats and doubles, the same ‘+’ can be used to add 2 class objects, thereby ‘+’ gets overloaded. When an operator is overloaded, its original meaning is not lost.
  • 3. Operator Overloading Operator overloading can overload operators to work with class and struct types. Use keyword operator. Follow with symbol overload+ struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } ... }
  • 4. Using Overloaded Operator Overloaded operator used like operators for other types. Compiler translates into method call use operator+ Point a = new Point(1, 2); Point b = new Point(3, 4); Point c = a + b;
  • 5. Types of Operator Overloading Binary Operators: Binary operator works with two parameters. binary + binary - struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } public static Point operator-(Point p, Point q) { return new Point(p.x - q.x, p.y - q.y); } ... }
  • 6. Types of Operator Overloading Unary Operators: Unary operator works with single parameter. unary + unary - struct Point { int x; int y; public static Point operator+(Point p) { return new Point(p.x, p.y); } public static Point operator-(Point p) { return new Point(-p.x, -p.y); } ... }
  • 7. Advantages of Operator Overloading With operator overloading readability of the code improves. With operator overloading the code becomes explicit in nature, looking at the code, fellow developers can easily guess what is going on. With operator overloading the code looks more conventional and becomes easy to follow. The method defining operator overloading, should always be static and public. Otherwise compiler error out with message "User defined operator must always be static and public".
  • 8. Limitations Only some operators can be overloaded Unary: + -! ~ ++ --true false Binary: + -* / % & | ^ << >> == != > < >= <= Cannot o Create new operators. o Change precedence. o Change associativity. o Change number of arguments. o Overload prefix/postfix versions separately.
  • 9. Summary There are different ways in which operators may be overloaded within C#. There are numerous ways that overloading can be used to your advantage in custom classes and structs. It is now up to you to decide how to best put it to use in your applications, and important to remember that not all languages support operator overloading.