CS 162 Intro to Programming II Operator Overloading 1 - - PowerPoint PPT Presentation

cs 162 intro to programming ii
SMART_READER_LITE
LIVE PREVIEW

CS 162 Intro to Programming II Operator Overloading 1 - - PowerPoint PPT Presentation

CS 162 Intro to Programming II Operator Overloading 1 11.6 Operator Overloading Operators such as = , + , and others can be redefined for use with objects of a class The name of the function for


slide-1
SLIDE 1

CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡

Operator ¡Overloading ¡

1 ¡

slide-2
SLIDE 2

11.6 Operator Overloading

  • Operators such as =, +, and others can be

redefined for use with objects of a class

  • The name of the function for the overloaded
  • perator is operator followed by the
  • perator symbol, e.g.,
  • perator+ is the overloaded + operator and
  • perator= is the overloaded = operator

11-2

slide-3
SLIDE 3

Operator Overloading

  • Operators can be overloaded as
  • instance member functions, or as
  • friend functions
  • The overloaded operator must have the

same number of parameters as the standard version. For example,

  • perator= must have two parameters,

since the standard = operator takes two parameters.

11-3

slide-4
SLIDE 4

Overloading Operators as Instance Members

A binary operator that is overloaded as an instance member needs only one parameter, which represents the operand on the right:

class OpClass { private: int x; public: OpClass operator+(OpClass right); };

11-4

slide-5
SLIDE 5

Overloading Operators as Instance Members

  • The left operand of the overloaded binary
  • perator is the calling object
  • The implicit left parameter is accessed

through the this pointer

OpClass OpClass::operator+(OpClass r) { OpClass sum; sum.x = this->x + r.x; return sum; }

11-5

slide-6
SLIDE 6

Invoking an Overloaded Operator

  • Operator can be invoked as a member

function: OpClass a, b, s; s = a.operator+(b);

  • It can also be invoked in the more

conventional manner: OpClass a, b, s; s = a + b;

11-6

slide-7
SLIDE 7

Overloading Assignment

  • Overloading the assignment operator solves

problems with object assignment when an

  • bject contains pointer to dynamic memory.
  • Assignment operator is most naturally
  • verloaded as an instance member function
  • It needs to return a value of the assigned
  • bject to allow cascaded assignments such

as

a = b = c;

11-7

slide-8
SLIDE 8

Overloading Assignment

Assignment overloaded as a member function:

class CpClass

{ int *p; public: CpClass(int v=0) { p = new int; *p = v; ~CpClass(){delete p;} CpClass operator=(CpClass); };

11-8

slide-9
SLIDE 9

Overloading Assignment

Implementation returns a value:

CpClass CpClass::operator=(CpClass r)

{ *p = *r.p; return *this; };

Invoking the assignment operator:

CpClass a, x(45);

a.operator=(x); // either of these a = x; // lines can be used

11-9

slide-10
SLIDE 10

Notes on Overloaded Operators

  • Overloading can change the entire

meaning of an operator

  • Most operators can be overloaded
  • Cannot change the number of operands of

the operator

  • Cannot overload the following operators:

?: . .* sizeof

11-10

slide-11
SLIDE 11

Overloading Types of Operators

  • ++, -- operators overloaded differently

for prefix vs. postfix notation

  • Overloaded relational operators should

return a bool value

  • Overloaded stream operators >>, <<

must return istream, ostream objects and take istream, ostream objects as parameters

11-11

slide-12
SLIDE 12

Overloaded [] Operator

  • Can be used to create classes that

behave like arrays, providing bounds- checking on subscripts

  • Overloaded [] returns a reference to
  • bject, not an object itself

11-12