Chapter 11-2 Operator Overloading - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Chapter 11-2 Operator Overloading

Description:

... required for all stream-I/O operations Defines input/output classes istream ostream Defines standard stream objects cin : the standard input stream object ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 15
Provided by: Dell570
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11-2 Operator Overloading


1
Chapter 11-2Operator Overloading
  • C, How to Program
  • Deitel Deitel

2
Streams
  • C I/O occurs in streams sequences of bytes
  • Input
  • Bytes flow from a device to main memory
  • Output
  • Bytes flow from main memory to a device
  • I/O transfers typically take longer than
    processing the data

3
Standard Streams
  • C standard stream libraries
  • Enables I/O operations with Unicode characters
  • Unicode character set
  • Represents most of the worlds commercially
    viable languages, mathematical symbols and more
  • www.unicode.org

4
ltiostreamgt header file
  • Declares basic services required for all
    stream-I/O operations
  • Defines input/output classes
  • istream
  • ostream
  • Defines standard stream objects
  • cin the standard input stream object
  • cout the standard output stream object

5
Stream operators overloading
  • Stream insertion operator
  • Left-shift operator (ltlt) is overloaded for stream
    output
  • Left operand of type ostream
  • Such as cout object in cout ltlt classObject
  • Stream extraction operator
  • Right-shift operator(gtgt) is overloaded for stream
    input
  • Left operand of istream
  • Such as cin object in cin gtgt classObject
  • Already overloaded to process each built-in type
  • Thus, both must be global functions

6
friend function
  • Use global function with two arguments
  • One argument must be class object or reference
  • Often use friend function for performance reason.
  • friend function of a class is defined outside
    that classs scope (external function), yet has
    the right to access the non-public and public
    members of the class.
  • We declare a prototype of this external function
    within the class with keyword friend.

7
operatorgtgt
  • friend function (global function)
  • //Complex.h
  • friend istream operatorgtgt( istream , Complex
    ) //(real,img)
  • //Complex.cpp
  • istream operatorgtgt(istream input, Complex
    cNumber)
  • double real, imaginary
  • input.ignore(256, '(') //ignor (
  • input gtgt real //read real part
  • input.ignore(256,',') //white spaces up to
    next "," are ignored.
  • input gtgt imaginary //read imaginary part
  • input.ignore(256,')') //ignore white spaces
    after it.
  • if (input.good())
  • cNumber.real real
  • cNumber.imaginary imaginary
  • return input // enable cin gtgt c1 gtgt c2 gtgt c3

8
Operatorltlt
  • friend function (global function)
  • //Complex.h
  • friend ostream operatorltlt(ostream , const
    Complex )
  • //(real,imaginary)
  • //Complex.cpp
  • ostream operatorltlt(ostream output,
  • const Complex
    cNumber)
  • output ltlt "(" ltlt cNumber.real
  • ltlt", ltlt cNumber.imaginary ltlt ")"
  • return output //enable cout ltlt c1 ltlt c2 ltlt
    c3

9
Restrictions on Operator Overloading
  • Cannot change
  • Precedence of operator (order of evaluation)
  • Use parentheses to force order of operators
  • 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)

10
Restrictions on Operator Overloading
  • Cannot create new operators
  • Operators must be overloaded explicitly
  • Overloading and does not overload
  • Operator ? cannot be overloaded

11
Fig. 11.1 Operators that can be overloaded.
12
Fig. 11.2 Operators that cannot be overloaded.
13
Restrictions on Operator Overloading
  • At least one argument of an operator function
    must be an object or reference of a user-defined
    type. This prevents programmers from changing how
    operators work on fundamental types.

14
Reference
  • Reproduced from the Cyber Classroom for C, How
    to Program, 5/e by Deitel Deitel.
  • Reproduced by permission of Pearson Education,
    Inc.
Write a Comment
User Comments (0)
About PowerShow.com