# 第一章 快速入門 筆記
## 程式原始碼(source)檔案命名慣例(convention)
源碼檔案(source file)的名稱會以一個後綴(suffix)結尾,這後綴是以一個句號(period)後面接著一個或多個字元(characters)所組成,這個後綴會告訴系統檔案這是一個 C++ 程式,不同的編譯器使用不同的後綴慣例(convention),常見的有 .cxx、.cpp、.cc、.C
## 熟悉編譯器
以下僅以 GNU 發行的編譯器(compiler) g++ 為例:
- 編譯(compile):g++ --std=c++11 ch01.cpp -o main
- 執行(execute):./prog1
從程式進入點 main 函式進入後,直到程式結束,會 return 一個數字給作業系統(operation system),如果數字為 0 即代表正常結束,而非 0 則代表程式執行失敗
- 查看程式執行完後的狀態:echo $?
- 編譯多個文件:g++ main.cpp other1.cpp other2.cpp -o main
輸入 g++ --help,查看編譯器可使用的選項(options):
```
Usage: g++ [options] file...
Options:
-pass-exit-codes Exit with highest error code from a phase
--help Display this information
--target-help Display target specific command line options
--help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
Display specific types of command line options
(Use '-v --help' to display command line options of sub-processes)
--version Display compiler version information
-dumpspecs Display all of the built in spec strings
-dumpversion Display the version of the compiler
-dumpmachine Display the compiler's target processor
-print-search-dirs Display the directories in the compiler's search path
-print-libgcc-file-name Display the name of the compiler's companion library
-print-file-name=<lib> Display the full path to library <lib>
-print-prog-name=<prog> Display the full path to compiler component <prog>
-print-multiarch Display the target's normalized GNU triplet, used as
a component in the library path
-print-multi-directory Display the root directory for versions of libgcc
-print-multi-lib Display the mapping between command line options and
multiple library search directories
-print-multi-os-directory Display the relative path to OS libraries
-print-sysroot Display the target libraries directory
-print-sysroot-headers-suffix Display the sysroot suffix used to find headers
-Wa,<options> Pass comma-separated <options> on to the assembler
-Wp,<options> Pass comma-separated <options> on to the preprocessor
-Wl,<options> Pass comma-separated <options> on to the linker
-Xassembler <arg> Pass <arg> on to the assembler
-Xpreprocessor <arg> Pass <arg> on to the preprocessor
-Xlinker <arg> Pass <arg> on to the linker
-save-temps Do not delete intermediate files
-save-temps=<arg> Do not delete intermediate files
-no-canonical-prefixes Do not canonicalize paths when building relative
prefixes to other gcc components
-pipe Use pipes rather than intermediate files
-time Time the execution of each subprocess
-specs=<file> Override built-in specs with the contents of <file>
-std=<standard> Assume that the input sources are for <standard>
--sysroot=<directory> Use <directory> as the root directory for headers
and libraries
-B <directory> Add <directory> to the compiler's search paths
-v Display the programs invoked by the compiler
-### Like -v but options quoted and commands not executed
-E Preprocess only; do not compile, assemble or link
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-o <file> Place the output into <file>
-pie Create a position independent executable
-shared Create a shared library
-x <language> Specify the language of the following input files
Permissible languages include: c c++ assembler none
'none' means revert to the default behavior of
guessing the language based on the file's extension
```
輸入 g++ -v --help 可以看到更完整的指令。以下是其他常用的:
```
-h FILENAME, -soname FILENAME: Set internal name of shared library
-I PROGRAM, --dynamic-linker PROGRAM: Set PROGRAM as the dynamic linker to use
-l LIBNAME, --library LIBNAME: Search for library LIBNAME
-L DIRECTORY, --library-path DIRECTORY: Add DIRECTORY to library search path
```
獲得程式執行完後的狀態:
- windows:echo %ERRORLEVEL%
- unix:echo $?
```bash
g++ file.cpp -o file
file
echo $1
0 # 回傳 0 代表程式正常執行結束
```
註:如使用 windows 的 powershell 而非原生的 cmd,請改為使用 echo $LASTEXITCODE
## 初步認識輸入/輸出(input/output)
標準輸入輸出程式庫定義了四個 IO 物件:標準輸入 cin(standard input)、標準輸出 cout(standard output),標準錯誤 cerr、clog(standard error),而標準錯誤的 cerr 和 clog 之間的差異在於:cerr 沒有緩衝區(buffer),它會直接將輸出印在螢幕上,通常用來顯示錯誤訊息,而 clog 則是用來記錄程式執行過程中的一般資訊
範例:
```cpp
#include <iostream> // 標準函式庫
#include "myheader.h" // 自己額外製作的 header file
int main() {
std::cout << "hello"` int input_num;
std::cin >> input_num;
}
```
注意!運算子(operator)>> 和 << 的運算都是左結合(left associative)的,也就是 << 執行完後,會留下左邊的物件,也就是 cout 和 cin 本身
endl:這是一個被稱作操控符(manipulator)的特殊值,效果是:結束當前所在的行,並且將周邊設備(peripherals)的緩衝區(buffer)中的内容刷(flush)到設備中
unix 和 mac 下鍵盤的輸入文件結束符號(end-of-file),使用 ctrl + d 代表一個文件的結尾,而 windows 中則是使用 ctrl + z
標頭檔(header):類別的類型一般都儲存在標頭檔之中,對於標準函式庫的標頭檔要使用一對角括號(angle brackets)框起來,而非標準庫的標頭則使用雙引號(double quotation marks),宣告要寫在 .h 文件,類別的定義和實作則寫在 .cpp 文件之中
標頭保護(header guard):使用 ifndef、define 巨集(macro)以避免多次包含同一頭文件造成無法編譯的情況
```cpp
#ifndef SALESITEM_H
#define SALESITEM_H
// Definition of Sales_itemclass and related functions goes here
#endif
```
使用點號運算子(dot operator) . 表示我們想用該物件的某個成員,左手邊的運算元(operands)必須是類別型別的一個物件,而右手邊的運算元必須是該型別的一個成員名稱,可以是成員變數(member variable)也可以是成員函式(member function 又稱方法 method)
命名空間(namespace):使用範圍運算子(scope operator) :: 呼叫
## 註解(comments)
- 單行註解:使用雙斜線(double slash)//
- 多行註解:使用 /**/,編譯器會將 /* 和 */ 之間的內容都作為註解內容而忽略
但當一個註解有跨越多行,通常比較好的做法是在視覺上凸顯內部文字行,讓讀者知道它們是一個多行註解的一部分。我們的風格是以一個星號(asterisk)作為註解每一行的開頭,指出該行是屬於多行註解的一部分
```cpp
#define SALESITEM_H
/*
* 多行註解格式
* 每一行的開頭都會加上一個 *
*/
```
## while 述句(while statement)
循環執行,直到條件式(condition)為 false
## for 述句(for statement)
for 述句的標頭(header 此 header 並非指 .h 檔的 header)由三個部分组成:
- 一個初始述句(init-statement)
- 一個條件(condition)
- 一個運算式(expression)
## 使用檔案重導(file redirection)
```bash
./main <infile >outfile
```
- main 是執行檔名稱
- infile 作為資料輸入檔
- outfile 作為資料輸出檔
使用檔案重導這會使原本要印出在螢幕上的文字,改部改為輸入到 outfile 文件中,而原本要從螢幕輸入的資料,也會改為從 infile 中汲取