monday, mar 31, 2003kate gregory with material from deitel and deitel week 12 labs 4 and 5 are back...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/1.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Week 12
• Labs 4 and 5 are back
• File IO
• Looking ahead to the final
![Page 2: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/2.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Date Week Topic ChapterHand Out Due Back Test
6-Jan-03 1Administrivia / Overview / Intro to C++ / Control Structures 1
13-Jan-03 2 Functions / Arrays, Pointers, Strings 2,3,4,5Lab 1 / Lab 2
20-Jan-03 3 Classes, Data Abstraction 6 Lab 1 5%27-Jan-03 4 More on Classes 7 Lab 3 Lab 2 5%3-Feb-03 5 No Lecture
10-Feb-03 6 Operator Overloading 8 Lab 4 Lab 3 5%17-Feb-03 Reading Break24-Feb-03 7 Inheritance 9 Lab 4 5% Midterm 25%3-Mar-03 8 Virtual Functions and Polymorphism 10 Lab 5
10-Mar-03 9 Stream IO 11 Lab 6 Lab 5 5%17-Mar-03 10 Templates 12,13 Lab 7 Lab 6 5%24-Mar-03 11 Exceptions Lab 7 5%31-Mar-03 12 File IO 1417-Apr-03 Exam Final 40%
![Page 3: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/3.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Lab 4
• Use const every chance you get– The compiler will let you know if it’s wrong– But don’t bother with foo(const int i)
• Reuse, reuse, reuse• Floating point division is expensive and
inaccurate– The point of a Fraction class is to do better than
that
![Page 4: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/4.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Lab 5
• Protected methods are your best choice when the derived classes really need access to a base class variable
• Use const, use initializer syntax, delegate to the base class
• Choose good test data• Classes with a virtual function need a
virtual destructor
![Page 5: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/5.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
File IO
• Based on the stream IO you already know
• Include <fstream> as well as <iostream>
• The operator << and >> overloads you write will work with files just as with cout and cin
![Page 6: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/6.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Writing text to a file#include <iostream>#include <fstream>using namespace std;
int main(){ofstream outputfile("out.txt");outputfile << "Hi there" << endl;
return 0;}
![Page 7: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/7.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Opening and closing• The constructor opens the file• The destructor closes it• You can close it yourself if you need to –
for example to make sure that others can read the file even while your application is still running
• You can use the default constructor, then open it yourself later by calling open()
![Page 8: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/8.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Reading text from a file
ifstream inputfile("out.txt");
char input [100];
inputfile.getline(input, 100);
cout << "file contains:"
<< endl;
cout << input << endl;
![Page 9: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/9.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Working with your operatorsEmployee e( -- whatever -- );cout << e;
outputfile << e; // uses your operator
• You might write a different operator for ofstream and format the output differently
• You’re the programmer!
![Page 10: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/10.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Open modes• The defaults are good for most people, but you
might want to use some of the “open modes”– ios::app– ios::ate (position at end, can move)– ios::in– ios::out– ios::trunc (out and trunc both toss whatever is already
in the file)– ios::binary
• You can combine these with bitwise or |
![Page 11: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/11.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Opening for append
ofstream outputfile("out.txt",
ios::app);• Everything will be written after what is
already in the file
• Good for logs
![Page 12: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/12.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Looping through a fileifstream inputfile("out.txt");
char input [100];
cout << "file contains:" << endl;
while (!inputfile.eof())
{
inputfile.getline(input, 100);
cout << input << endl;
}
![Page 13: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/13.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
Class Topics• Control Structures, Functions• Arrays, Pointers, Strings• Classes, Data Abstraction• Operator Overloading• Inheritance• Virtual Functions and Polymorphism• Stream IO• Templates• Exceptions• File IO
![Page 14: 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](https://reader036.vdocuments.net/reader036/viewer/2022083005/56649f265503460f94c3d7df/html5/thumbnails/14.jpg)
Monday, Mar 31, 2003 Kate Gregorywith material from Deitel and Deitel
There is no next class
• Get ready for the final!– Thursday April 17th, 9am-noon, AC