際際滷

際際滷Share a Scribd company logo
Department of I.T. Engineering
Presentation
On
Operator Overloading
Academic year 2018-19
Laxmi Institute of Technology, Sarigam
Approved by AICTE, New Delhi; Affiliated to Gujarat Technological University, Ahmedabad
Enrolment
number
Name
160860116018 Nishant P. Joshi
160860116029 Raj M. Patel
160860116032 Udit A. Patel
Content
 Introduction
 Operator overloading
 Defining operator overloading
 Overloading Input/output operator
 Overloading Unary Operator
 Overloading Binary Operator
 Rules for overloading operator
 Restrictions on Operator Overloading
Introduction
 Overloading an operator
 Write function definition as normal
 Function name is keyword operator followed by the
symbol for the operator being overloaded
 operator+ used to overload the addition operator (+)
 Using operators
 To use an operator on a class object it must be
overloaded unless the assignment operator(=)or the
address operator(&)
 Assignment operator by default performs
memberwise assignment
 Address operator (&) by default returns the address
of an object
Operator overloading
 Addition (+) operator can work on operands of type
char, int, float & double.
 However, if s1, s2, s3 are objects of the class string, the
we can write the statement,
s3 = s1 + s2;
 This means C++ has the ability to provide the operators
with a special meaning for a data type.
 Mechanism of giving special meaning to an operator is
known as operator overloading.
Operator overloading
 Operator  is a symbol that indicates an operation.
 Overloading  assigning different meanings to an
operation, depending upon the context.
 For example:
input(>>) / output(<<) operator
 The built-in definition of the operator << is for
shifting of bits.
 It is also used for displaying the values of various
data types
Defining operator overloading
 The general form of an operator function is:
return-type class-name :: operator op (argList)
{
function body // task defined.
}
 where return-type is the type of value returned by
the specified operation.
 op is the operator being overloaded.
 operator op is the function name, where operator is
a keyword.
Overloading Input/output operator
 C++ is able to input and output the built-in data types
using the stream extraction operator >> and the stream
insertion operator <<.
 Overloaded to perform input/output for user defined data
types.
 Left Operand will be of types ostream & and istream &.
 Function overloading this operator must be a Non-
Member function because left operand is not an Object
of the class.
 It must be a friend function to access private data
members.
Overloading Input/output operator
#include<iostream>
using namespace std;
class time
{
int hr,min,sec;
public: time()
{
hr=0, min=0; sec=0;
}
time(int h,int m, int s)
{
hr=h, min=m; sec=s;
}
friend ostream& operator << (ostream &out, time &tm);
//overloading <<' operator
};
Overloading Input/output operator
ostream& operator << (ostream &out, time &tm) //operator function
{
out << "Time is " << tm.hr << "hour : " << tm.min<< "min : " <<
tm.sec
<< "sec";
return out;
}
int main()
{
time tm(3,15,45); cout << tm; return 0;
}
Output:
Time is 3 hour : 15 min : 45 sec
Overloading Unary Operator
#include <iostream>
using namespace std;
class temp
{
private:
int count;
public:
temp():count(5)
{ }
void operator ++()
{
count=count+1;
}
void Display()
{
cout<<Count:<<count;
}
};
int main()
{
temp t;
++t;
/* operator function void
operator ++() is called */
t.Display();
return 0;
Output
Count: 6
Overloading Binary Operator
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the
value of Complex Numbers
a,b:";
cin>>a>>b;
}
complex
operator+(complex ob)
{ complex t;
t.a=ob.a +a;
t.b=ob.b+b;
return(t);
complex operator-(complex ob)
{
complex t;
t.a=ob.a - a;
t.b=ob.b -b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"
<<"n";
}
};
cout<<"Input Values:n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
void main()
{
clrscr();
complex
obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
Overloading Binary Operator
In overloading of binary operators the left hand operand
is used to invoke the operator function and the right hand
Rules for overloading operator
 Only existing operators can be overloaded. We cannot
create a new operator.
 Overloaded operator should contain one operand of
user-defined data type.  Overloading operators are only
for classes. We cannot overload the operator for built-in
data types.
 Overloaded operators have the same syntax as the
original operator.
 Operator overloading is applicable within the scope
(extent) in which overloading occurs.
Restrictions on Operator Overloading
 Overloading restrictions
 Arity (number of operands) cannot be changed
 Unary operators remain unary, and binary operators remain
binary
 No new operators can be created
 Use only existing operators
 No overloading operators for built-in types
 Cannot change how two integers are added
 Produces a syntax error
Restrictions on Operator Overloading
 C++ operators that can be overloaded
 C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
Thank you

More Related Content

What's hot (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
Hitesh Kumar
C++ programming function
C++ programming functionC++ programming function
C++ programming function
Vishalini Mugunen
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
class and objects
class and objectsclass and objects
class and objects
Payel Guria
Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
Mayank Jain
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
inheritance
inheritanceinheritance
inheritance
Nivetha Elangovan
Inline function
Inline functionInline function
Inline function
Tech_MX
C++ Notes
C++ NotesC++ Notes
C++ Notes
MOHAMED RIYAZUDEEN
Friend function
Friend functionFriend function
Friend function
zindadili
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
Hitesh Kumar
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
class and objects
class and objectsclass and objects
class and objects
Payel Guria
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
Mayank Jain
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
Inline function
Inline functionInline function
Inline function
Tech_MX
Friend function
Friend functionFriend function
Friend function
zindadili

Similar to operator overloading (20)

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
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
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ramya marichamy
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
Lecture5
Lecture5Lecture5
Lecture5
ravifeelings
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Garima Singh Makhija
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
University College of Engineering Kakinada, JNTUK - Kakinada, India
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTCBasics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
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
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptxObject Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
RashidFaridChishti
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
Oops
OopsOops
Oops
ankush_kumar
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 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
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
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTCBasics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
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
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptxObject Oriented Programming using C++: Ch08 Operator Overloading.pptx
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
RashidFaridChishti
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
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

Recently uploaded (20)

Account Cash Flow Statement Report Generate in odoo
Account Cash Flow Statement Report Generate in odooAccount Cash Flow Statement Report Generate in odoo
Account Cash Flow Statement Report Generate in odoo
AxisTechnolabs
Data Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - WebinarData Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - Webinar
OnePlan Solutions
Carousel - Five Key FinTech Trends for 2025
Carousel - Five Key FinTech Trends for 2025Carousel - Five Key FinTech Trends for 2025
Carousel - Five Key FinTech Trends for 2025
Anadea
Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...
Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...
Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...
Shubham Joshi
SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?
kiran10101khan
Code or No-Code Tests: Why Top Teams Choose Both
Code or No-Code Tests: Why Top Teams Choose BothCode or No-Code Tests: Why Top Teams Choose Both
Code or No-Code Tests: Why Top Teams Choose Both
Applitools
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber Scale
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber ScaleAI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber Scale
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber Scale
Alluxio, Inc.
AI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI AssistansAI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI Assistans
HusseinMalikMammadli
EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]
bhagasufyan
AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio: Preprocessing, ...
AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio:  Preprocessing, ...AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio:  Preprocessing, ...
AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio: Preprocessing, ...
Alluxio, Inc.
Hire Odoo Developer OnestopDA Experts.
Hire Odoo Developer  OnestopDA Experts.Hire Odoo Developer  OnestopDA Experts.
Hire Odoo Developer OnestopDA Experts.
OnestopDA
Wondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free DownloadWondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free Download
arshadkhokher01
LDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free DownloadLDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free Download
5ls1bnl9iv
Minitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free DownloadMinitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free Download
v3r2eptd2q
iTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free DownloadiTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free Download
lr74xqnvuf
SE- Lecture 5 SE for easy understanding.ppt
SE- Lecture 5 SE for easy understanding.pptSE- Lecture 5 SE for easy understanding.ppt
SE- Lecture 5 SE for easy understanding.ppt
theworldimagine985
SE- Lecture 5 for software development.ppt
SE- Lecture 5 for software development.pptSE- Lecture 5 for software development.ppt
SE- Lecture 5 for software development.ppt
theworldimagine985
A Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman BhaumikA Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman Bhaumik
Raman Bhaumik
OutSystems User Group Utrecht February 2025.pdf
OutSystems User Group Utrecht February 2025.pdfOutSystems User Group Utrecht February 2025.pdf
OutSystems User Group Utrecht February 2025.pdf
mail496323
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Yann-Ga谷l Gu辿h辿neuc
Account Cash Flow Statement Report Generate in odoo
Account Cash Flow Statement Report Generate in odooAccount Cash Flow Statement Report Generate in odoo
Account Cash Flow Statement Report Generate in odoo
AxisTechnolabs
Data Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - WebinarData Storytelling for Portfolio Leaders - Webinar
Data Storytelling for Portfolio Leaders - Webinar
OnePlan Solutions
Carousel - Five Key FinTech Trends for 2025
Carousel - Five Key FinTech Trends for 2025Carousel - Five Key FinTech Trends for 2025
Carousel - Five Key FinTech Trends for 2025
Anadea
Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...
Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...
Mastering Software Test Automation: A Comprehensive Guide for Beginners and E...
Shubham Joshi
SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?SketchUp Pro Crack [2025]-Free Download?
SketchUp Pro Crack [2025]-Free Download?
kiran10101khan
Code or No-Code Tests: Why Top Teams Choose Both
Code or No-Code Tests: Why Top Teams Choose BothCode or No-Code Tests: Why Top Teams Choose Both
Code or No-Code Tests: Why Top Teams Choose Both
Applitools
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber Scale
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber ScaleAI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber Scale
AI/ML Infra Meetup | Deployment, Discovery and Serving of LLMs at Uber Scale
Alluxio, Inc.
AI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI AssistansAI Agents and More:Build Your AI Assistans
AI Agents and More:Build Your AI Assistans
HusseinMalikMammadli
EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]EASEUS Partition Master Crack with License Code [Latest]
EASEUS Partition Master Crack with License Code [Latest]
bhagasufyan
AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio: Preprocessing, ...
AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio:  Preprocessing, ...AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio:  Preprocessing, ...
AI/ML Infra Meetup | Optimizing ML Data Access with Alluxio: Preprocessing, ...
Alluxio, Inc.
Hire Odoo Developer OnestopDA Experts.
Hire Odoo Developer  OnestopDA Experts.Hire Odoo Developer  OnestopDA Experts.
Hire Odoo Developer OnestopDA Experts.
OnestopDA
Wondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free DownloadWondershare Filmora 14.3.2 Crack + License Key Free Download
Wondershare Filmora 14.3.2 Crack + License Key Free Download
arshadkhokher01
LDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free DownloadLDPlayer 9.1.20 Latest Crack Free Download
LDPlayer 9.1.20 Latest Crack Free Download
5ls1bnl9iv
Minitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free DownloadMinitool Partition Wizard Crack Free Download
Minitool Partition Wizard Crack Free Download
v3r2eptd2q
iTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free DownloadiTop VPN Latest Version 2025 Crack Free Download
iTop VPN Latest Version 2025 Crack Free Download
lr74xqnvuf
SE- Lecture 5 SE for easy understanding.ppt
SE- Lecture 5 SE for easy understanding.pptSE- Lecture 5 SE for easy understanding.ppt
SE- Lecture 5 SE for easy understanding.ppt
theworldimagine985
SE- Lecture 5 for software development.ppt
SE- Lecture 5 for software development.pptSE- Lecture 5 for software development.ppt
SE- Lecture 5 for software development.ppt
theworldimagine985
A Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman BhaumikA Brief Introduction About Raman Bhaumik
A Brief Introduction About Raman Bhaumik
Raman Bhaumik
OutSystems User Group Utrecht February 2025.pdf
OutSystems User Group Utrecht February 2025.pdfOutSystems User Group Utrecht February 2025.pdf
OutSystems User Group Utrecht February 2025.pdf
mail496323
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Projects Panama, Valhalla, and Babylon: Java is the New Python v0.9
Yann-Ga谷l Gu辿h辿neuc

operator overloading

  • 1. Department of I.T. Engineering Presentation On Operator Overloading Academic year 2018-19 Laxmi Institute of Technology, Sarigam Approved by AICTE, New Delhi; Affiliated to Gujarat Technological University, Ahmedabad Enrolment number Name 160860116018 Nishant P. Joshi 160860116029 Raj M. Patel 160860116032 Udit A. Patel
  • 2. Content Introduction Operator overloading Defining operator overloading Overloading Input/output operator Overloading Unary Operator Overloading Binary Operator Rules for overloading operator Restrictions on Operator Overloading
  • 3. Introduction Overloading an operator Write function definition as normal Function name is keyword operator followed by the symbol for the operator being overloaded operator+ used to overload the addition operator (+) Using operators To use an operator on a class object it must be overloaded unless the assignment operator(=)or the address operator(&) Assignment operator by default performs memberwise assignment Address operator (&) by default returns the address of an object
  • 4. Operator overloading Addition (+) operator can work on operands of type char, int, float & double. However, if s1, s2, s3 are objects of the class string, the we can write the statement, s3 = s1 + s2; This means C++ has the ability to provide the operators with a special meaning for a data type. Mechanism of giving special meaning to an operator is known as operator overloading.
  • 5. Operator overloading Operator is a symbol that indicates an operation. Overloading assigning different meanings to an operation, depending upon the context. For example: input(>>) / output(<<) operator The built-in definition of the operator << is for shifting of bits. It is also used for displaying the values of various data types
  • 6. Defining operator overloading The general form of an operator function is: return-type class-name :: operator op (argList) { function body // task defined. } where return-type is the type of value returned by the specified operation. op is the operator being overloaded. operator op is the function name, where operator is a keyword.
  • 7. Overloading Input/output operator C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. Overloaded to perform input/output for user defined data types. Left Operand will be of types ostream & and istream &. Function overloading this operator must be a Non- Member function because left operand is not an Object of the class. It must be a friend function to access private data members.
  • 8. Overloading Input/output operator #include<iostream> using namespace std; class time { int hr,min,sec; public: time() { hr=0, min=0; sec=0; } time(int h,int m, int s) { hr=h, min=m; sec=s; } friend ostream& operator << (ostream &out, time &tm); //overloading <<' operator };
  • 9. Overloading Input/output operator ostream& operator << (ostream &out, time &tm) //operator function { out << "Time is " << tm.hr << "hour : " << tm.min<< "min : " << tm.sec << "sec"; return out; } int main() { time tm(3,15,45); cout << tm; return 0; } Output: Time is 3 hour : 15 min : 45 sec
  • 10. Overloading Unary Operator #include <iostream> using namespace std; class temp { private: int count; public: temp():count(5) { } void operator ++() { count=count+1; } void Display() { cout<<Count:<<count; } }; int main() { temp t; ++t; /* operator function void operator ++() is called */ t.Display(); return 0; Output Count: 6
  • 11. Overloading Binary Operator #include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=ob.a +a; t.b=ob.b+b; return(t); complex operator-(complex ob) { complex t; t.a=ob.a - a; t.b=ob.b -b; return(t); } void display() { cout<<a<<"+"<<b<<"i" <<"n"; } };
  • 12. cout<<"Input Values:n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); } void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue(); obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; Overloading Binary Operator In overloading of binary operators the left hand operand is used to invoke the operator function and the right hand
  • 13. Rules for overloading operator Only existing operators can be overloaded. We cannot create a new operator. Overloaded operator should contain one operand of user-defined data type. Overloading operators are only for classes. We cannot overload the operator for built-in data types. Overloaded operators have the same syntax as the original operator. Operator overloading is applicable within the scope (extent) in which overloading occurs.
  • 14. Restrictions on Operator Overloading Overloading restrictions Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary No new operators can be created Use only existing operators No overloading operators for built-in types Cannot change how two integers are added Produces a syntax error
  • 15. Restrictions on Operator Overloading C++ operators that can be overloaded C++ Operators that cannot be overloaded Operators that cannot be overloaded . .* :: ?: sizeof Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete