SlideShare a Scribd company logo
1 of 8
C++ Operators
Jim Fawcett
Copyright © 2002-2006
CSE687 – Object Oriented Design Class Notes
 A C++ operator is really just a function. Assignment, for example, may
be invoked either way shown below:
x = y;
or
x.operator=(y);
Here, the x object is invoking the assignment operator on itself, using y for
the assigned values.
 The left hand operand is always the invoking object and the right hand
operand is always passed to the function as an argument.
 General form of the binary operator:
x@y  x.operator@(y) - member function
x@y  operator@(x,y) - global function
C++ Binary Operator Model
Indexing Operators
 Indexing operators should usually come in pairs:
val& X::operator[](int n); x[3] = ‘a’;
val X::operator[](int n) const; char ch = x[2];
 The second form allows you to pass an indexed object into a
function by const reference and still be able to read indexed
values.
With only the first form, any indexing in the function will result
in a compile time error since the operator does not guarantee
not to change the const object.
Unary Increment/Decrement Operators
This example based on iterators pointing to contiguous memory
iterator& operator++()
{ /* ++(this->ptr); return *this */ }
iterator operator++(int)
{ /* iterator temp = *this, ++(this->ptr), return temp */ }
iterator& operator--()
{ /* --(this->ptr); return *this */ }
iterator operator--(int)
{ /* iterator temp = *this; --(this->ptr), return temp */ }
Sum Operators
 Arithmetic operators should come in pairs. Addition looks like this:
X& X::operator+=(const X &x);
X X::operator+(const X &x);
Addition should be implemented this way:
X X::operator+(const X &x) {
X temp = *this; // copy of me
temp += x; // copy of me + x
return temp;
}
 You implement operator+=(…) first, and get operator+(…) almost
for free.
Overloading Arithmetic Operators
 Define:
operator+, operator-, operator*, and operator/
in terms of :
operator+=, operator-=, operator*=, and operator/=
 Remember the binary operator model:
operators as class members: x@y  x.operator@(y)
operators as global functions: x@Y  operator(x,y)
Insertion
 The insertion and extraction operators:
ostream& operator(ostream& out, const X &x);
istream& operator(ostream& in, const X &x);
Have to be implemented as global (non-member) functions since they
are invoked with the statements:
out << x; and in >> x;
 Since the streams, out and in, appear on the left side of the operator,
and are not objects of the X class, we must use the global form shown
at the top of this slide.
 You should try to implement them without making them friends of the
X class. You may need to implement public helper functions to do that.
End of Presentation

More Related Content

Similar to CppOperators.ppt

08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
Youth For Peace
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
yatinnarula
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
abhay singh
 

Similar to CppOperators.ppt (20)

08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
 
B.sc CSIT 2nd semester C++ Unit2
B.sc CSIT  2nd semester C++ Unit2B.sc CSIT  2nd semester C++ Unit2
B.sc CSIT 2nd semester C++ Unit2
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Lecture5
Lecture5Lecture5
Lecture5
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
PYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptxPYTHON-PROGRAMMING-UNIT-II (1).pptx
PYTHON-PROGRAMMING-UNIT-II (1).pptx
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Array Cont
Array ContArray Cont
Array Cont
 
The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Types of Operators in C programming .pdf
Types of Operators in C programming  .pdfTypes of Operators in C programming  .pdf
Types of Operators in C programming .pdf
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

CppOperators.ppt

  • 1. C++ Operators Jim Fawcett Copyright © 2002-2006 CSE687 – Object Oriented Design Class Notes
  • 2.  A C++ operator is really just a function. Assignment, for example, may be invoked either way shown below: x = y; or x.operator=(y); Here, the x object is invoking the assignment operator on itself, using y for the assigned values.  The left hand operand is always the invoking object and the right hand operand is always passed to the function as an argument.  General form of the binary operator: x@y  x.operator@(y) - member function x@y  operator@(x,y) - global function C++ Binary Operator Model
  • 3. Indexing Operators  Indexing operators should usually come in pairs: val& X::operator[](int n); x[3] = ‘a’; val X::operator[](int n) const; char ch = x[2];  The second form allows you to pass an indexed object into a function by const reference and still be able to read indexed values. With only the first form, any indexing in the function will result in a compile time error since the operator does not guarantee not to change the const object.
  • 4. Unary Increment/Decrement Operators This example based on iterators pointing to contiguous memory iterator& operator++() { /* ++(this->ptr); return *this */ } iterator operator++(int) { /* iterator temp = *this, ++(this->ptr), return temp */ } iterator& operator--() { /* --(this->ptr); return *this */ } iterator operator--(int) { /* iterator temp = *this; --(this->ptr), return temp */ }
  • 5. Sum Operators  Arithmetic operators should come in pairs. Addition looks like this: X& X::operator+=(const X &x); X X::operator+(const X &x); Addition should be implemented this way: X X::operator+(const X &x) { X temp = *this; // copy of me temp += x; // copy of me + x return temp; }  You implement operator+=(…) first, and get operator+(…) almost for free.
  • 6. Overloading Arithmetic Operators  Define: operator+, operator-, operator*, and operator/ in terms of : operator+=, operator-=, operator*=, and operator/=  Remember the binary operator model: operators as class members: x@y  x.operator@(y) operators as global functions: x@Y  operator(x,y)
  • 7. Insertion  The insertion and extraction operators: ostream& operator(ostream& out, const X &x); istream& operator(ostream& in, const X &x); Have to be implemented as global (non-member) functions since they are invoked with the statements: out << x; and in >> x;  Since the streams, out and in, appear on the left side of the operator, and are not objects of the X class, we must use the global form shown at the top of this slide.  You should try to implement them without making them friends of the X class. You may need to implement public helper functions to do that.