Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Slides:



Advertisements
Similar presentations
Lecture 15: I/O and Parsing
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
CMSC 2021 Stream I/O Operators and Friend Functions.
CS 117 Section 2 + KNET Computer accounts – ed to KNET students –Change password Homework 1 Lab Tutors –In lab for next 2 weeks –Will help you with.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Vector Application : A UML Example © Dr. David A. Workman School of EE and CS University of Central Florida Feb. 8, 2001.
File I/O ifstreams and ofstreams Sections 11.1 &
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
Lab2 C++ Warmup: Pet Applicatioin Cop 4331 and EEL4884 © Dr. David A. Workman School of EE and Computer Science University of Central Florida February.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
Lab2 Diagrams Cop 4232 Fall Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
UNIT VI Streams & Files in C++: Stream classes, stream errors, disk file I/O with streams, File pointers, Error handling in file I/O. File I/O with members.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Introduction to the Object-oriented Data Protocol © Dr. David Workman COP4331 School of EE and CS February 4, 2010.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Comparison of Design Styles (Part II) Case Study: 2D Geometry Solver COP4331 OO Processes for Software Development © Dr. David A. Workman March 17, 2009.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
CS212: Object Oriented Analysis and Design
Basic Input and Output Operations
week 1 - Introduction Goals
Data Streams.
Programming with Data Files
Chapter 3: Input/Output
Introduction to Programming
File I/O.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
File I/O in C++ I.
Introduction to Programming
Presentation transcript:

Lab2 Diagrams Cop 4232 Fall 2005

Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr

Fall 2005Lab2 Cop42323 SimMgmt & SimModels SimMgmtSimModels Agent #InMgr& finMgr //class #OutMgr& foutMgr //class -string name //instance +Agent() +Agent( string ) +Agent(Tokenizer& ) +Agent(StringTokenizer& ) +Insert() +Extract() +Extract(Tokenizer&) +Extract(StringTokenizer&) +operator>>(ifstream&, Agent&) +operator string +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) #Put() #Get(Tokenizer&) #Get(StringTokenizer&) Customer -Money cash -string paymethod -CreditCard bankcard +Customer() +Customer( string, Money, string, CreditCard ) +Customer(Tokenizer& ) +Customer(StringTokenizer& ) +Extract() +Extract(Tokenizer&) +Extract(StringTokenizer&) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put() PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt //computed -double purchasegal //computed -double pumprate -Grade gradeselected +PumpStation() +PumpStation( string, Money[], Money, double, Grade ) + PumpStation(Tokenizer& ) + PumpStation(StringTokenizer& ) +Extract() +Extract(Tokenizer&) +Extract(StringTokenizer&) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put()

Fall 2005Lab2 Cop42324 OO Data for Class Hierarchies X{ }X XData1: value1 XData2: value2 XData3: XDatak: valuek Y{ }Y YData1: value1 YData2: value2 Instance of X Embedded instance of Y INPUT FORMAT: Tokens delimited by whitespace characters – otherwise, free form – no margin requirements. OUTPUT FORMAT: Tokens delimited by whitespace characters on each side. Opening token and closing token have to align on the same character position, but on different lines. First data member follows the opening token on the same line. All other data members are output one per line with all labels aligned on the same margin position as the first data member.

Fall 2005Lab2 Cop42325 SimMgmt & SimModels Version #1 Agent -string name //instance +Agent() +Agent( ifstream& ) +Agent(Tokenizer& ) +Agent(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) +operator>>(ifstream&, Agent&) +operator string #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) Customer -Money cash -string paymethod -CreditCard bankcard PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected +Customer() +Customer( ifstream& ) +Customer(Tokenizer& ) +Customer(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) +PumpStation() +PumpStation( ifstream& ) +PumpStation(Tokenizer& ) +PumpStation(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) InMgr and OutMgr are NOT USED! Design Principle: Provide a constructor, Extract, Insert, Get and Put methods for each external medium or boundary object to be supported by the class design. Input objects or sources: ifstream, Tokenizer, string Output objects or sinks: ostream, Also – we want to provide insertion and extraction operators on ostream and ifstream, respectively.

Fall 2005Lab2 Cop42326 Client A for Version #1 int main() { ifstream fin; ostream fout; string finName, foutName; cout << “Enter input file name: “; cin >> finName; cout << “Enter output file name: “; cin >> foutName; fin.open( finName.c_str()); fout.open( foutName.c_str()); Agent alpha, beta(fin), gamma; fin >> gamma; alpha.Extract(fin); fout << alpha << endl; beta.Insert(fout); fout << gamma << endl; } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output Style 1 Output Style 2 Problem Formatting the output image is limited. To achieve the indented style, each instance must begin on a new line of the output stream. Nested objects cannot be properly indented due to lack of formatting control on ostream objects. Free form Partially aligned

Fall 2005Lab2 Cop42327 Client B for Version #1 int main() { Tokenizer T; //Tokenizer encapsulates input stream and // user dialog Agent alpha, beta(T), gamma(T); alpha.Extract(T); string Image = “ Agent{ name: DELTA }Agent “; Agent delta( Image ); //Hidden use of StringTokenizer fout << alpha << endl; beta.Insert(fout); fout << gamma << endl; fout << delta << endl; } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: DELTA }Agent Output File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: DELTA }Agent Output Style 1 Output Style 2 Problem Formatting the output image is limited. To achieve the indented style, each instance must begin on a new line of the output stream. Nested objects still cannot be properly indented. or

Fall 2005Lab2 Cop42328 SimMgmt & SimModels Version #2 Agent -string name //instance +Agent() +Agent( InMgr& ) +Agent( Tokenizer& ) +Agent( string ) +Insert( OutMgr& ) +Extract( InMgr& ) +Extract( Tokenizer& ) +Extract( string ) +operator>>(ifstream&, Agent&) +operator string #Put( OutMgr& ) #Get( InMgr& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) Customer -Money cash -string paymethod -CreditCard bankcard PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected +Customer() +Customer( InMgr& ) +Customer( Tokenizer& ) +Customer( string ) +Insert( OutMgr& ) +Extract( InMgr& ) +Extract( Tokenizer& ) +Extract( string ) #Put( OutMgr& ) #Get( InMgr& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) +PumpStation() +PumpStation( InMgr& ) +PumpStation( Tokenizer& ) +PumpStation( string ) +Insert( OutMgr& ) +Extract( InMgr& ) +Extract( Tokenizer& ) +Extract( string ) #Put( OutMgr& ) #Get( InMgr& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) InMgr and OutMgr are USED! Analysis Output format requirements can be met only if Insert( OutMgr&) is used. Insertion operator << provides insufficient format control – no way to link ostream to OutMgr within the insertion operator. In fact, the insertion operator may use any output stream – not necessarily the one associated with OutMgr. Even if the ostream used by the insertion operator is the associated with OutMgr, independent use of endl will corrupt the state of OutMgr – must use newLine().

Fall 2005Lab2 Cop42329 Client A for Version #2 int main() { InMgr finMgr; ifstream& fin = finMgr.getStream(); OutMgr foutMgr; ostream& fout = foutMgr.getStream(); Agent alpha, beta(finMgr), gamma; fin >> gamma; alpha.Extract(finMgr); fout << alpha << endl; //<< cannot use foutMgr beta.Insert(foutMgr); //previous line compromises foutMgr fout << gamma << endl; //invalidates state of foutMgr } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output Style 1 Output Style 2Problem Use of insertion operators in client may corrupt or invalidate the state of OutMgr. The implementation of << cannot know the associated OutMgr (and there may not be one)! If the client inserts its own formatting operations (e.g. endl) then the state of the associated OutMgr becomes invalid and subsequent output may not be formatted correctly. To correct this, operator<< should not be defined as a friend. Defining operator>> is permitted as a friend. In fact, Extract() and Get() can be defined to take ifstream& as a parameter – it is not necessary to use InMgr& Output File

Fall 2005Lab2 Cop SimMgmt & SimModels Agent #InMgr& finMgr //class #OutMgr& foutMgr //class -string name //instance +Agent() +Agent(Tokenizer& ) +Agent(string ) +Insert() //uses OutMgr +Extract() //uses InMgr +Extract(Tokenizer&) +Extract(string) +operator>>(ifstream&, Agent&) +operator string +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) #Put() //uses OutMgr #Get() //uses InMgr #Get(Tokenizer&) #Get(StringTokenizer&) Customer -Money cash -string paymethod -CreditCard bankcard +Customer() +Customer(Tokenizer& ) +Customer(string ) +Extract() +Extract(Tokenizer&) +Extract(string) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put() PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected + PumpStation() + PumpStation(Tokenizer& ) + PumpStation(string ) +Extract() +Extract(Tokenizer&) +Extract(string) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put() Version #3 InMgr and OutMgr are Encapsulated by Agent!

Fall 2005Lab2 Cop Client A for Version #3 int main() { InMgr& finMgr = Agent::InMgrOf(); //access class attributes OutMgr& foutMgr = Agent::OutMgrOf(); //access class attributes ifstream& fin = finMgr.getStream(); ostream& fout = foutMgr.getStream(); Agent alpha, beta, gamma; beta.Extract(); fin >> gamma; alpha.Extract(); fout << alpha; //verifies that fout is associated with foutMgr beta.Insert(); //uses foutMgr fout << gamma; //verifies that fout is associated with foutMgr foutMgr.newLine(); } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output File

Fall 2005Lab2 Cop Hidden File Streams IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr Agent #InMgr& finMgr //class #OutMgr& foutMgr //class +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) CreditCard #InMgr& finMgr //class #OutMgr& foutMgr //class +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) Base class #InMgr& finMgr //class #OutMgr& foutMgr //class +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) … Initialized at load time to pgmInMgr and pgmOutMgr Conclusion: If your design must satisfy both: (a) Use of insertion operator<< (b) Indented margins for nested objects Then each base class must provide a hidden OutMgr

Fall 2005Lab2 Cop Lab2 Fall05 SimMgmt SimModels CustomerPumpStation JiffyStop Agent 1..* MoneyCreditCard IOMgmt InMgrOutMgr TokenErrorIOError AppError pgmInMgrpgmOutMgr StringTokenizerTokenizer

Fall 2005Lab2 Cop Lab1 Figures A-1,2,3,4 Line Org Cur pos File Org idx Margin Stack MARGINSIZE Line Org Cur pos File Org Stack (before) Stack (after) idx Method: pushMargin() Line Org Cur pos File Org Stack (before) Stack (after) idx Method: popMargin() Cur fin File Org Method: deltaMargin(input) New fin (delta > 0) New fin (delta < 0) Line Org Curpos (before) File Org Method: newLine() Curpos (after) File Org Line Org

Fall 2005Lab2 Cop Figures 3-6,7,8 Line Org Cur pos File Org Method: deltaMargin(output) Cur pos (delta > 0) diff File Org Cur pos (delta < 0) Line Org diff Cur fin File Org Method: toMargin(input) New fin idx