Presentation is loading. Please wait.

Presentation is loading. Please wait.

06 – Software Design P.9 Input / Output Using Streams

Similar presentations


Presentation on theme: "06 – Software Design P.9 Input / Output Using Streams"— Presentation transcript:

1 06 – Software Design P.9 Input / Output Using Streams
1.1 The Software Life Cycle 1.2 Using Abstraction to Manage Complexity 06 – Software Design

2 Attendance Quiz #5 Software Design

3 Tip #6: Local Declarations
Software Design Should I declare locals in the middle of a function or at the top?  First, declare an object near first use. Improves readability by making it easy to find the definition. Easier to give useful initial value. Second, if you don’t have enough information to initialize an object until half way down the function, you should create it half way down the function when it can be initialized correctly. Initializing an object to an “empty” value at the top and then “assigning” it later results in a runtime performance hit. Building an object correctly is faster than building it incorrectly and remodeling it later. (e.g., a factor of 350% speed hit for simple classes like String. Bottom line: Locals should be declared near their first use and preferably to a meaningful value.

4 P.9 Input / Output Using Streams
Console Input / Output Input Streams Output Streams Formatting Output Using I/O Manipulators File Streams openmode String Streams P.8, pgs

5 Stream Class Hierarchy
C++ Primer Input Output

6 <fstream> Header
Software Design Class templates basic_ifstream Input file stream (class template) basic_ofstream Output file stream (class template) basic_fstream File stream (class template) basic_filebuf File stream buffer (class template) Classes Narrow characters (char) ifstream Input file stream class (class) ofstream Output file stream (class) fstream Input/output file stream class (class) filebuf File stream buffer (class) Wide characters (wchar_t) wifstream Input file stream (wide) (class) wofstream Output file stream (wide) (class) wfstream File stream (wide) (class) wfilebuf File stream buffer (wide) (class)

7 Output File Stream #include <iostream> #include <fstream>
C++ Primer #include <iostream> #include <fstream> #include <string> using namespace std; int main () { } string outFile = "myOutputFile.txt"; ofstream out(outFile); if (out.is_open()) { out << "This is line 1" << endl; out << "This is line 2" << endl; out.close(); } else cout << "Unable to open " << outFile; return 0;

8 Input File Stream #include <iostream> #include <fstream>
C++ Primer #include <iostream> #include <fstream> #include <string> using namespace std; int main (int argc, char* argv[]) { } string line; ifstream in(argv[1]); if (in.is_open()) { while (getline(in, line)) cout << line << endl; } in.close(); else cout << "Unable to open file " << argv[1]; return 0;

9 I/O Manipulators C++ Primer C++ provides various stream manipulators that perform formatting tasks. Stream manipulators are defined in <iomanip>. Manipulators are functions specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects. These manipulators provide capabilities for setting field widths (setw), setting precision (setprecision), setting and unsetting format flags, flushing streams, inserting a "newline" and flushing output stream, skipping whitespace in input stream

10 String Streams #include <iostream> #include <sstream>
C++ Primer #include <iostream> #include <sstream> using namespace std; int countWords(string str) { stringstream s(str); // Used for breaking words string word; // to store individual words int count = 0; while (s >> word) count++; cout << word << endl; } return count; int main() string s = "Now is the time for all good men."; cout << "Number of words: " << countWords(s); return 0;

11 Use cout if no output file is specified (argv[2]).
File or Console C++ Primer #include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, char* argv[]) { ifstream in(argv[1]); if (!in) return 1; ostream& out = (argc > 2) ? *(new ofstream(argv[2])) : cout; if (!out) return 2; while (!in.eof()) string line; getline(in, line); out << line << endl; } if (&out != &cout) delete(&out); in.close(); return 0; Use cout if no output file is specified (argv[2]).

12 1.1, pgs. 64-72 1.1 The Software Life Cycle Software Life Cycle Models
Software Life Cycle Activities Requirements Specification Analysis Design 1.1, pgs

13 The Software Life Cycle
Software Design The purpose of a typical college course programming assignment is to provide experience with a particular programming concept. In contrast, an industrial software product is likely to be used over an extended period by individuals other than the creator(s) of the product. A software product goes through stages called the software life cycle. Planning Requirements Analysis and Design Implementation Unit and Integration Testing Operation and Maintenance.

14 Software Development Software Design Waterfall Spiral Agile Extreme

15 Unified or Core Flow Model
Software Design The Unified Model attempts to resolve problems of the waterfall model. The unified model divides the problem into cycles (called phases or iterations) similar to mini-versions of the waterfall model. At the end of each cycle a review with the users takes place to obtain feedback which is taken into account in the next cycle.

16 Top-Down vs. Bottom-Up Design
Software Design Top-down design (also called stepwise refinement) starts at the top level (the original problem) and divides it into sub-problems. For each sub-problem, it identifies a sub-system for solving that sub-problem. Bottom-up design starts at the bottoms level and builds solutions. While goals for a product are still outlined, the assembly of a product is done on a system by system basis.

17 Object-Oriented Design
Software Design Unlike top-down design which focuses on actions and bottom-up design which focuses on data elements, object-oriented design (OOD) focuses on data and operations together. In OOD, we identify the objects that participate in our problem (whose common characteristics form a class) then identify how the objects interact to form a solution (the interaction occurs through messages) provide an operator for each class (usually a function) to process the message. Usually looking at the nouns in the problem statement often helps us identify objects; verbs often indicate operators. OOD is a combination of both top-down and bottom-up design.

18 UML as a Design Tool Software Design We use Unified Modeling LanguageTM (UML) diagrams as a design tool to illustrate the interaction between classes and between classes and external entities (often users, but not always). UML diagrams are a standard means of documenting class relationships and are widely used in industry.

19 1.2, pgs. 73-74 1.2 Using Abstraction to Manage Complexity
Procedural Abstraction Data Abstraction Information Hiding 1.2, pgs

20 Abstraction Software Design Abstraction is a powerful technique that helps programmers (or problem solvers) deal with complex issues in a piecemeal fashion. Procedural Abstraction is the concept that procedural development should separate the concern of what is to be achieved by a procedure from the details of how it is achieved. Data abstraction specifies the data objects for a problem and the operations to be performed on these data objects without being overly concerned with how the data objects will be represented and stored in memory.

21 Information Hiding Software Design Procedural abstraction and data abstraction enable the designer to make implementation decisions in a piecemeal fashion. The designer can postpone making decisions regarding the actual internal representation of the data objects and the implementation of its operators. The overall complexity can be controlled by separating top- level design from lower-level implementation. If the details of a data object's implementation are hidden from the higher-level module (a C++ class), the higher-level class can access the data object only through its member functions. This is an advantage because it allows internal changes to a function without having to rewrite the higher-level class. This process of "hiding" the details of a class's implementation is called information hiding.

22


Download ppt "06 – Software Design P.9 Input / Output Using Streams"

Similar presentations


Ads by Google