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

cs162 introduction to computer science ii
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

1

CS162: Introduction to Computer Science II

Streams II

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

slide-2
SLIDE 2

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

4

More specific More general Cat Animal Car Vehicle Electrical Engineering Engineering

Derived From Derived From Derived From

Child Parent

slide-3
SLIDE 3

3

Inheritance and Streams

C++ example #1

ifstream istream

Derived From

  • ifstream is for files
  • istream is for input streams (eg. cin is an
  • bject of type istream)
  • Shared behavior: can use >> on both
  • Behavior specific to ifstream: can use open()

and close() on ifstream but not on istream

Child Parent

Inheritance and Streams

C++ example #1

ifstream istream

Derived From

  • 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

Child Parent

slide-4
SLIDE 4

4

Inheritance and Streams

C++ example #2

  • fstream
  • stream

Derived From

  • 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

Child Parent

Inheritance and Streams

C++ example #2

  • fstream
  • stream

Derived From

  • We say ofstream is derived from ostream
  • An ofstream object has all the properties of an
  • stream object
  • An ofstream object is also an object of type
  • stream

Child Parent

slide-5
SLIDE 5

5

Inheritance and Streams

void foo1(ifstream& sourceFile) { int n1, n2; sourceFile >> n1 >> n2; cout << n1 << “ + “ << n2 << “ = “ << (n1 + n2) << endl; }

9

ifstream fin; fin.open(“input.txt”); foo1(fin); /* Works */ foo1(cin); /* Doesn’t work because cin is not an ifstream */

Inheritance and Streams

void foo2(istream& sourceFile) { int n1, n2; sourceFile >> n1 >> n2; cout << n1 << “ + “ << n2 << “ = “ << (n1 + n2) << endl; }

10

ifstream fin; fin.open(“input.txt”); foo2(fin); /* Works */ foo2(cin); /* Works */

slide-6
SLIDE 6

6

The stringstream class

11

stringstream

  • This is a handy class for creating strings
  • stringstream derived from

iostream which is derived from istream

12

slide-7
SLIDE 7

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> #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; }

14

Must include this header to use stringstream

slide-8
SLIDE 8

8

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; }

15

Clears error statuses

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; }

16

Sets the string in stringstream

slide-9
SLIDE 9

9

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; }

17

Returns the value

  • f the stringstream

as a string

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

slide-10
SLIDE 10

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; ss >> s3; std::cout << (s1+s2+s3)/3.0 << std::endl; return 0; }

20

This reads: 100 into s1, 80 into s2, 50 into s3

slide-11
SLIDE 11

11

stringstream

The code in the previous slide prints out: 76.6667

21