Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

Similar documents
More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

COP Programming Assignment #7

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CS 247: Software Engineering Principles. ADT Design

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

PIC10B/1 Winter 2014 Exam I Study Guide

Introduction. Lecture 5 Files and Streams FILE * FILE *

Sets: Efficient Searching and Insertion

Lecture 5 Files and Streams

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

Streams. Rupesh Nasre.

Streams. Ali Malik

CSI33 Data Structures

Module C++ I/O System Basics

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

The C++ Language. Arizona State University 1

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Lecture 3 The character, string data Types Files

Understanding main() function Input/Output Streams

I/O Streams and Standard I/O Devices (cont d.)

ADTs & Classes. An introduction

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

COMP322 - Introduction to C++

Programming with C++ as a Second Language

Engineering Problem Solving with C++, Etter/Ingber

Operator Overloading in C++ Systems Programming

CS 103 Unit 14 - Streams

W3101: Programming Languages C++ Ramana Isukapalli

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Lecture 4 Tao Wang 1

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Revising the wording of stream input operations

CS 103 Unit 14 - Streams

G52CPP C++ Programming Lecture 18

UNIT- 3 Introduction to C++

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

CSCI 1061U Programming Workshop 2. C++ Basics

Jan 27, C++ STL Streams. Daniel Maleike

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

PIC 10A Objects/Classes

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

IS 0020 Program Design and Software Tools

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 12 - C++ Stream Input/Output

C++ files and streams. Lec 28-31

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Chapter 18 - C++ Operator Overloading

Objectives. In this chapter, you will:

Final Exam Solutions PIC 10B, Spring 2016

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

UEE1303(1070) S 12 Object-Oriented Programming in C++

Input/Output Streams: Customizing

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Final Exam. Name: Student ID: Section: Signature:

Stream States. Formatted I/O

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

C++ Input/Output Chapter 4 Topics

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter 3 - Notes Input/Output

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

UEE1302 (1102) F10: Introduction to Computers and Programming

This watermark does not appear in the registered version - Slide 1

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Introduction to C++ (Extensions to C)

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

Streams - Object input and output in C++

All About: File I/O in C++ By Ilia Yordanov, ; C++ Resources

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

CS242 COMPUTER PROGRAMMING

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Constructors.

11.2. Overloading Operators

What s in a file, what s in a string?

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 2

C++ For Science and Engineering Lecture 12

CS11 Advanced C++ Lecture 2 Fall

The University Of Michigan. EECS402 Lecture 15. Andrew M. Morgan. Savitch Ch. 8 Operator Overloading Returning By Constant Value

Chapter 11 Customizing I/O

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Due Date: See Blackboard

Lecture 3 Tao Wang 1

Ch. 12: Operator Overloading

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Transcription:

Overview of Lecture 1 Overloading I/O Operators 2 Overloading << and >> for Fractions 3 Formatted vs Unformatted Input 4 Setting the State of a Stream 5 Questions PIC 10B Streams, Part II April 20, 2016 1 / 10

The Operators << and >> What does the code int n = 10; std :: cout << n; really mean? std::cout is an instance of the class std::ostream. << is a binary operator that has been overloaded to take an int as a parameter. The << operator converts the value of n to the text you see printed on the screen! A declaration for << (for int) looks like std :: ostream & operator < <( std :: ostream & os, const int & val ); Notice << returns a reference to an ostream in fact, it returns a reference to os. The operator >> is declared similarly for the class istream: std :: istream & operator > >( std :: istream & is, const int & val ); PIC 10B Streams, Part II April 20, 2016 2 / 10

Overloading << and >> The symbols << and >> are operators that can be overloaded, just like any other operators. For example, we can declare for our Fraction class std :: ostream & operator < <( std :: ostream & os, Fraction & frac ); and std :: istream & operator > >( std :: istream & is, Fracction & frac ); How should we implement these operators? PIC 10B Streams, Part II April 20, 2016 3 / 10

Implementing << for Fraction Implementing << should not be difficult... we should just implement it in terms of the versions of <<! Desired behavoir: Fraction frac (3, 4); // Make a Fraction with value 3/4 std :: cout << frac ; // Should print 3/4 To make this happen: std :: ostream & operator < <( std :: ostream & os, Fraction & frac ) { os << frac. numerator () << "/" << frac. denominator (); That s all there is to it! The os already knows how to deal with ints and strings! See fraction-io.cpp for examples. PIC 10B Streams, Part II April 20, 2016 4 / 10

Implementing >> for Fraction Dealing with >> and std::istream objects can be messier because we may have to verify the formatting. Should our program ignore white-space characters? For example, should we allow "3 / 4" to be a valid Fraction input? If the pattern int/int is not matched, how do we let the std::istream know? First attempt at a simple Fraction reader: std :: istream & operator > >( std :: istream & is, Fraction & frac ) { int num, den ; char c; istream >> num >> c >> den ; frac. set_value (num, den ); Will this work for all inputs? PIC 10B Streams, Part II April 20, 2016 5 / 10

What We d Like for Fraction Input Ideally all of the following would be admissable inputs 3/4 // This is how a fraction will be printed using << 3 / 4 // This should also be acceptable 3 // Should represent an integer with value 3/1 PIC 10B Streams, Part II April 20, 2016 6 / 10

Formatted vs Unformatted Input Formatted input operators std :: istream is;... // Read something from is and store value to var is >> var ; // must have operator >> defined for var s datatype! getline ( is, str ); // read a line and save to std :: string str Unformatted input is. get (); // Returns value of character read is. get ( ch ); // Stores value of character read to char ch is. peek (); // returns next character in stream is. unget (); // go back to the previous character in the stream Notes: By default >> ignores whitespace characters. get and peek do not ignore whitespace. get increments the position in the stream by one character. peek does not increment the position. unget decrements the position in the stream, allowing you to re-read the previous character. PIC 10B Streams, Part II April 20, 2016 7 / 10

Getting Fraction Input Code std :: istream & operator > >( std :: istream & is, Fraction & frac ) { int num, den = 1; char slash ; is >> num ; slash = is. peek (); while ( slash == ) { // Ignore spaces before / is.get (); slash = is. peek (); if( slash == / ) { is >> slash ; is >> den ; frac = Fraction ( num, den ); return is; PIC 10B Streams, Part II April 20, 2016 8 / 10

Handling Errors with std::istream Stream objects have a state: The state is good() if the object hasn t encountered an error. Otherwise the state is fail, eof (end of file), or bad io.fail() is true if the last operation didn t work as expected because of a logical error (e.g., tried to read an int but encountered the character a ). io.eof() is true if the IO object reached the end of the file. io.bad() is true if the IO object encountered an error reading or writing. If a std::istream is is in a bad state, is.clear() puts it back into a good state if it was in a fail state. The character EOF denotes the end of a file. You can set the state of a stream to fail using is. setstate ( std :: ios_base :: failbit ); PIC 10B Streams, Part II April 20, 2016 9 / 10

Questions 1 What is the difference between getline (is, str ); and is >> str ; When might you prefer one to the other? 2 Consider the following code, which is meant to copy ints input from cin to some output stream os. int val ; while ( std :: cin >> val ) { os << val ; Does the program run as you d expect? When does the while loop terminate? How would you change the code so that the loop terminates after the user hits return/enter? PIC 10B Streams, Part II April 20, 2016 10 / 10