Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel

3 Monday, Mar 31, 2003Kate Gregory with 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

4 Monday, Mar 31, 2003Kate Gregory with 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

5 Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel File IO Based on the stream IO you already know Include as well as The operator > overloads you write will work with files just as with cout and cin

6 Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Writing text to a file #include using namespace std; int main() { ofstream outputfile("out.txt"); outputfile << "Hi there" << endl; return 0; }

7 Monday, Mar 31, 2003Kate Gregory with 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()

8 Monday, Mar 31, 2003Kate Gregory with 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;

9 Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Working with your operators Employee 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!

10 Monday, Mar 31, 2003Kate Gregory with 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 |

11 Monday, Mar 31, 2003Kate Gregory with 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

12 Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Looping through a file ifstream inputfile("out.txt"); char input [100]; cout << "file contains:" << endl; while (!inputfile.eof()) { inputfile.getline(input, 100); cout << input << endl; }

13 Monday, Mar 31, 2003Kate Gregory with 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

14 Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel There is no next class Get ready for the final! –Thursday April 17 th, 9am-noon, AC


Download ppt "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."

Similar presentations


Ads by Google