Container Classes & Exception Handling

Passing Streams to Functions http://cs.mst.edu Passing Streams to Functions One Rule: always pass a stream as a reference http://cs.mst.edu file: fileopen.h // Pre: template parameter T must be either ifstream or ofstream type. template

void fileopen (T & filestr, const string promptpart) { const int MAX_TRIES = 5; int count = 0; string filename; cout<>filename; filestr.open(filename.c_str()); while (!filestr) { filestr.clear(); // may be necessary on your platform cout<>filename; filestr.open(filename.c_str());

count++; if (count > MAX_TRIES) { cout< #include fileopen.h int main() {

ifstream in; fileopen(in, input); http://cs.mst.edu Operator Overloading ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } http://cs.mst.edu Operator Overloading ostream& operator << (ostream & out, const point & p)

{ out << ( << p.m_X << , << p.m_Y << ); return out; } // example 1 point p1, p2; cout << p1; cout << p1 << " << p2; http://cs.mst.edu Operator Overloading ostream& operator { out << ( << return out;

} // example 1 point p1, p2; cout << p1; cout << p1 << // example 2 ofstream fout; fout << p1; fout << p1 << << (ostream & out, const point & p) p.m_X << , << p.m_Y << ); << p2; << p2;

http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } Stream Processing...

http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } Stream Processing... cout << p1

http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } Stream Processing... cout << p1 executes overloaded operator<<

http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } Stream Processing... cout << p1

executes overloaded operator<< returns cout with the points data added to the stream http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; }

Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << );

return out; } System Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << returns cout with the space added to the stream http://cs.mst.edu Chaining // example 1 cout << p1 << << p2;

... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << returns cout with the space added to the stream cout << p2 http://cs.mst.edu

Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out; } Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream

cout << returns cout with the space added to the stream cout << p2 executes overloaded operator<< http://cs.mst.edu Chaining // example 1 cout << p1 << << p2; ... ostream& operator << (ostream & out, const point & p) { out << ( << p.m_X << , << p.m_Y << ); return out;

} Stream Processing... cout << p1 executes overloaded operator<< returns cout with the points data added to the stream cout << returns cout with the space added to the stream cout << p2 executes overloaded operator<< returns cout with the points data added to the stream http://cs.mst.edu Final Note iostream and fstream are of the same family

getline, ignore, get, putback, etc are all available for filestreams as well! ifstream fin; char input; fin.open(input.dat); while (in.get(input)) { process_data(input); http://cs.mst.edu End of Session http://cs.mst.edu

Recently Viewed Presentations

  • Monday, 4-5:30 pm McGuinn 121 (cont.) Cyril Orji,

    Monday, 4-5:30 pm McGuinn 121 (cont.) Cyril Orji,

    Lonergan's Challenge for the Last 50th Anniversary Year of Vatican II: Healing & Creating in History In Memoriam: William E. Murnion and Peter Corbishley Schedule of Probabilities SUNDAY, JUNE 14 Sunday Evening, 7pm McGuinn 121 John Haughey, SJ Columbiere Community,...
  • Stem-Changing Verbs (e to ie)

    Stem-Changing Verbs (e to ie)

    Stem-changing verbs are also called "shoe verbs" or "boot verbs" because of the pattern of spelling changes. pensar (to think) (e to ie) Note the spelling change only occurs in the "boot" shape Verbs with a stem-change will often be...
  • Chapter - 4 Structure of Atom

    Chapter - 4 Structure of Atom

    It will try to attain stability by losing, gaining or sharing electrons with other atoms to attain octet configuration. The number of electrons lost, gained or shared by an atom to attain octet configuration is the combining capacity or valency...
  • The 13 British Colonies (1689-1754)

    The 13 British Colonies (1689-1754)

    An almanac is a book containing information such as calendars, weather predictions, and advice. Farmers. Farms in the colonies varied in size from large cash-crop plantations in the South to small, self-supporting farms in the Middle and New England Colonies....
  • The Case For Passive Investing

    The Case For Passive Investing

    Flatter or negative sloping term yield curves seem to correspond to lower PE ratios and upwards sloping yield curves to higher PE ratios. Using the Regression to gauge the market… We can use the regression to predict E/P ratio in...
  • ACT Reading Preparation

    ACT Reading Preparation

    ABBI SAW A WUWU After Before Because If Since Although When(ever) These twelve words are the second KEY to correctly placing commas. They are easy to remember, so MEMORIZE them. Once you RECOGNIZE them in a sentence, you can evaluate...
  • SANBI 2018/19 AUDITED ANNUAL FINANCIAL STATEMENTS 1 AUDIT

    SANBI 2018/19 AUDITED ANNUAL FINANCIAL STATEMENTS 1 AUDIT

    Services were for supply of ablution facilities at the Pretoria campus. Disciplinary taken. 58 083. Non-compliance with SCM policy and Treasury Regulation 16A6.1. Services rendered without following a proper procurement process. Services were for supply of ablution facilities at the...
  • Healthy Eating and Living Parts 4, 5 Review

    Healthy Eating and Living Parts 4, 5 Review

    Ecology Food Chain Lesson Bundle. Ecology Feeding Levels Unit Notes. Biomagnification, Bioaccumulation of Pollution, Food Chain Lesson Bundle. Ecology Feeding Levels, Pyramid of Biomass, Number Lesson Bundle. Animal Dentition Lesson Bundle. Ecology Feeding Levels Unit Review Game. Ecology Feeding Levels...