Kate Gregory with material from Deitel and Deitel - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Kate Gregory with material from Deitel and Deitel

Description:

C Stream IO. cin is an instance of the istream class ... Manipulators change the behaviour of the stream when they are sent to it ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 17
Provided by: kategr
Category:

less

Transcript and Presenter's Notes

Title: Kate Gregory with material from Deitel and Deitel


1
Week 9
  • Midterm is marked
  • Lab 4 is not marked
  • Lab 5 is due now
  • Stream IO
  • Lab 6

2
Midterm
  • Average 36.4/50 73
  • High mark 49/50
  • Each and every question earned somebody full
    marks
  • Section 2 was the hardest
  • Be sure to follow instructions
  • Take by reference, write a constructor, etc

3
(No Transcript)
4
Stream IO
  • Simple input and output is easy to do
  • cout ltlt Enter a number
  • cin gtgt x
  • Type aware
  • Allows tremendous power through operator
    overloading
  • cout ltlt NewHire

5
What was wrong with printf?
  • You had to tell it the types involved
  • printf(x is d, x)
  • If you lied to it, very strange things happened
  • Tremendous vulnerability for things like buffer
    overflow attacks when reading a string
  • There was no simple way to print a struct, array,
    or other non-built-in type

6
What was good about printf?
  • The f is for formatted
  • Easy to set precision and padding
  • printf(5.2f,x)
  • Terrific for tables and reports
  • So how do we achieve this with cin and cout?

7
C Stream IO
  • cin is an instance of the istream class
  • cout is an instance of the ostream class, as is
    cerr
  • The gtgt and ltlt operators can be overloaded to
    teach the istream and ostream classes how to
    handle your classes

8
Manipulators
  • Manipulators change the behaviour of the stream
    when they are sent to it
  • cout ltlt "hello" ltlt endl
  • cout ltlt x ltlt flush
  • Standard manipulators are defined in iomanip or
    iomanip.h
  • You can even write your own manipulators

9
Setting the base
  • int x 32
  • cout ltlt x ltlt in hex is
  • cout ltlt hex ltlt x ltlt dec
  • hex for hexadecimal
  • dec for decimal (ordinary)
  • oct for octal
  • setbase(n) to specify numerically

10
Setting the precision
  • setprecision manipulator
  • float f 1.23456
  • cout ltlt f ltlt endl
  • cout ltlt setprecision(2)
  • cout ltlt f ltlt endl
  • cout ltlt setprecision(0)

11
Setting the Width
  • setw manipulator
  • int i1
  • int j12
  • int k123
  • cout ltlt setw(5)
  • cout ltlt i ltlt endl
  • cout ltlt setw(5)
  • cout ltlt j ltlt endl
  • cout ltlt setw(5)
  • cout ltlt k ltlt endl

12
Member Functions
  • cin and cout are objects. They have member
    functions as any other object does.
  • cout.precision() is the same as the setprecision
    manipulator
  • cout.width() is the same as the setw manipulator
  • cin.getline()reads an entire line, spaces and
    all, into a string

13
Setting the Justification
  • Another manipulator sets a number of internal
    flags to cover things like alignment
  • cout ltlt setw(5) ltlt i ltlt endl
  • cout ltlt setw(5) ltlt setiosflags(iosleft) ltlt i ltlt
    endl
  • cout ltlt setiosflags(iosright)
  • Default is right-aligned within the width

14
Setting the Fill Character
  • cout.fill('')
  • cout ltlt setw(5) ltlt j ltlt endl
  • cout.fill(' ')
  • cout ltlt setw(5) ltlt j ltlt endl
  • Note single quotes
  • Default is space

15
You have tremendous control
  • Check the text or your online documentation for
    other manipulators, member functions, and flags
  • Remember you can use all this power when printing
    integers and floats, as here, but also in your
    own classes when overloading ltlt and gtgt

16
For Next class
  • Read chapters 12 and 13
  • Lab 6
Write a Comment
User Comments (0)
About PowerShow.com