SlideShare a Scribd company logo
CSE 332: C++ Overloading
Overview of C++ Overloading
• Overloading occurs when the same operator or
function name is used with different signatures
• Both operators and functions can be overloaded
• Different definitions must be distinguished by their
signatures (otherwise which to call is ambiguous)
– Reminder: signature is the operator/function name and the
ordered list of its argument types
– E.g., add(int,long) and add(long,int) have different
signatures
– E.g., add(const Base &) and add(const Derived &)
have different signatures, even if Derived is-a Base
– Most specific match is used to select which one to call
CSE 332: C++ Overloading
Overloading vs. Overriding
• Overriding a base class member function is
similar to overloading a function or operator
– But for overriding, definitions are distinguished by
their scopes rather than by their signatures
• C++ can distinguish method definitions
according to either static or dynamic type
– Depends on whether a method is virtual or not
– Depends on whether called via a reference or
pointer vs. directly on an object
– Depends on whether the call states the scope
explicitly (e.g., Foo::baz();)
CSE 332: C++ Overloading
Function Overloading
class A {
public:
int add(int i, int j);
// not allowed, would be
// ambiguous with above:
// long add(int m, int n);
// Ok, different signature
long add(long m, long n);
};
int
main (int argc, char **argv) {
int a = 7;
int b = 8;
int c = add(a, b);
return 0;
}
• Calls to overloaded functions and
operators are resolved by
– Finding all possible matches based
on passed arguments
• May involve type promotion
• May involve instantiating templates
– Finding the “best match” among
those possible matches
• Signature does not include the
return type
– Which might not help even if it did,
i.e., calls may ignore result
– So, overloading can’t be resolved by
return type alone
– Compiler generates an error if the
call can’t be resolved
CSE 332: C++ Overloading
Operator Overloading
class A {
friend ostream &operator<<
(ostream &, const A &);
private:
int m_;
};
ostream &operator<<
(ostream &out, const A &a) {
out << "A::m_ = " << a.m_;
return out;
}
int main () {
A a;
cout << a << endl;
return 0;
}
• Similar to function
overloading
– Resolved by signature
– Best match is used
• But the list of operators and
the “arity” of each is fixed
– Can’t invent operators (e.g.,
like ** for exponentiation )
– Must use same number of
arguments as for built-in types
(except for operator())
– Some operators are off limits
:: (scope) ?: (conditional)
.* (member dereference)
. (member) sizeof
typeid (RTTI)
CSE 332: C++ Overloading
Operator Symmetry, Precedence
class Complex {
public:
// Constructor
Complex (double r, double i);
friend Complex operator*
(const Complex &, const Complex &);
// but not friend Complex operator^
// (const Complex &, const Complex &);
private:
int real_;
int imaginary_;
};
// multiplication works just fine
Complex operator* (const Complex &,
const Complex &);
// exponentiation operator unworkable
// Complex operator^ (const Complex &,
// const Complex &);
• Make arithmetic
operators symmetric
– As non-member friends
– Return result by value
– Don’t mix base and
derived types in their
parameter lists
• Operators for user-
defined types obey the
same precedence rules
as for built-in types
– Can lead to some
unexpected mistakes
– E.g., if uncommented
exponentiation for
a + b * c ^ 2
CSE 332: C++ Overloading
Member vs. Non-Member Overloading
// declarations in .h file
class A {
public:
friend bool operator<
(const A &, const A &);
A operator++(); // prefix
A operator++(int); // postfix
private:
int m_;
};
bool operator==(const A &lhs,
const A &rhs);
// definitions in .cpp file
bool operator==(const A &lhs,
const A &rhs)
{return lhs.m_ == rhs.m_;}
A A::operator++() // prefix
{++m_; return *this;}
A A::operator++(int) // postfix
{A ret(*this); ++*this; return ret;}
• Remember a this pointer is passed
to any non-static member function
– Object doesn’t appear in parameters
– For non-member functions and
operators all parameters are listed
• The rules about operator arity are
obeyed in code on left
– Operator == is binary
– Prefix/postfix ++ are unary, parameter
for postfix distinguishes its signature
• Must declare and define [] and ==
and -> and () as member operators
• Non-member operators are needed
when working with classes you wrote
and classes you didn’t write
– E.g., ostream << and istream >>
• Non-member operators are also
useful to preserve symmetry
– E.g., for arithmetic/relational operators
– May help to avoid unexpected type
conversions, especially with an
inheritance hierarchy
CSE 332: C++ Overloading
Summary: Tips on Overloading
• Use virtual overriding when you want to substitute
different subtypes polymorphically
– E.g., move() in derived and base classes
• Use overloading when you want to provide related
interfaces to similar abstractions
– E.g., migrate(Bird &) vs. migrate(Elephant &)
– Make[] -> () = *= ++ -- etc. members
– Make << >> + * - / == < etc. non-members
• Use different names when the abstractions differ
– E.g., fly() versus walk()

More Related Content

Similar to C++_overloading.ppt

overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
Shanmuganathan C
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
C++ language
C++ languageC++ language
C++ language
Hamza Asif
 
operator overloading
operator overloadingoperator overloading
operator overloading
Nishant Joshi
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
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
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Function Overloading.ppt
Function Overloading.pptFunction Overloading.ppt
Function Overloading.ppt
nivedita murugan
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
PowerfullBoy1
 
Functions
FunctionsFunctions
C++.pptx
C++.pptxC++.pptx
C++.pptx
Sabi995708
 

Similar to C++_overloading.ppt (20)

overloading in C++
overloading in C++overloading in C++
overloading in C++
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Oops
OopsOops
Oops
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
C++ language
C++ languageC++ language
C++ language
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
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
 
Function Overloading.ppt
Function Overloading.pptFunction Overloading.ppt
Function Overloading.ppt
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Lecture5
Lecture5Lecture5
Lecture5
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
Functions
FunctionsFunctions
Functions
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 

Recently uploaded

Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 

Recently uploaded (20)

Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 

C++_overloading.ppt

  • 1. CSE 332: C++ Overloading Overview of C++ Overloading • Overloading occurs when the same operator or function name is used with different signatures • Both operators and functions can be overloaded • Different definitions must be distinguished by their signatures (otherwise which to call is ambiguous) – Reminder: signature is the operator/function name and the ordered list of its argument types – E.g., add(int,long) and add(long,int) have different signatures – E.g., add(const Base &) and add(const Derived &) have different signatures, even if Derived is-a Base – Most specific match is used to select which one to call
  • 2. CSE 332: C++ Overloading Overloading vs. Overriding • Overriding a base class member function is similar to overloading a function or operator – But for overriding, definitions are distinguished by their scopes rather than by their signatures • C++ can distinguish method definitions according to either static or dynamic type – Depends on whether a method is virtual or not – Depends on whether called via a reference or pointer vs. directly on an object – Depends on whether the call states the scope explicitly (e.g., Foo::baz();)
  • 3. CSE 332: C++ Overloading Function Overloading class A { public: int add(int i, int j); // not allowed, would be // ambiguous with above: // long add(int m, int n); // Ok, different signature long add(long m, long n); }; int main (int argc, char **argv) { int a = 7; int b = 8; int c = add(a, b); return 0; } • Calls to overloaded functions and operators are resolved by – Finding all possible matches based on passed arguments • May involve type promotion • May involve instantiating templates – Finding the “best match” among those possible matches • Signature does not include the return type – Which might not help even if it did, i.e., calls may ignore result – So, overloading can’t be resolved by return type alone – Compiler generates an error if the call can’t be resolved
  • 4. CSE 332: C++ Overloading Operator Overloading class A { friend ostream &operator<< (ostream &, const A &); private: int m_; }; ostream &operator<< (ostream &out, const A &a) { out << "A::m_ = " << a.m_; return out; } int main () { A a; cout << a << endl; return 0; } • Similar to function overloading – Resolved by signature – Best match is used • But the list of operators and the “arity” of each is fixed – Can’t invent operators (e.g., like ** for exponentiation ) – Must use same number of arguments as for built-in types (except for operator()) – Some operators are off limits :: (scope) ?: (conditional) .* (member dereference) . (member) sizeof typeid (RTTI)
  • 5. CSE 332: C++ Overloading Operator Symmetry, Precedence class Complex { public: // Constructor Complex (double r, double i); friend Complex operator* (const Complex &, const Complex &); // but not friend Complex operator^ // (const Complex &, const Complex &); private: int real_; int imaginary_; }; // multiplication works just fine Complex operator* (const Complex &, const Complex &); // exponentiation operator unworkable // Complex operator^ (const Complex &, // const Complex &); • Make arithmetic operators symmetric – As non-member friends – Return result by value – Don’t mix base and derived types in their parameter lists • Operators for user- defined types obey the same precedence rules as for built-in types – Can lead to some unexpected mistakes – E.g., if uncommented exponentiation for a + b * c ^ 2
  • 6. CSE 332: C++ Overloading Member vs. Non-Member Overloading // declarations in .h file class A { public: friend bool operator< (const A &, const A &); A operator++(); // prefix A operator++(int); // postfix private: int m_; }; bool operator==(const A &lhs, const A &rhs); // definitions in .cpp file bool operator==(const A &lhs, const A &rhs) {return lhs.m_ == rhs.m_;} A A::operator++() // prefix {++m_; return *this;} A A::operator++(int) // postfix {A ret(*this); ++*this; return ret;} • Remember a this pointer is passed to any non-static member function – Object doesn’t appear in parameters – For non-member functions and operators all parameters are listed • The rules about operator arity are obeyed in code on left – Operator == is binary – Prefix/postfix ++ are unary, parameter for postfix distinguishes its signature • Must declare and define [] and == and -> and () as member operators • Non-member operators are needed when working with classes you wrote and classes you didn’t write – E.g., ostream << and istream >> • Non-member operators are also useful to preserve symmetry – E.g., for arithmetic/relational operators – May help to avoid unexpected type conversions, especially with an inheritance hierarchy
  • 7. CSE 332: C++ Overloading Summary: Tips on Overloading • Use virtual overriding when you want to substitute different subtypes polymorphically – E.g., move() in derived and base classes • Use overloading when you want to provide related interfaces to similar abstractions – E.g., migrate(Bird &) vs. migrate(Elephant &) – Make[] -> () = *= ++ -- etc. members – Make << >> + * - / == < etc. non-members • Use different names when the abstractions differ – E.g., fly() versus walk()