33
http://cs.mst.edu Overloading Operators

Overloading Operators

  • Upload
    sanam

  • View
    20

  • Download
    0

Embed Size (px)

DESCRIPTION

Overloading Operators. Operators. Operators are functions, but with a different kind of name – a symbol. Functions have parameters to which you pass arguments; operators have parameters to which you pass operands. Operands = Arguments. The name of a operator is “operator ” - PowerPoint PPT Presentation

Citation preview

Page 1: Overloading Operators

http://cs.mst.edu

Overloading Operators

Page 2: Overloading Operators

http://cs.mst.edu

Operators Operators are functions, but with a different kind

of name – a symbol. Functions have parameters to which you pass

arguments; operators have parameters to which you pass operands. Operands = Arguments.

The name of a operator is “operator <symbol>” Operators are called differently from functions.

Page 3: Overloading Operators

http://cs.mst.edu

Operators Operators are functions, but with a different kind of

name – a symbol. Functions have parameters to which you pass

arguments; operators have parameters to which you pass operands. Operands = Arguments.

The name of a operator is “operator <symbol>” Operators are called differently from functions. We say “operator overloading” because all operators

are already defined – you cannot create new operators.

Page 4: Overloading Operators

http://cs.mst.edu

Operators Operators are functions, but with a different kind of

name – a symbol. Functions have parameters to which you pass

arguments; operators have parameters to which you pass operands. Operands = Arguments.

The name of a operator is “operator <symbol>” Operators are called differently from functions. We say “operator overloading” because all operators are

already defined – you cannot create new operators. An operator can be defined either as a member function of a

class or a non-member function.

Page 5: Overloading Operators

http://cs.mst.edu

Operator Calls Example: var1 * var2 (a binary operator)

Page 6: Overloading Operators

http://cs.mst.edu

Operator Calls Example: var1 * var2 (a binary operator)

var1 is the lhs operand, var2 is the rhs operand. If operator * () is a non-member function, then var1

and var2 are arguments passed to fill two parameters. If operator * () is a member function, then var1 is the

calling object and var2 is passed to a parameter

Page 7: Overloading Operators

http://cs.mst.edu

Operator Calls Example: ~var1 (a unary operator)

var1 is the only operand. If operator ~ () is a non-member function, then var1 is

the argument passed to fill a single parameter. If operator ~ () is a member function, then var1 is the

calling object.

Page 8: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and <<

cout << var; cin >> var;

Page 9: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and << arithmetic: +, -, *, and /

var1 + var2 var1 - var2 var1 * var2 var1 / var2

Page 10: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: =

var1 = var2; var1 = var2 + var3;

Page 11: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /=

var1 += var2; var1 -= var2; var1 *= var2; var1 /= var2;

Page 12: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= brackets and parenthesis: [] and ()

object[index_pos] = var; object(var);

Page 13: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= brackets and parenthesis: [] and () and many others!

Page 14: Overloading Operators

http://cs.mst.edu

Operators insertion and extraction: >> and << arithmetic: +, -, *, and / assignments: = arithmetic assignments: +=, -=, *=, and /= brackets and parenthesis: [] and () and many others! FORBIDDEN OVERLOADS: :: . .* sizeof ?:

Page 15: Overloading Operators

http://cs.mst.edu

Additional Rules You cannot create new operators.

Page 16: Overloading Operators

http://cs.mst.edu

Additional Rules You cannot create new operators. If you overload an operator, at least one of the

parameters must be a user-defined type.

Page 17: Overloading Operators

http://cs.mst.edu

Additional Rules You cannot create new operators. If you overload an operator, at least one of the

parameters must be a user-defined type. You cannot change the arity of a operator.

Page 18: Overloading Operators

http://cs.mst.edu

Additional Rules You cannot create new operators. If you overload an operator, at least one of the

parameters must be a user-defined type. You cannot change the arity of a operator. You cannot change the order of precedence of

operators by overloading them.

Page 19: Overloading Operators

http://cs.mst.edu

Additional Rules You cannot create new operators. If you overload an operator, at least one of the

parameters must be a user-defined type. You cannot change the arity of a operator. You cannot change the order of precedence of

operators by overloading them. Certain operators must be overloaded as class

members. They are = [] () ->

Page 20: Overloading Operators

http://cs.mst.edu

Additional Rules You cannot create new operators. If you overload an operator, at least one of the

parameters must be a user-defined type. You cannot change the arity of a operator. You cannot change the order of precedence of operators

by overloading them. Certain operators must be overloaded as class members.

They are = [] () -> An overloaded operator cannot have default arguments.

Page 21: Overloading Operators

http://cs.mst.edu

Rules of Thumb Make your definitions of an overloaded operator

make sense. As an example, you would not want to define ‘+’ for complex numbers as subtraction.

Page 22: Overloading Operators

http://cs.mst.edu

Rules of Thumb Make your definitions of an overloaded operator

make sense. As an example, you would not want to define ‘+’ for complex numbers as subtraction.

Define an operator overload as a member function if it modifies the calling object; as a nonmember function if it doesn’t.

Page 23: Overloading Operators

http://cs.mst.edu

Rules of Thumb Make your definitions of an overloaded operator

make sense. As an example, you would not want to define ‘+’ for complex numbers as subtraction.

Define an operator overload as a member function if it modifies the calling object; as a nonmember function if it doesn’t.

Define symmetric operator pairs in terms of one another. As an example, define != using the == operator you already defined.

Page 24: Overloading Operators

http://cs.mst.edu

Recall

//fraction.cpp...Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs){ Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; temp.m_Denominator = lhs.m_Denominator * rhs.m_Denominator; return temp;}

//fraction.h...class Fraction{ ... friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); ...};

Page 25: Overloading Operators

http://cs.mst.edu

Revised!

//fraction.cpp...Fraction operator* (const Fraction & lhs, const Fraction & rhs){ Fraction result(lhs); return (result*=rhs);}

//fraction.h...class Fraction{ ... friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); ...};

Page 26: Overloading Operators

http://cs.mst.edu

Revised!

//fraction.cpp...Fraction operator* (const Fraction & lhs, const Fraction & rhs){ Fraction result(lhs); return (result*=rhs);}

//fraction.h...class Fraction{ ... friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); ...};

needs to be defined

Page 27: Overloading Operators

http://cs.mst.edu

Revised!

//fraction.cpp...Fraction& Fraction::operator*= (const Fraction & rhs){   m_Numerator*=rhs.m_Numerator;   m_Denominator*=rhs.m_Denominator;    return (*this);}

//fraction.h...class Fraction{ ... friend Fraction operator* (const Fraction & lhs, const Fraction & rhs); Fraction& operator*= (const Fraction & rhs); ...};

Page 28: Overloading Operators

http://cs.mst.edu

*this

x 5int pointer

int

Page 29: Overloading Operators

http://cs.mst.edu

*this

x 5int pointer

int

x refers to the pointer

Page 30: Overloading Operators

http://cs.mst.edu

*this

x 5int pointer

int

*x refers to the int being pointed at

Page 31: Overloading Operators

http://cs.mst.edu

*thisthis

Fraction& Fraction::operator*= (const Fraction & rhs){   m_Numerator*=rhs.m_Numerator;   m_Denominator*=rhs.m_Denominator;    return (*this);}

Fraction pointer

Fraction

this refers to the pointer that is keeping track of your

Fraction object’s place in memory – pointing to the

calling object of this function

Page 32: Overloading Operators

http://cs.mst.edu

*thisthis

Fraction& Fraction::operator*= (const Fraction & rhs){   m_Numerator*=rhs.m_Numerator;   m_Denominator*=rhs.m_Denominator;    return (*this);}

Fraction pointer

Fraction

*this refers to the fraction object currently executing your call to *=

the calling object

Page 33: Overloading Operators

http://cs.mst.edu

End of Session