Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 02 (Part I) Introduction to C++ Programming.

Similar presentations


Presentation on theme: "Chapter 02 (Part I) Introduction to C++ Programming."— Presentation transcript:

1 Chapter 02 (Part I) Introduction to C++ Programming

2 Goal Introduction C++ Development Environment First Program in C++: –Printing a line of text

3 What Is a Computer? Computer –Device capable of performing computations and making logical decisions Computer programs –Sets of instructions that control computer’s processing of data –Written by people called computer programmers Hardware –Various devices comprising computer Keyboard, screen, mouse, disks, memory, CD-ROM, processing units, etc.

4 Computer Organization Six logical units of computer –Input unit “Receiving” section Obtains information from input devices –Keyboard, mouse, microphone, scanner, networks, etc. –Output unit “Shipping” section Places information processed by computer on output devices –Screen, printer, networks, etc.

5 Computer Organization Six logical units of computer (Cont.) –Memory unit Rapid access, relatively low capacity. Retains information from input unit. –Immediately available for processing Retains processed information. –Until placed on output devices Often called memory or primary memory. –Arithmetic and logic unit (ALU) Performs arithmetic calculations and logic decisions

6 Computer Organization Six logical units of computer (Cont.) –Central processing unit (CPU) “Administrative” section Coordinates and supervises other sections of computer –Secondary storage unit Long-term, high-capacity “warehouse” section Stores inactive programs or data Secondary storage devices –Hard drives, CDs, DVDs Slower to access than primary memory Less expensive per unit than primary memory

7 Computer Languages Three types of computer languages –Machine language Only language computer directly understands Generally consist of strings of numbers –Ultimately 0s and 1s Instruct computers to perform elementary operations Cumbersome for humans Example –+1300042774 +1400593419 +1200274027

8 Computer Languages –Assembly language English-like abbreviations representing elementary computer operations Clearer to humans Incomprehensible to computers –Convert to machine language by translator programs (assemblers) Example loadbasepay add overpay store grosspay

9 Computer Languages –High-level languages Similar to everyday English –Uses common mathematical notations Single statements accomplish substantial tasks Compilers –Converted to machine language by translator programs Example –grossPay = basePay + overTimePay

10 History of C –Evolved from BCPL and B Developed by Dennis Ritchie (Bell Laboratories) –Development language of UNIX –Hardware independent Can write portable programs –ANSI and ISO standard for C published in 1990 ANSI/ISO 9899: 1990

11 History of C++ –Extension of C Developed by Bjarne Stroustrup (Bell Laboratories) in e arly 1980s –Provides new features to “spruce up” C –Provides capabilities for object-oriented programming Objects: reusable software components –Model items in the real world Object-oriented programs –Easier to understand, correct and modify

12 C++ Development Environment Microsoft Visual Studio 2005 (VS 2005) – 開始 → 程式集 →Microsoft Visual Studio 2005

13 Step 1 檔案 → 新增 → 專案 … (1) (2) (3) (4) (5)

14 Step 2 請尋找「方案總管」 ( 檢視 → 方案總管 ) – 在原始程式檔上右按滑鼠,選擇「加入 → 新增項目」

15 Step 3 選擇 (1) 程式碼, (2) C++ 檔,並 (3) 輸入檔名 (main.cpp) (1) (2) (3)

16 Step 4 在中央的編輯區中(上方會顯示你正編輯的檔名) 輸入程式:

17 Step 5 在編輯過程中,不要忘記存檔的動作(把存檔當習慣) 編譯並執行程式 –Ctrl+F5 (非偵錯模式) – 偵錯模式(可以追蹤出錯的程式碼) 編譯( compile ):把程式語言「翻譯」成機器指令碼。 執行( execute ):將機器指令碼載入記憶體, CPU 開始逐行 執行指令。

18 Note 如果在編譯的過程中,有出現錯誤,編譯器會指出錯誤 的位置;請仔細檢查拼字錯誤。 按「否」,以修 正錯誤 在錯誤上方點兩下,會顯示出錯誤發生「大約」的位置。

19 2.2 First Program in C++

20 Printing a Line of Text Ctrl+F5

21 Good Programming Practice Every program should begin with a comment that describes the purpose of the program. Use blank lines and space characters to enhance program readability. Lines beginning with “//” are comments.

22 The Basic Framework of a Program The main function is where a C++ program start to execute.

23 Statements –Note: Missing the semicolon at the end of a C++ statement is a syntax error. Instruct the program to perform an action All statements end with a semicolon (;)

24 Statements –std::cout Specifying cout is belongs to “namespace” std. –std::cout Standard output stream object. “Connected” to screen. –Stream insertion operator “<<“ Inserting right operand into left operand. Instruct the program to perform an action. All statements end with a semicolon (;)

25 String Constant String is composed of characters. A string constant is enclosed by double quotation marks ("). Escape characters –A character preceded by "\" Indicates “special” character output –Example "\n" welcometoc++!\n

26 Escape sequences.

27 More Examples of String Constants “Hello,\nWorld!” Hello, World! “Hello, World!\n” Hello, World! “My name is\nJoe.” My name is Joe.

28 Exercise


Download ppt "Chapter 02 (Part I) Introduction to C++ Programming."

Similar presentations


Ads by Google