ece 462 object-oriented programming using c++ and java ... · yhl operator overloading 4 binary...

46
YHL Operator Overloading 1 ECE 462 Object-Oriented Programming using C++ and Java Operator Overloading in C++ Yung-Hsiang Lu [email protected]

Upload: others

Post on 02-Jun-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 1

ECE 462Object-Oriented Programming

using C++ and Java

Operator Overloading in C++

Yung-Hsiang [email protected]

Page 2: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 2

Operator Overloading in C++

You have been using overloaded operators for years.• 3 + 5 // integer addition (bit-wise + carries)• 3.5 + 0.0059 // floating point addition

// 1. align the decimal points// 2. add significands// 3. normalized and update exponent

• “hello” + “ world” // intuitively, it means append• Operator overloading is not essential in object-oriented

programming. In fact, Java does not allow programmer-defined operator overloading.

Page 3: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 3

Overloading Operators in C++

• We have seen overloaded operator for << >> and =• at least one operand (for binary operators) must be an

object or enumeration type (i.e. not a built-in type)• precedence not changed• arity not changed (! always unary)• argument(s) may be passed by value (copy) or by

reference, not by pointer• default argument value(s) illegal• cannot overload :: .* . ?:

Page 4: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 4

Binary Operators

• If the first operand is an object, a binary operator can be implemented in two forms– a member function, the first operand is this object– a “free” function (not a member of any class), usually

declared as a friend to access private attributes – not both

• If the first operand is not an object (such as int), the operator must be a free function.

• In most cases, the operand object(s) should use reference to prevent calling copy constructor.

Page 5: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 5

Global Function(not a member function of a class)

Page 6: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 6

Page 7: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 7

Page 8: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 8

Copy Constructor and Operator Overloading

Page 9: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 9

Page 10: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 10

Page 11: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 11

Member Function(first operand is "this")

Page 12: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 12

Page 13: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 13

binary operator withonly one parameter

Page 14: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 14

Page 15: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 15

Page 16: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 16

Binary Operator, One Input?

first + second;

can be thought of asfirst.operator + (second);

This operator is a member function. Hence, the first operand is given by the first object.

cout << first;

can be thought of ascout.operator << (first);

Page 17: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 17

Unary Operator, Global Function

Page 18: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 18

unary operator withonly one parameter

Page 19: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 19

Unary Operator, Member Function

Page 20: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 20

unary operator without parameter ("this" is the parameter)

Page 21: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 21

Comparison Operator <

Page 22: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 22

Page 23: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 23

Equality Operator ==

Page 24: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 24

Page 25: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Operator Overloading 25

Self Test

Page 26: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 1

ECE 462Object-Oriented Programming

using C++ and Java

Small Int and Conversion

Yung-Hsiang [email protected]

Page 27: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 2

Increment and Decrement Operators (Small Int)

Page 28: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 3

//prefix increment operator, such as ++iinline SmallInt& SmallInt::operator++() {

if (value < MAX) { value ++; } else { cerr << "Error: Range of SmallInt violated" << endl; }return *this;

}

//postfix increment operator, , such as i++inline const SmallInt SmallInt::operator++(int) {

SmallInt oldValue = *this;++(*this); // call the prefix increment operatorreturn oldValue;

}

Page 29: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 4

prefix and postfix increment

void f1(int i) {cout << i << endl;

}int x = 5;f1(++x); // increment before calling f1, outupt 6cout << x << endl; // 6f1(x ++); // increment after calling f1, output 6cout << x << endl; // 7

Page 30: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 5

inline

• inline ⇒ suggest C++ compiler to replace the function call by the function body to reduce the call overhead

• function call overhead– push the current location to the call stack– push the parameters' values to the call stack– change the program counter to the called function's address– execute the code in the function // useful work– pop the stack to get the return address– change the program counter to the return address

• inline replaces the call by the code inside the function

Page 31: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 6

Page 32: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 7

Page 33: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 8

Page 34: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 9

Page 35: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 10

Page 36: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 11

Page 37: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 12

Page 38: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 13

Page 39: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 14

Programmer Defined(Implicit) Class Conversion

Page 40: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 15

Page 41: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 16

Page 42: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 17

Explicit in C++

Page 43: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 18

Page 44: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 19

Page 45: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 20

Page 46: ECE 462 Object-Oriented Programming using C++ and Java ... · YHL Operator Overloading 4 Binary Operators • If the first operand is an object, a binary operator can be implemented

YHL Small Int 21

Self Test