Stream I/O Operators and Friend Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Stream I/O Operators and Friend Functions

Description:

Stream I/O Operators and Friend Functions Stream I/O Background contains declarations for classes istream ostream cin is an istream object cout is an ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 13
Provided by: Eddi98
Category:

less

Transcript and Presenter's Notes

Title: Stream I/O Operators and Friend Functions


1
Stream I/O OperatorsandFriend Functions
2
Stream I/O Background
  • ltiostream.hgt contains declarations for classes
  • istream
  • ostream
  • cin is an istream object
  • cout is an ostream object

3
Stream I/O Background (continued)
  • C implements stream I/O by overloading the gtgt
    and ltlt operators to allow integers, reals, and
    characters to be input from or output to a
    stream. (gtgt and ltlt are really bit shift
    operators.)
  • Extraction operator (gtgt) use
  • istream_object gtgt object1 gtgt object2 gtgt
    objectn
  • cin gtgt x gtgt y
  • Insertion operator (ltlt) use
  • ostream_object ltlt object1 ltlt object2 ltlt
    objectn
  • cout ltlt Number ltlt num ltlt endl
  • gtgt and ltlt return objects of type
    istream_object and ostream_object, respectively.

4
Overloading Stream I/O Operators
  • How can we read data into or write out the values
    of data types other than integer, floating point,
    or character?
  • Fraction
  • Vector
  • Set
  • Options
  • Class methods, such as
  • Fraction FractionreadFraction()
  • Fraction FractiondisplayFraction()
  • Write functions to further overload gtgt and ltlt

5
Overloading Stream I/O Operators (continued)
  • Syntax for overloading gtgt
  • istream operator gtgt (istream is_name,

  • class_name object_name)
  • is_name is passed by reference because its state
    will be changed
  • object_name is passed by reference because data
    will be read into it
  • Syntax for overloading ltlt
  • ostream operator ltlt(ostream os_name,
  • const
    class_name object_name)
  • os_name is passed by reference because its state
    will be changed
  • object_name is a const because it will not be
    changed

6
Overloading Stream I/O Operators (continued)
  • Fraction class example
  • class Fraction
  • public
  • // constructors and other methods
  • void setNumerator(int n)
  • void setDenominator(int d)
  • int getNumerator()
  • int getDenominator()
  • private
  • int numerator, denominator
  • istream operator gtgt(istream input, Fraction
    f)
  • ostream operator ltlt(ostream output, const
    Fraction f)

7
Overloading Stream I/O Operators (continued)
  • istream operator gtgt(istream input, Fraction f)
  • char c // to hold the / separator
  • int n, d
  • input gtgt n gtgt c gtgt d
  • if (d 0)
  • cerr ltlt Zero denominator invalid ltlt endl
  • exit(1)
  • f.setNumerator(n)
  • f.setDenominator(d)
  • return input

8
Overloading Stream I/O Operators (continued)
  • ostream operator ltlt(ostream output, const
    Fraction f)
  • output ltlt f.getNumerator() ltlt / ltlt
    f.getDenominator()
  • return output

9
Friend Functions
  • A function can be defined to be a friend of a
    class
  • friend function-name(arguments) // inside of
    class definition
  • A friend function has direct access to the class
    private members, both data and methods
  • Making a function a friend can be convenient --
    the class does not need get methods for the
    function to access its data
  • Using a friend function violates the rules of
    encapsulation.
  • Use friends only when absolutely necessary!

10
Friend Functions (continued)
  • Fraction class using friends
  • class Fraction
  • public
  • // constructors and other methods
  • friend istream operator gtgt(istream
    input, Fraction f)
  • friend ostream opeator ltlt(ostream
    input, const Fraction f)
  • // set and get methods
  • private
  • int numerator, denominator
  • istream operator gtgt(istream input, Fraction
    f)
  • ostream operator ltlt(ostream output, const
    Fraction f)

11
Friend Functions (continued)
  • istream operator gtgt(istream input, Fraction f)
  • char c // to hold the / separator
  • input gtgt f.numerator gtgt c gtgt f.denominator
    // direct access
  • if (f.denominator 0)
  • cerr ltlt Zero denominator invalid ltlt endl
  • exit(1)
  • return input

12
Friend Functions (continued)
  • ostream operator ltlt(ostream output, const
    Fraction f)
  • output ltlt f.numerator ltlt / ltlt f.denominator
    // direct access
  • return output
Write a Comment
User Comments (0)
About PowerShow.com