Title: Chapter 11 Operator Overloading String and Array Objects
1Chapter 11 Operator Overloading String and
Array Objects
2Objectives
- Fundamentals of Operator Overloading
- Restrictions on Operator Overloading
- Operator Functions as Class Members vs. Global
Functions - Overloading Stream Insertion and Stream
Extraction Operators - Overloading Unary Operators
- Overloading Binary Operators
311.1 Introduction
- Use operators with objects (operator overloading)
- Clearer than function calls for certain classes
- Examples
-
- Performs arithmetic on multiple items (like
matrix, polynomial, etc.)
411.2 Fundamentals of Operator Overloading
- Types for operator overloading
- Can use existing operators with user-defined
types - Cannot create new operators
- Overloading operators
- Create a function for the class
- Name of operator function
- Keyword operator followed by symbol
- Example
- operator for the addition operator
5Software Engineering Observation 11.1
- Operator overloading contributes to Cs
extensibilityone of the languages most
appealing attributes.
6Good Programming Practice 11.2
- Overloaded operators should mimic the built-in
functionality - For example, the operator should be overloaded
to perform addition, not subtraction.
711.2 Fundamentals of Operator Overloading
- Overloading provides concise notation
- object2 object1.add( object2 ) vs.object2
object2 object1 - If you want to use operators on a class object,
the operators must be overloaded for that class,
except - Assignment operator ()
- Memberwise assignment between objects
- Address operator ()
- Returns address of object
- Comma operator (,)
- Evaluates expression to its left then the
expression to its right
811.3 Restrictions on Operator Overloading
- Cannot change
- Precedence of operator
- Associativity (left-to-right or right-to-left)
- Number of operands
- e.g., is unary, can only act on one operand
- How operators act on built-in data types (i.e.,
cannot change integer addition) - Cannot create new operators
- Operators must be overloaded explicitly.
- Overloading and does not overload .
- Some operators cannot be overloaded.
9Fig. 11.1 Operators that can be overloaded.
Fig. 11.2 Operators that cannot be overloaded.
1011.4 Operator Functions as Class Members vs.
Global Members
- As member functions
- Use this keyword to implicitly get left operand
argument - Operators (), , -gt or any assignment operator
must be overloaded as a class member function
11Example Overlaoding Operator as a Member
Function
- Overloading operator as member function
- Prototype in the header file (.h)
class Complex public
Complex(int0, int0) Complex operator
(Complex) private int real,
imginary
12Example Overlaoding Operator as a Member
Function
- Overloading operator as member function
- In main()
Complex P, Q //Initialize P and Q Complex R
P Q
This statement will be interpreted as Complex R
P.operator (Q)
13Example Overlaoding Operator as a Member
Function
- Definition in the source file (.cpp)
- Use this pointer to explicitly indicate the first
object operand.
Complex Complex operator(Complex Q)
return Complex(realQ.real, imaginaryQ.imaginary)
Complex Complex operator(Complex Q)
return Complex(this-gtrealQ.real,
this-gtimaginaryQ.imaginary)
1411.4 Operator Functions as Class Members vs.
Global Members
- As global functions
- Need parameters for both operands
- Can be a friend to access private or protected
data
15Example Overlaoding Operator as a Global
Function
- Overloading operator as global function
- Prototype
- operator() is not part of class Complex.
- Definition in the source file (.cpp)
Complex operator(Complex P, Complex Q)
Complex operator(Complex P, Complex Q)
16Example Overlaoding Operator as a Member
Function
- Since the global function is not part of the
class, accessing private data of the class should
be through - public get functions of the class.
- declaring the global function as a friend of the
class.
17Example Overlaoding Operator as a Global
Function
- Through public get functions of the class
class Complex public Complex(int0,
int0) int GetReal() int
GetImaginary() private int real,
imaginary Complex operator(Complex P,
Complex Q) return Complex(P.GetReal()Q.Get
Real(), P.GetImginary()Q. GetImginary())
18Example Overlaoding Operator as a Global
Function
- Through declaring the global function as a friend
of the class.
class Complex friend Complex operator
(Complex P, Complex Q) public
Complex(int0, int0) private int
real, imaginary Complex operator(Complex
P, Complex Q) return Complex(P.realQ.real,
P.imginaryQ. imginary)
1911.4 Operator Functions as Class Members vs.
Global Members
- Overloaded ltlt operator
- Left operand of type ostream
- Such as cout object in cout ltlt classObject
- Similarly, overloaded gtgt has left operand of
istream - Thus, both must be global functions
2011.5 Overloading Stream Insertion and Stream
Extraction Operators
- ltlt and gtgt operators
- Already overloaded to process each built-in type
and can also process a user-defined class - Overload using global, friend functions
- Example program
- Class Complex
- Holds the real part and the imaginary part say,
3 and 5. - Print out formatted complex number automatically
- 3 5i
21Example
class Complex friend istream operatorgtgt
(istream in, Complex P) friend ostream
operatorltlt (ostream out, Complex P) public
Complex(int0, int0) private
int real, imaginary istream
operatorgtgt(istream in, Complex P) in gtgt
P.real gtgt P.imaginary return in ostream
operatorltlt(ostream out, Complex P) out
ltlt P.real ltlt ltlt P.imaginary ltlt i
return out
22Example
include ltiostreamgt using namespace std int
main() Complex P, Q cin gtgt P gtgt Q
cout ltlt P cout ltlt P ltlt Q return
0
This statement is interpreted as operatorgtgt
(operatorgtgt(cin, P), Q)
This statement is interpreted as operatorltlt(cout,
P)
This statement is interpreted as operatorltlt
(operatorltlt(cout, P), Q)
2311.4 Operator Functions as Class Members vs.
Global Members (Cont.)
- Commutative operators may require to be
commutative - So both a b and b a work
- Suppose we have two different classes
- Overloaded operator can only be member function
when its class is on left - Complex int
- Can be member function
- When other way, need a global overloaded function
- int Complex
2411.6 Overloading Unary Operators
- Overloading unary operators
- As non-static member function with no arguments
- Remember, static functions only access static
data - As global function with one argument
- Argument must be class object or reference to
class object
2511.6 Overloading Unary Operators
- Example
- Overload ! to test for empty string
- If non-static member function, needs no arguments
- class Stringpublic bool operator!()
const - !s becomes s.operator!()
- If global function, needs one argument
- bool operator!( const String )
- s! becomes operator!(s)
26Example Overlaoding Operator as a Member
Function
- Overloading operator as member function
- Prototype in the header file (.h)
class Complex public
bool operator! () private int real,
imginary
bool Complex operator!() if (real 0
imaginary 0) return true
return false
27Example Overlaoding Operator as a Global
Function
- Through declaring the global function as a friend
of the class.
class Complex friend bool operator!
(Complex P) public Complex(int0,
int0) private int real, imaginary
bool operator!(Complex P) if (P.real
0 P.imaginary 0) return true
return false