Chapter 11 Operator Overloading String and Array Objects - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Chapter 11 Operator Overloading String and Array Objects

Description:

Operator Functions as Class Members ... Use operators with objects (operator overloading) ... object2 = object1.add( object2 ); vs. object2 = object2 object1; ... – PowerPoint PPT presentation

Number of Views:324
Avg rating:3.0/5.0
Slides: 28
Provided by: tcu3
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11 Operator Overloading String and Array Objects


1
Chapter 11 Operator Overloading String and
Array Objects
  • Part I

2
Objectives
  • 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

3
11.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.)

4
11.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

5
Software Engineering Observation 11.1
  • Operator overloading contributes to Cs
    extensibilityone of the languages most
    appealing attributes.

6
Good Programming Practice 11.2
  • Overloaded operators should mimic the built-in
    functionality
  • For example, the operator should be overloaded
    to perform addition, not subtraction.

7
11.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

8
11.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.

9
Fig. 11.1 Operators that can be overloaded.
Fig. 11.2 Operators that cannot be overloaded.
10
11.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

11
Example 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
12
Example 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)
13
Example 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)
14
11.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

15
Example 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)
16
Example 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.

17
Example 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())
18
Example 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)
19
11.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

20
11.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

21
Example
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
22
Example
  • In main()

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)
23
11.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

24
11.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

25
11.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)

26
Example 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
27
Example 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
Write a Comment
User Comments (0)
About PowerShow.com