cs162 introduction to computer science ii
play

CS162: Introduction to Computer Science II Streams II 1 - PDF document

CS162: Introduction to Computer Science II Streams II 1 Inheritance and Streams We say class B is derived from class A if: Class B is a more specific type of Class A Both classes have some things in common Class B has more


  1. CS162: Introduction to Computer Science II Streams II 1 Inheritance and Streams We say class B is derived from class A if: • Class B is a more specific type of Class A • Both classes have some things in common • Class B has more features 2 1

  2. Inheritance and Streams Examples from the real-world: • Cat has a derived from relationship with Animal • Car has a derived from relationship with Vehicle • Electrical Engineering has a derived from relationship with Engineering 3 Inheritance and Streams More specific More general Cat Derived From Animal Car Vehicle Derived From Electrical Engineering Derived From Engineering Child Parent 4 2

  3. Inheritance and Streams C++ example #1 ifstream Derived From istream Child Parent • ifstream is for files • istream is for input streams (eg. cin is an object of type istream) • Shared behavior: can use >> on both • Behavior specific to ifstream : can use open() and close() on ifstream but not on istream Inheritance and Streams C++ example #1 ifstream Derived From istream Child Parent • We say ifstream is derived from istream • An ifstream object has all the properties of an istream object • An ifstream object is also an object of type istream 3

  4. Inheritance and Streams C++ example #2 ofstream Derived From ostream Child Parent • ofstream is for files • ostream is for output streams (eg. cout and cerr are objects of type ostream) • Shared behavior: can use << on both • Behavior specific to ofstream : can use open() and close() on ofstream but not on ostream Inheritance and Streams C++ example #2 ofstream Derived From ostream Child Parent • We say ofstream is derived from ostream • An ofstream object has all the properties of an ostream object • An ofstream object is also an object of type ostream 4

  5. Inheritance and Streams void foo1(ifstream& sourceFile) { int n1, n2; sourceFile >> n1 >> n2; cout << n1 << “ + “ << n2 << “ = “ << (n1 + n2) << endl; } ifstream fin; fin.open(“input.txt”); foo1(fin); /* Works */ foo1(cin); /* Doesn’t work because cin is not an ifstream */ 9 Inheritance and Streams void foo2(istream& sourceFile) { int n1, n2; sourceFile >> n1 >> n2; cout << n1 << “ + “ << n2 << “ = “ << (n1 + n2) << endl; } ifstream fin; fin.open(“input.txt”); foo2(fin); /* Works */ foo2(cin); /* Works */ 10 5

  6. The stringstream class 11 stringstream • This is a handy class for creating strings • stringstream derived from iostream which is derived from istream 12 6

  7. stringstream #include <sstream> #include <string> #include <iostream> int main(int argc, char** argv) { std::stringstream ss; ss.clear(); ss.str("1. "); float c = 9.99; ss << "Macho burrito: $" << c; std::cout << ss.str() << std::endl; return 0; } 13 stringstream #include <sstream> #include <string> Must include this header to #include <iostream> use stringstream int main(int argc, char** argv) { std::stringstream ss; ss.clear(); ss.str("1. "); float c = 9.99; ss << "Macho burrito: $" << c; std::cout << ss.str() << std::endl; return 0; } 14 7

  8. stringstream #include <sstream> #include <string> #include <iostream> int main(int argc, char** argv) { std::stringstream ss; ss.clear(); Clears error ss.str("1. "); statuses float c = 9.99; ss << "Macho burrito: $" << c; std::cout << ss.str() << std::endl; return 0; } 15 stringstream #include <sstream> #include <string> #include <iostream> int main(int argc, char** argv) { std::stringstream ss; ss.clear(); Sets the string ss.str("1. "); in stringstream float c = 9.99; ss << "Macho burrito: $" << c; std::cout << ss.str() << std::endl; return 0; } 16 8

  9. stringstream #include <sstream> #include <string> #include <iostream> int main(int argc, char** argv) { std::stringstream ss; Returns the value ss.clear(); of the stringstream ss.str("1. "); as a string float c = 9.99; ss << "Macho burrito: $" << c; std::cout << ss.str() << std::endl; return 0; } 17 stringstream The code in the previous slides prints out: 1. Macho burrito: $9.99 You can also use stringstreams to parse strings (see next slide) 18 9

  10. stringstream #include <sstream> #include <string> #include <iostream> int main(int argc, char** argv) { int s1, s2, s3; std::stringstream ss("100 80 50"); ss >> s1; ss >> s2; ss >> s3; std::cout << (s1+s2+s3)/3.0 << std::endl; return 0; } 19 stringstream #include <sstream> #include <string> #include <iostream> int main(int argc, char** argv) { int s1, s2, s3; std::stringstream ss("100 80 50"); ss >> s1; ss >> s2; This reads: ss >> s3; 100 into s1, std::cout << (s1+s2+s3)/3.0 << std::endl; 80 into s2, return 0; 50 into s3 } 20 10

  11. stringstream The code in the previous slide prints out: 76.6667 21 11

Recommend


More recommend