Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Chapter 15: Operator Overloading
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
File I/O ifstreams and ofstreams Sections 11.1 &
FILE HANDLING IN C++.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
C++ FILE I/O.
Student Book Input / Output in C++
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
FILE HANDLING(WORKING WITH FILES) FILE- A file is a collection of related data stored in a particular area on the disk. STREAMS- interface between the.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
C ++ MULTIPLE CHOICE QUESTION
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
FILE HANDLING IN C++.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Today’s Lecture I/O Streams Tools for File I/O
Topics Input and Output Streams More Detailed Error Testing
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
C++ Programming: chapter 6 – iostream
CISC101 Reminders Assignment 3 due today.
File I/O in C++ II.
SPL – PS3 C++ Classes.
Presentation transcript:

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions, cont. –Random file I/O. –Start Operator Overloading. (If we have time.) Soon: –More about what goes in a RAD.

Function Details! A few odds ‘n ends: –Return types –Overloading –Variable scope –static local variables –Default arguments –Passing by value, by reference and by constant reference –Pointers to functions Fall 2015CISC/CMPE320 - Prof. McLeod2 Last time.

Fall 2015CISC/CMPE320 - Prof. McLeod3 Aside – static Variables You can declare a local variable to be static inside a function. See StaticLocalTest.cpp Note that the static variable is only set to zero once. Look at the program in a debugger?

Fall 2015CISC/CMPE320 - Prof. McLeod4 Default Arguments For example: int getVol(int len, int width = 1, int height = 1); This can be done by overloading alone. You can accept the default value or replace it. Default arguments must come after normal parameters, and cannot have a normal parameter mixed in the list. Note – do not assign default arguments again in the function implementation. See DefaultArguments.cpp

Fall 2015CISC/CMPE320 - Prof. McLeod5 Passing Arguments See PassingThings.cpp. In summary: –Pass a reference to avoid using the copy constructor. –If you are not going to change the reference’s contents, use the const keyword. –If you are going to change the parameter, then “call by reference” is a bit easier. –Pass arrays by passing the pointer. You may need to pass the size of the array as another argument… –Pass an object by constant reference to avoid side effects, and to take the least amount of time and memory.

Fall 2015CISC/CMPE320 - Prof. McLeod6 Aside - Pointers to Functions You can define a pointer to a function. See FunctionsAsPointers.cpp Note that the return type and parameter list must match up. The example does not look useful, but you can also pass function pointers as parameters. The function could be a comparator, for example.

Fall 2015CISC/CMPE320 - Prof. McLeod7 Aside - Pointers to Functions, Cont. This is mostly a C usage. There are better ways to do this including function templates and by taking advantage of polymorphism – this is all C++! See another example: SortAlgorithmTemplateExample.cpp modified from an example in cplusplus.com.

Fall 2015CISC/CMPE320 - Prof. McLeod8 Random File Access Text I/O as discussed before is all sequential access – unidirectional. To open a file for input and output at the same time and to read any position in any order, you need to use random I/O. Same library: fstream.

Fall 2015CISC/CMPE320 - Prof. McLeod9 Random File Access, Cont. To open for input and output, for example: fstream rwStream; rwStream.open(“stuff.txt”, ios::in | ios::out); Last time we used ifstream for input and ofstream for output. fstream extends both of these classes, allowing you to do input and output at the same time.

Random File Access, Cont. File open modes: Combine modes using the binary “or” |. Fall 2015CISC/CMPE320 - Prof. McLeod10 ModeMeaning ios::app Append to end of file and you cannot move anywhere else. ios::ate Starts like append, but then you can move anywhere in the file. ios::in Open for input. ios::out Open for output. ios::trunc Discard contents (default behavior for ios::out). ios::binary Open for binary I/O.

Fall 2015CISC/CMPE320 - Prof. McLeod11 Random File Access, Cont. Imagine two pointers in the file - a read pointer (“get”) and a write pointer (“put”). To read or write you first have to position the pointer at a certain byte position. For example: rwStream.seekp(1000, ios::beg); Positions the write pointer 1000 bytes from the beginning of the file.

Fall 2015CISC/CMPE320 - Prof. McLeod12 Random File Access, Cont. rwStream.seekg(1000, ios::end); Positions the read pointer 1000 bytes from the end of the file. You can also use ios::cur to position the pointer in relative terms. After the seek, you read and write the normal way. But how to know where to position the “pointers”?

Fall 2015CISC/CMPE320 - Prof. McLeod13 Random File Access, Cont. This only works when you have a structured file. You need to know what is stored, in which order it is stored and how big each item is. Remember that you can use the sizeof() operator to get the size of anything – primitive types or objects. This way you could write an entire object to the file and read it back the same way.

Fall 2015CISC/CMPE320 - Prof. McLeod14 Operator Overloading – Java You should remember that the binary + operator is overloaded in Java: is 5 “two” + “three” is “twothree” 2 + “three” is “2three” “two” + 3 is “two3” So the result of the operator depends on the type of the operands. If there is a String literal on one side then the other side is converted to a String and a concatenation takes place.

Fall 2015CISC/CMPE320 - Prof. McLeod15 + and string Objects in C++ string two("two"); string three("three"); string sum1 = two + three; // sum1 is "twothree" string sum2 = 2 + three; // doesn’t work Note that a string literal is actually a char* object and the + operator does not work with them if they are on both sides of the operator.

Fall 2015CISC/CMPE320 - Prof. McLeod16 + and string Objects in C++, Cont. However, something like: string two("two"); string sum1 = two + "five"; does work. The char* literal is converted to a string object first. So, the operation that takes place depends on the types of the operands on either side of the operator.

Fall 2015CISC/CMPE320 - Prof. McLeod17 Operator Overloading This flexibility is a result of operator overloading in the string class – a task carried out by the class designer to make the class easier to use. In Java operator overloading is almost non- existent. For example, if a, b and c are BigIntegers, to carry out the operation: x = a + b * c; you would have to write x = a.add(b.multiply(c));

Fall 2015CISC/CMPE320 - Prof. McLeod18 Overloadable Operators in C++ +-*/%^& |~!=<>+= -=*=/=%=^=&=|= <<>>>>=<<===!=<= >=&&||++--->*, ->[]()newnew[] deletedelete[]

Fall 2015CISC/CMPE320 - Prof. McLeod19 Overloadable Operators, Cont. These include unary operators, binary operators and both bitwise shift and stream insertion operators (already overloaded!). An overloaded operator is only used when one or the other, or both, operands are of your class type. (ie. you cannot overload int + int – sorry…) You cannot create new operators. You cannot change associativity or the order of precedence. You cannot make a unary operator a binary one, or visa-versa.

Fall 2015CISC/CMPE320 - Prof. McLeod20 Overloadable Operators, Cont. And, you only overload the ones that make sense!!!