SlideShare a Scribd company logo
Operator
overloading
(binary)
Let’s begin..
Content-:
• Operator overloading
• Need for operator overloading
• Overload able/non-overload able operators
• Binary operator overloading
• Back to basics
• How to perform operator overloading in C++
• Overloading of addition operator
What is operator overloading?
• C++ allows us to specify more than one
definition for an operator in the same scope,
which is called operator overloading.
• It is a type of polymorphism in which an
operator is overloaded to give user defined
meaning to it.
• An overloaded declaration is a declaration that is
declared with the same name as a previously
declared declaration in the same scope, except
that both declarations have different arguments
and obviously different definition.
What is operator overloading?
• When you call an overloaded operator, the compiler
determines the most appropriate definition to use,
by comparing the argument types you have used to
call the operator with the parameter types specified
in the definitions.
• Basic meaning of operator must not be changed.
• Keyword "operator" followed by the symbol for the
operator being defined. Like any other function, an
overloaded operator has a return type and a
parameter list.
• We can redefine or overload most of the built-in
operators available in C++.Only existing operator
can be overload.
Need for operator
overloading :
• In order make statement (like
obj3=obj1*obj2) valid in C++, we need
to overload the operators.
• Because compiler doesn’t know how to
perform operations on object.
+ - * / % ^
& | ~ ! = &&
< > <= >= ++ --
<< >> == != || +=
-= *= /= %= [] ()
:: .* . ?:
Overload able/Non-overload able Operators:
Following is the list of operators which can be overloaded −
Following is the list of operators which cannot be overloaded −
Binary operator overloading:
• The binary operators take two operands.
• One argument is atleast must be there in arguments
list.
• We use binary operators very frequently like
addition (+) operator, subtraction (-) operator,
division (/) operator and modulus (%) operator.
• Syntax:
return type operator (argument)
{
code to be executed;
return value;
}
Overload able binary operators:
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
• Bitwise operators
• Less than (<)
• Greater than(>)
• Equals to(=)
• Not equals to (!=)
• Less than equals to (<=)
• Greater than equals to (>=)
Back to the basics!
C Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“n enter two numbers : ”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“n c = %d”,c);
getch();
}
int a int b
int c
44 35
79
How to perform operator
overloading in C++
Method
//inside class
object operator+ (object obj)
{
object temp;
temp.a = a + obj.a;
return temp;
}
void main()
{
object obj1,obj2,obj3;
//getdata code
obj3 = obj1 + obj2;
//display code
}
obj3 = obj1 + obj2;
ArgumentReturn value
Operator
member
function
Caller object
Binary overloading of (+) operator:
#include<iostream.h>
#include<conio.h>
class demo{
int a;
public:
void setdata(int x){
a=x;
}
void showdata(){
cout<<a;
}
demo operator + (demo d){
demo temp;
temp.a = a + d.a;
return temp;
}
};
void main()
{
demo d1,d2,d3;
clrscr();
d1.setdata(6);
d2.setdata(2);
d3 = d1 + d2;
d3.showdata();
getch();
}
demo d1 demo d2
demo d3
6
8
2
Thank you

More Related Content

What's hot

Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional Programming
Sartaj Singh
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
primeteacher32
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
BalajiGovindan5
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
BalajiGovindan5
 
Java script function
Java script functionJava script function
Java script function
suresh raj sharma
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)Jay Patel
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
Son Nguyen
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
Type conversions
Type conversionsType conversions
Type conversions
sanya6900
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
Learn By Watch
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
primeteacher32
 
Functional programming in Javascript
Functional programming in JavascriptFunctional programming in Javascript
Functional programming in Javascript
Knoldus Inc.
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
Marcus Denker
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
CSEC Mathematics Review - Introduction To Functions & Relations
CSEC Mathematics Review - Introduction To Functions & RelationsCSEC Mathematics Review - Introduction To Functions & Relations
CSEC Mathematics Review - Introduction To Functions & Relations
Kevin Small
 
Function
FunctionFunction
Function
Saniati
 

What's hot (20)

Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional Programming
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
Java script function
Java script functionJava script function
Java script function
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Type conversions
Type conversionsType conversions
Type conversions
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Functional programming in Javascript
Functional programming in JavascriptFunctional programming in Javascript
Functional programming in Javascript
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
CSEC Mathematics Review - Introduction To Functions & Relations
CSEC Mathematics Review - Introduction To Functions & RelationsCSEC Mathematics Review - Introduction To Functions & Relations
CSEC Mathematics Review - Introduction To Functions & Relations
 
Function
FunctionFunction
Function
 

Similar to Operator overloading (binary)

OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
sana younas
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Garima Singh Makhija
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 
C++_overloading.ppt
C++_overloading.pptC++_overloading.ppt
C++_overloading.ppt
Satyanandaram Nandigam
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Syed Arslan Rizvi
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563Youth For Peace
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
Thapar Institute
 

Similar to Operator overloading (binary) (20)

OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Oops
OopsOops
Oops
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
C++_overloading.ppt
C++_overloading.pptC++_overloading.ppt
C++_overloading.ppt
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)Programming course slides c++ (Prof Mansoor Bhatti)
Programming course slides c++ (Prof Mansoor Bhatti)
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 

Recently uploaded

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
varshanayak241
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 

Recently uploaded (20)

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 

Operator overloading (binary)

  • 2. Content-: • Operator overloading • Need for operator overloading • Overload able/non-overload able operators • Binary operator overloading • Back to basics • How to perform operator overloading in C++ • Overloading of addition operator
  • 3. What is operator overloading? • C++ allows us to specify more than one definition for an operator in the same scope, which is called operator overloading. • It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. • An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition.
  • 4. What is operator overloading? • When you call an overloaded operator, the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the operator with the parameter types specified in the definitions. • Basic meaning of operator must not be changed. • Keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list. • We can redefine or overload most of the built-in operators available in C++.Only existing operator can be overload.
  • 5. Need for operator overloading : • In order make statement (like obj3=obj1*obj2) valid in C++, we need to overload the operators. • Because compiler doesn’t know how to perform operations on object.
  • 6. + - * / % ^ & | ~ ! = && < > <= >= ++ -- << >> == != || += -= *= /= %= [] () :: .* . ?: Overload able/Non-overload able Operators: Following is the list of operators which can be overloaded − Following is the list of operators which cannot be overloaded −
  • 7. Binary operator overloading: • The binary operators take two operands. • One argument is atleast must be there in arguments list. • We use binary operators very frequently like addition (+) operator, subtraction (-) operator, division (/) operator and modulus (%) operator. • Syntax: return type operator (argument) { code to be executed; return value; }
  • 8. Overload able binary operators: • Addition (+) • Subtraction (-) • Multiplication (*) • Division (/) • Modulus (%) • Bitwise operators • Less than (<) • Greater than(>) • Equals to(=) • Not equals to (!=) • Less than equals to (<=) • Greater than equals to (>=)
  • 9. Back to the basics!
  • 10. C Program #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf(“n enter two numbers : ”); scanf(“%d%d”,&a,&b); c=a+b; printf(“n c = %d”,c); getch(); } int a int b int c 44 35 79
  • 11. How to perform operator overloading in C++
  • 12. Method //inside class object operator+ (object obj) { object temp; temp.a = a + obj.a; return temp; } void main() { object obj1,obj2,obj3; //getdata code obj3 = obj1 + obj2; //display code } obj3 = obj1 + obj2; ArgumentReturn value Operator member function Caller object
  • 13. Binary overloading of (+) operator: #include<iostream.h> #include<conio.h> class demo{ int a; public: void setdata(int x){ a=x; } void showdata(){ cout<<a; } demo operator + (demo d){ demo temp; temp.a = a + d.a; return temp; } }; void main() { demo d1,d2,d3; clrscr(); d1.setdata(6); d2.setdata(2); d3 = d1 + d2; d3.showdata(); getch(); } demo d1 demo d2 demo d3 6 8 2