tailieunhanh - Lecture Programming principles and practice using C++: Chapter 7 - Bjarne Stroustrup
This chapter spends a lot of time of the structure and looks of code; that is, factors that affect comprehension and maintainability. It is a good idea to remind the students that often they are the maintainers (maybe of their own code, a few months after they first wrote it). A useful program is never finished: it will be ported, corrected, extended, etc. | Chapter 7 Completing a Program Bjarne Stroustrup Abstract Tokens and token streams Structs and classes Cleaning up the code Prompts Program organization constants Recovering from errors Commenting Code review Testing A word on complexity and difficulty Variables Stroustrup/Programming/2015 Completing the calculator Now wee need to Complete the implementation Token and Token_stream Get the calculator to work better Add features based on experience Clean up the code After many changes code often become a bit of a mess We want to produce maintainable code Stroustrup/Programming/2015 Token We want a type that can hold a “kind” and a value: struct Token { // define a type called Token char kind; // what kind of token double value; // used for numbers (only): a value }; // semicolon is required Token t; = '8'; // . (dot) is used to access members // (use ‘8’ to mean “number”) = ; Token u = t; // a Token behaves much like a . | Chapter 7 Completing a Program Bjarne Stroustrup Abstract Tokens and token streams Structs and classes Cleaning up the code Prompts Program organization constants Recovering from errors Commenting Code review Testing A word on complexity and difficulty Variables Stroustrup/Programming/2015 Completing the calculator Now wee need to Complete the implementation Token and Token_stream Get the calculator to work better Add features based on experience Clean up the code After many changes code often become a bit of a mess We want to produce maintainable code Stroustrup/Programming/2015 Token We want a type that can hold a “kind” and a value: struct Token { // define a type called Token char kind; // what kind of token double value; // used for numbers (only): a value }; // semicolon is required Token t; = '8'; // . (dot) is used to access members // (use ‘8’ to mean “number”) = ; Token u = t; // a Token behaves much like a built-in type, such as int // so u becomes a copy of t cout '8' '+' Stroustrup/Programming/2015 Token struct Token { // user-defined type called Token char kind; // what kind of token double value; // used for numbers (only): a value }; Token{‘+’}; // make a Token of “kind” ‘+’ Token{'8',}; // make a Token of “kind” ‘8’ and value A struct is the simplest form of a class “class” is C++’s term for “user-defined type” Defining types is the crucial mechanism for organizing programs in C++ as in most other modern languages a class (including structs) can have data members (to hold information), and function members (providing operations on the data) Stroustrup/Programming/2015 Token_stream A Token_stream reads characters, producing Tokens on demand We can put a Token into a Token_stream for later use A Token_stream uses a “buffer” to hold tokens we put back into it 1+2*3; empty Token_stream buffer: Input stream: For 1+2*3;, .
đang nạp các trang xem trước