Object Oriented Programming COP3330 / CGS5409 Arithmetic Operator - - PowerPoint PPT Presentation

object oriented programming cop3330 cgs5409 arithmetic
SMART_READER_LITE
LIVE PREVIEW

Object Oriented Programming COP3330 / CGS5409 Arithmetic Operator - - PowerPoint PPT Presentation

Object Oriented Programming COP3330 / CGS5409 Arithmetic Operator Overloading Increment (++) / Decrement (--) In-class exercise There are many operators available that work on built-in types, like int and double. Operator


slide-1
SLIDE 1

Object Oriented Programming COP3330 / CGS5409

slide-2
SLIDE 2

 Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise

slide-3
SLIDE 3

 There are many operators available that work on

built-in types, like int and double.

 Operator overloading

Operator overloading -- is the creation of new versions of these operators for use with user- defined types.

 An operator

  • perator in C++ is just a function

function that is called with special notation (usually more intuitive or familiar notation). Overloading an

  • perator simply involves writing a function.

 Operator overloading is done for the purpose of

using familiar operator notation on programmer- programmer- defined defined types (classes).

slide-4
SLIDE 4

 Overloading an operator cannot change its

precedence.

 Overloading an operator cannot change its

associativity.

 It is not possible to create new operators --

  • nly new versions of existing ones.

 Operator meaning on the built-in types

cannot be changed.

slide-5
SLIDE 5

 Some operators can be written as member functions of a

class

 Some operators can be written as stand-alone functions --

it's common to use friend on these

 Some operators can be written either way (your choice)  A binary operator has two operands

  • Written as a stand-alone function, both operands would be sent

as parameters, so it would be a function with two parameters

  • Written as a member function, the first operand would be the

calling object, and the other would be sent as a parameter (i.e. a function with one parameter)

 A unary operator has one operand

  • As a stand-alone function, the operand is sent as a parameter
  • As a member function, one calling object, no parameters
slide-6
SLIDE 6

 An operator is just a function. This means that it

must be created with a return type, a name, and a parameter list The rules above give some restrictions on the parameter list

 The name of an operator is always a conjunction

  • f the keyword operator and the operator symbol

itself.

 Examples:

  • perator+
  • perator++
  • perator<<
  • perator==
slide-7
SLIDE 7

 So the format of an operator overload

declaration is just like that of a function, with the keyword operator as part of the name: returnType operatorOperatorSymbol (parameterList);

slide-8
SLIDE 8

 Consider the arithmetic operators.

int x = 3, y = 6, z; float a = 3.4, b = 2.1, c; z = x + y; c = a / b;

 Now consider this notation.

1/2 + 1/3 // evaluates to 5/6 2/3 - 1/3 // evaluates to 1/3

slide-9
SLIDE 9

 Now, what about using arithmetic operators on our

Fraction class (a user-defined type): Fraction n1, n2, n3; n3 = n1 + n2; // will the compiler accept this?

 It should be clear that this would not

not make sense to the compiler. Fraction is a programmer-defined type. How would the computer know about common denominators, etc?

 These code statements would be nice to use,

however, because it's the same way we use other numeric types (like int and double).

 Operator overloading makes this possible

slide-10
SLIDE 10

 The arithmetic operators can be overloaded

either as:

 stand-alone functions or  member functions.

slide-11
SLIDE 11

 To add objects, we could write a function

called Add, of course. Recall this example: friend Fraction Add(Fraction f1, Fraction f2);

 With this function prototype, a sample call

would be: Fraction n1, n2, n3; n3 = Add(n1, n2);

slide-12
SLIDE 12

 The + notation would certainly be more convenient.  The operator version just has a different name:

friend Fraction operator+(Fraction f1, Fraction f2);

 The usual function style call would look like this:

n3 = operator+(n1, n2);

 While this is legal, the advantage is being able to use

the more common infix notation: n3 = n1 + n2; // this becomes legal

slide-13
SLIDE 13

Here's a full definition of the + operator for class Fraction. Note that this is also a possible definition for the Add function: Fraction operator+(Fraction f1, Fraction f2) { Fraction r; // declare a Fraction for result // load result Fraction with sum numerators r.numerator = (f1.numerator*f2.denominator) + (f2.numerator*f1.denominator); // load result with the common denominator r.denominator = f1.denominator * f2.denominator; return r; // return the result Fraction }

slide-14
SLIDE 14

 Once this operator overload is defined, then the

following is is legal.

 Note that cascading also works (because the

  • peration returns a Fraction). Now we have the

standard intuitive use of + Fraction n1, n2, n3, n4, n5; n5 = n1 + n2 + n3 + n4; // now it is legal!

 Note: This function could also be written with const

reference parameters friend Fraction operator+(const Fraction& f1, const Fraction& f2);

slide-15
SLIDE 15

 One member function version of Add was:

Fraction Add(const Fraction& f) const;

 A sample call to this function:

Fraction n1, n2, n3; n3 = n1.Add(n2);

slide-16
SLIDE 16

 The corresponding operator overload. Again, we change

the name to operator+ Fraction operator+(const Fraction& f) const;

 Again, we could use typical function notation, and the dot-

  • perator:

n3 = n1.operator+(n2);

 But the whole point is to be able to use the more familiar

notation, which still works, and no dot-operator required: n3 = n1 + n2; // n1 is calling object, n2 is argument

slide-17
SLIDE 17

 A full definition of this version of operator+

Fraction Fraction::operator+(const Fraction& f2) const { Fraction r; // result r.numerator = (numerator * f2.denominator) + (f2.numerator * denominator); r.denominator = (denominator * f2.denominator); return r; }

slide-18
SLIDE 18

 There are two

two versions of ++, pre-increment and post-increment

 The pre-increment operator: Has one operand, which

can be the calling object (member function) or a parameter (for non-member function implementation)

 Performs the increment, then returns the newly

incremented object.

 The post-increment operator: Has one operand,

  • fficially. But to distinguish it from the pre-

increment, an extra dummy parameter (int) is included.

 Performs the increment, but returns a copy of the

  • bject's previous value.
slide-19
SLIDE 19

 operator+= It is a binary operator. In this example,

it's written as a member function, so it has one parameter.

 The second parameter is "added" to the calling

  • bject. Note that this is a shortcut assignment, so the

left side (the calling object) is is changed. The right side (the parameter) is not not changed -- hence the parameter is passed as a const reference

 Note that the function returns *this. The keyword this

is a pointer to the calling object (from inside a member funtion). So *this is the target -- i.e. the calling object itself

 Note that the return from this operator is an IntPair

  • bject, returned by reference

by reference.

slide-20
SLIDE 20

 IntPair class  IntPair v2  Version 2 uses the +=

slide-21
SLIDE 21