YHL Operator Overloading 1
ECE 462Object-Oriented Programming
using C++ and Java
Operator Overloading in C++
Yung-Hsiang [email protected]
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.
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 :: .* . ?:
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.
YHL Operator Overloading 5
Global Function(not a member function of a class)
YHL Operator Overloading 6
YHL Operator Overloading 7
YHL Operator Overloading 8
Copy Constructor and Operator Overloading
YHL Operator Overloading 9
YHL Operator Overloading 10
YHL Operator Overloading 11
Member Function(first operand is "this")
YHL Operator Overloading 12
YHL Operator Overloading 13
binary operator withonly one parameter
YHL Operator Overloading 14
YHL Operator Overloading 15
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);
YHL Operator Overloading 17
Unary Operator, Global Function
YHL Operator Overloading 18
unary operator withonly one parameter
YHL Operator Overloading 19
Unary Operator, Member Function
YHL Operator Overloading 20
unary operator without parameter ("this" is the parameter)
YHL Operator Overloading 21
Comparison Operator <
YHL Operator Overloading 22
YHL Operator Overloading 23
Equality Operator ==
YHL Operator Overloading 24
YHL Operator Overloading 25
Self Test
YHL Small Int 1
ECE 462Object-Oriented Programming
using C++ and Java
Small Int and Conversion
Yung-Hsiang [email protected]
YHL Small Int 2
Increment and Decrement Operators (Small Int)
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;
}
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
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
YHL Small Int 6
YHL Small Int 7
YHL Small Int 8
YHL Small Int 9
YHL Small Int 10
YHL Small Int 11
YHL Small Int 12
YHL Small Int 13
YHL Small Int 14
Programmer Defined(Implicit) Class Conversion
YHL Small Int 15
YHL Small Int 16
YHL Small Int 17
Explicit in C++
YHL Small Int 18
YHL Small Int 19
YHL Small Int 20
YHL Small Int 21
Self Test