+ All Categories
Home > Documents > De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical...

De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical...

Date post: 19-Apr-2020
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
34
Definitions Rules Basics Methods of operator overload Basic examples Member Non-member and non-member friend When to use each Overview of operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Overloading operators Comp Sci 1570 Introduction to C++
Transcript
Page 1: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Overloading operators

Comp Sci 1570 Introduction to C++

Page 2: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 3: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Overloading operators

• Function overloading provides a mechanism to create andresolve function calls to multiple functions with the samename, so long as each function has a unique functionprototype.

• This allows you to create variations of a function to workwith different data types, without having to think up aunique name for each variant.

• In C++, operators are implemented as functions.• By using function overloading on the operator functions,

you can define your own versions of the operators thatwork with different data types (including classes thatyou’ve written).

• Using function overloading to overload operators is calledoperator overloading.

ReturnType c l a s s n a m e ( o p t i o n a l ) : : ope ra to r OperatorSymbol ( argument l i s t ){

\\ s t a t e m e n t s ;}

Page 4: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 5: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 6: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Rules for overloading operators

• You cannot create new operators.

• If you overload an operator, at least one of the parametersmust be a user-defined type. Thus, you cannot redefine anoperator for a built-in type.

• There are some operators you are prevented fromoverloading (see tables upcoming)

• You cannot change the arity (number of operandsrequired) of a operator.

• You cannot change the order of precedence or associativityof operators by overloading them.

• An overloaded operator cannot have default arguments.

Page 7: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 8: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Operators can be overloaded in several locations

• As a member function

• As a normal non-member function

• As a friend non-member function

Page 9: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 10: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Operator+

• See code implementing it all 3 ways

Page 11: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 12: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Overloading as a member function

• For example, the assignment (=), subscript ([]), functioncall (()), and member selection (− >) operators must beoverloaded as member functions

Page 13: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 14: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Overload as non-member and Non-member friend

• For example, we are not able to overload operator << ()as a member function.

• The overloaded operator must be added as a member ofthe left operand.

• In this case, the left operand is an object of typestd::ostream. std::ostream is fixed as part of the standardlibrary. We don’t modify the class declaration to add theoverload as a member function of std::ostream.

Page 15: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 16: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

When to use normal, friend, or member overload

• When dealing with binary operators that don’t modify theleft operand (e.g. operator+), the normal or friendfunction version is typically preferred, because it works forall parameter types (even when the left operand isn’t aclass object, or is a class that is not modifiable). Thenormal or friend function version has the added benefit of“symmetry”, as all operands become explicit parameters(instead of the left operand becoming *this and the rightoperand becoming an explicit parameter).

Page 17: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

When to use normal, friend, or member overload

• When dealing with binary operators that do modify theleft operand (e.g. operator+=), the member functionversion is typically preferred. In these cases, the leftmostoperand will always be a class type, and having the objectbeing modified become the one pointed to by *this isnatural. Because the rightmost operand becomes anexplicit parameter, there’s no confusion over who isgetting modified and who is getting evaluated.

Page 18: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

When to use normal, friend, or member overload

• Unary operators are usually overloaded as memberfunctions as well, since the member version has noparameters.

Page 19: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Summary for which method

• If you’re overloading assignment (=), subscript ([]),function call (()), or member selection (− >), do so as amember function.

• If you’re overloading a unary operator, do so as a memberfunction.

• If you’re overloading a binary operator that modifies itsleft operand (e.g. operator+=), do so as a memberfunction if you can.

• If you’re overloading a binary operator that does notmodify its left operand (e.g. operator+), do so as anormal function or friend function.

• Remember: Which method and syntax you use for eachoperator depends on the particular arbitraryimplementation in C++, and requireschecking/memorizing the rules

Page 20: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 21: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 22: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Arithmetic operators

• Here, a, b, and c represent valid values (literals, valuesfrom variables, or return value), object names, or lvalues,as appropriate.

• R, S , and T , stand for any type(s), and K for a class typeor enumerated type.

Page 23: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 24: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Comparison and relational operators

Page 25: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 26: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Logical operators

Page 27: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 28: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Bitwise operators

Page 29: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 30: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Compound assignment operators

Page 31: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 32: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Member and pointer operators

Page 33: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Outline

1 Definitions

2 RulesBasics

3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each

4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther

Page 34: De nitions Rules Overloading operators...operators Arithmetic Comparison and relational Logical Bitwise Compound assignment Member and pointer Other Arithmetic operators Here, a, b,

Definitions

Rules

Basics

Methods ofoperatoroverload

Basic examples

Member

Non-memberand non-memberfriend

When to useeach

Overview ofoperators

Arithmetic

Comparison andrelational

Logical

Bitwise

Compoundassignment

Member andpointer

Other

Other operators


Recommended