C++抽象程式設計 Programming Abstracting in C++ 全書筆記(未完結)
此篇文章來源於斯坦福的計算機程式設計課,
網址:http://web.stanford.edu/class/cs106b/
每次重頭開始看書都是看那些基本語法,語法看得太多並沒有任何效果,以後不再複習C的基礎知識,比如變數,語句,迴圈看得再多也還是不會面向物件,以後要看深入的的知識。
簡單摘抄幾個句子。
00. 目錄
1. Overview of C++
1.1 Your first C++ program
1.2 The history of C++
1.3 The structure of a C++ program
1.4 Variables
01. C++常見的的命名規則,屬性和方法小寫開頭,類使用大寫,單詞之間首字母大寫。常量全部大寫,可以用短劃線隔開各個。
In this text, names of variables and functions begin with a lowercase letter, such as limit or raiseToPower. The names of classes and other programmer-defined data types begin with an uppercase letter, as Direction or TokenScanner.
Constant values are written entirely in uppercase, as in PI or HALFJOLLAR. Whenever an identifier consists of serveral English words run together, the usual convention is to capitalize the first letter of each word to make the name easier to read.
Because that strategy doesn't work for constants, programmers use the underscore character to mark the word boundaries.
1.5 Data types
02. 型別
In C++, every data type value has an associated data type. From a formal perspective, a data type is define by two properties: a domain, which is the set of values that belong to that type, and a set of operations, which defines the behavior of that type.
03. 列舉
The syntax for defining an enumerated type is
enum typename {namelist};
Where typename is the name of the new types and namelist is a list of the constants in the domain, separated by commas.
04. 優先順序
If two operators have the same precedence, they are applied in the order spcified by their associativity, which indicates whether that operator group to the left or to the right.
1.6 Expressions
1.7 Statements
Summary
Review question
05. 習題
review
1. source file
2. // /* */
3. <>表示系統標準庫 " "自己寫的標頭檔案
4. const duoble CENTIMETERS_PER_INCH = 2.54;
5. main return 0;
6. 螢幕上換行
7. 名字 型別 數值 範圍
8. c f
9. member operator
10. 表示的範圍不同
11. 美國字元
12. true false
13. double x; cin >> x;
14. cout << "i = " << i << ", d = " << d << ", c = " << c << ", s = " << s << endl;
15. int 5, int 3, double 4.8, double 18, int 4, int 2
16. unary表示負值,substraction表示減法操作。
17. 去掉小數部分
18. 一種型別轉化為另一種型別, type (var)
19. 4
20. var1 += var2
21. ++x 先將x加1再進行操作。
22. short-circuit evaluation 在&& 和 ||中只要計算正確後面不再計算
23. if (boolean) statement;
switch(var) {
case item:
statement; break;
}
24. 判斷一個輸入是否結束的特殊值。
for (int i = 1; i <= 100; i++)
for (int i = 100; i >= 0; i+= 2)
Exercises
2. Functions and Libraries
2.1 The idea of a function
2.2 Libraries
2.3 Defining functions in C++
06. 預設值需要記住的兩點
When you use default parameters, it helps to keep the following rules in mind:
1. The specification of the default value appears only in the function prototype and not in the function definition.
2. Any default parameters must appear at the end of the parameter list.
2.4 The mechanics of function calls
2.5 Reference parameters
07. 將大的程式分成若干小段,同時使用引用傳遞引數,程式結構很清晰。
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
/* Function prototypes */
void getCoefficients(double &a, double &b, double &c);
void solveQuadratic(double a, double b, double c, double &x1, double &x2);
void printRoots(double x1, double x2);
void error(string msg);
int main(int argc, char *argv[])
{
double a, b, c, r1, r2;
getCoefficients(a, b, c);
solveQuadratic(a, b, c, r1, r2);
printRoots(r1, r2);
return 0;
}
// Reads in the coefficients of a quadratic equation into the
// reference paramters a, b, and c.
void getCoefficients(double &a, double &b, double &c) {
cout << "Enter coefficients for the quadratic equation: " << endl;
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
cout << "c: ";
cin >> c;
}
// Solves a quadratic equation for the coefficients a, b, and c. The
// roots are returned in the reference parameters x1 and x2.
void solveQuadratic(double a, double b, double c, double &x1, double &x2) {
if (a == 0) error("The coefficient a must be nonzero.");
double disc = b * b - 4 * a * c;
if (disc < 0) error ("This equation has no real roots");
double sqrtDisc = sqrt(disc);
x1 = (-b + sqrtDisc) / (2 * a);
x2 = (-b - sqrtDisc) / (2 * a);
}
// Displays x1 and x2, which are the roots of the quadratic equation.
void printRoots(double x1, double x2) {
if (x1 == x2)
cout << "There is a double root at " << x1 << endl;
else
cout << "The roots are " << x1 << " and " << x2 << endl;
}
// Writes the string msg to the cerr stream and then exits the program
// with a standard status value indicating that a failure has occurred.
void error(string msg) {
cerr << msg << endl;
exit(EXIT_FAILURE);
}
2.6 Interfaces and Implementations
08. 巨集的使用
#ifndef ERROR_H
#define ERROR_H
#include <string>
void error(std::string msg);
#endif // ERROR_H
In this interface, the boilerplate consists of the #ifndef and #define lines at the beginning of the interface and the matching #endif line at the end.
These lines make sure that the compiler doesn't compile the same interface twice.
09. 實現
#include <iostream>
#include <cstdlib>
#include <string>
#include "error.h"
using namespace std;
void error(string msg) {
cerr << msg << endl;
exit(EXIT_FAILURE);
}
10. 型別
enum Direction {NORTH, EAST, SOUTH, WEST};
The simplest approach to making this type accessible through a library interface would be to write a direction.h interface that contained only this line along with the usual interface that contained only this line along with the usual interface
boilerplate. If you were to adopt that strategy, you wouldn't need to supply an implementation at all.
#ifndef DIRECTION_H
#define DIRECTION_H
#include <string>
enum Direction {NORTH, EAST, SOUTH, WEST};
Direction leftFrom(Direction dir);
Direction rightFrom(Direction dir);
std::string DirectionToString(Direction dir);
#endif // DIRECTION_H
#include <string>
#include "direction.h"
using namespace std;
Direction leftFrom(Direction dir)
{
return Direction((dir + 3)) % 4;
}
Direction rightFrom(Direction dir)
{
return Direction((dir + 1) % 4);
}
string DirectionToString(Direction dir)
{
switch (dir) {
case NORTH:
return "NORTH";
case EAST:
return "EAST";
case SOUTH:
return "SOUTH";
case WEST:
return "WEST";
default:
return "???";
}
}
11. 常量需要在宣告和引用中同時加extern
To export the constant PI, you need to add the keyword extern to both its definition and the prototype declaration in the interface.
extern const double PI;
gmath.cpp檔案中
extern const double PI = 3.14159265358979323846;
2.7 Principles of interface design
12. 程式不要複雜化
To make programming manageable, you must reduce the complexity of the programming process as much as possible.
Functions reduce some of the complexity; libraries offer a similar reduction in programing complexity but at a higher level of detail.
A library gives its client access to a set of functions and types that implement what computer scientists describe as a programming abstraction.
2.8 Designing a random number library
13. 統一
A central feature of a well-designed interface is that is presents a unified and consistent abstraction.
14. 設計一個random函式庫
2.9 Introduction to the Stanford libraries
Summary
Review questions
Exercises
3. Strings
3.1 Using strings as abstract values
3.2 String operations
3.3 The <cctype> library
3.4 Modifying the contents of a string
3.5 The legacy of C-style strings
3.6 Writing string applications
3.7 The strlib.h library
Summary
Review questions
Exercises
4. Streams
4.1 Using strings as abstract values
4.2 Formatted input
4.3 Data files
4.4 Class hierarchies
4.5 The simpio.h and filelib.h libraries
Summary
Review questions
Exercises
5. Collections
5.1 The Vector class
5.2 The Stack class
5.3 The Queue class
5.4 The Map class
5.5 The Set class
5.6 Iterating over a collection
Summary
Review questions
Exercises
6. Designing Classes
6.1 Representing points
6.2 Operator overloading
6.3 Rational numbers
6.4 Designing a token scanner class
6.5 Encapsulating programs as classes
Summary
Review questions
Exercises
7. Introduction to Recursion
7.1 A simple example of recursion
7.2 The factorial function
7.3 The Fibonacci function
7.4 Checking palindromes
7.5 The binary search algorithm
7.6 Mutual recursion
7.7 Thinking recursively
Summary
Review questions
Exercises
8. Recursive Strategies
8.1 The Towers of Hanoi
8.2 The subset-sum problem
8.3 Generating permutations
8.4 Graphical recursion
Summary
Review questions
Exercises
9. Backtracking Algorithms
9.1 Recursive backtracking in a maze
9.2 Backtracking and games
9.3 The minimax algorithm
Summary
Review questions
Exercises
10. Algorithmic Analysis
10.1 The sorting problem
10.2 Computational complexity
10.3 Recursion to the rescue
10.4 Standard complexity classes
10.5 The Quicksort algorithm
10.6 Mathematical induction
Summary
Review questions
Exercises
11. Pointers and Arrays
11.1 The Structure of memory
11.2 Pointers
11.3 Arrays
11.4 Pointer arithmetic
Summary
Review questions
Exercises
12. Dynamic Memory Management
12.1 Dynamic allocation and the heap
12.2 Linked lists
12.3 Freeing memory
12.4 Defining a CharStack class
12.5 Heap-stack diagrams
12.6 Unit testing
12.7 Copying objects
12.8 The use of const
12.9 Efficiency of the CharStack class
Summary
Review questions
Exercises
13. Efficiency and Representation
13.1 Software patterns for editing text
13.2 Designing a simple text editor
13.3 An array-based implementation
13.4 A stack-based implementation
13.5 A list-based implementation
Summary
Review questions
Exercises
14. Linear Structures
14.1 Templates
14.2 Implementing stacks
14.3 Implementing queues
14.4 Implementing vectors
14.5 Integrating prototypes and code
Summary
Review questions
Exercises
15. Maps
15.1 Implementing maps using vectors
15.2 Lookup tables
15.3 Hashing
15.4 Implementing the HashMap class
Summary
Review questions
Exercises
16. Trees
16.1 Family trees
16.2 Binary search trees
16.3 Balanced trees
16.4 Implementing maps using BSTs
16.5 Partially ordered trees
Summary
Review questions
Exercises
17 Sets
17.1 Sets as a mathematical abstraction
17.2 Expanding the set interface
17.3 Implementation strategies for sets
17.4 Optimizing sets of small integers
Summary
Review questions
Exercises
18. Graphs
18.1 The structure of a graph
18.2 Representation strategies
18.3 A low-level graph abstraction
18.4 Graph traversals
18.5 Defining a Graph class
18.6 Finding shortest paths
18.7 Algorithms for searching the web
Summary
Review questions
Exercise
19. Inheritance
19.1 Simple inheritance
19.2 A hierarchy of graphical shapes
19.3 A class hierarchy for expressions
19.4 Parsing an expression
19.5 Multiple inheritance
Summary
Review questions
Exercises
20 Strategies for iteration
20.1 Using iterators
20.2 Using functions as data values
20.3 Encapsulating data with functions
20.4 The STL algorithms library
20.5 Functional programming in C++
20.6 Implementing iterators
Summary
Review questions
Exercises
A Stanford library interfaces
Index
(完結)