CS 325 Software Development and Systems - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS 325 Software Development and Systems

Description:

I/O operators will also be friend functions of the class Rational. Syntax for overloading ... output member functions to be friend functions that overload the ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 14
Provided by: janett
Category:

less

Transcript and Presenter's Notes

Title: CS 325 Software Development and Systems


1
CS 325 Software Development and Systems
2
Lecture 8
  • Today
  • Rational Class
  • Friend Functions
  • Chapter 6 Classes
  • Chapter 7 More on classes
  • Chapter 8 Operator Overloading
  • Next Project
  • Is on the website (cs.ua.edu/325) due Thursday,
    October 7 _at_ midnight

3
Quick Review
  • What goes in the specification?
  • What do we call the file where the specification
    is stored?
  • What goes in the implementation?
  • An instance of a class is called
  • What is the difference between the private
    section and the public section?

4
Lets write a class for Rational Numbers
  • Want to be able input rational numbers, output
    rational numbers, add rational numbers, multiply
    rational numbers,

5
Want to be able to overload operators
  • Well use friend functions to do this.
  • Friend function an ordinary function that has
    access to the private parts of a class
  • Prototype must be in class specification
  • friend rtrn_type funcName(arguments)
  • Prototype will be defined in class implementation
  • rtrn_type funcName(arguments)

6
Addition with rationals
  • In public section of specification
  • friend rational operator (rational, rational
    )
  • In implementation
  • rational operator (rational r1, rational r2)
    rational res
  • res.n r1.n r2.d r2.n r1.d
  • res.d r1.d r2.d

7
Class Exercises
  • Write a friend function that will allow us to
    overload the operator for multiplication
  • Write a member function that will allow us to
    reduce a rational number

8
Overloading ltlt gtgt
  • Read and print integers easily
  • int x
  • cin gtgt x
  • cout ltlt x ltlt endl
  • Need same ability for our Rational class
  • Rational r
  • cin gtgt r
  • cout ltlt r ltlt endl
  • Overload ltlt and gtgt operators to work with
    Rationals (the Rational class)

9
Background on cin and cout
  • Both are instances of a class (objects)
  • cin an instance of input stream class
  • cout an instance of output stream class
  • ltlt and gtgt are binary operators
  • int a, b
  • cin gtgt a // reads value into a
  • cingtgtb // reads value into b
  • Need to let these two classes see a Rational

10
Overloading ltlt gtgt
  • Need to let the I/O classes have access to the
    private part of our Rational class
  • input operation needs to set num and den
  • output operation needs to read num and den
  • I/O operators will also be friend functions of
    the class Rational

11
Syntax for overloading ltlt gtgt
  • friend ostream operatorltlt(ostream os, Rational
    r)
  • os ltlt r.nltlt "/" ltlt r.d return os
  • Line 1
  • 1. The method is a friend of class Rational
  • 2. The method returns an ostream (modified
    output stream)
  • 3. The operator we are overloading is ltlt
  • 4. The two arguments are the output stream the
    Rational to output
  • Line 2
  • 1. On the output stream, print numerator, /,
    denominator
  • 2. Return the modified output stream (now
    includes these characters)

12
Class Exercises
  • Modify your input/output member functions to be
    friend functions that overload the gtgt and ltlt
    operators.

13
End of Lecture 8
  • Next Time Overloading operators using member
    functions
Write a Comment
User Comments (0)
About PowerShow.com