1 / 28

Chapter 02 (Part I)

Chapter 02 (Part I). Introduction to C++ Programming. Goal. Introduction C++ Development Environment First Program in C++: Printing a line of text. What Is a Computer?. Computer Device capable of performing computations and making logical decisions Computer programs

becca
Download Presentation

Chapter 02 (Part I)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 load basepayadd overpaystore 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 • 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++ • History of C++ • Extension of C • Developed by Bjarne Stroustrup (Bell Laboratories) in early 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) (2) (1) (3)

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

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

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

  19. 2.2 First Program in C++

  20. Ctrl+F5 Printing a Line of Text

  21. Lines beginning with “//” are comments. 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.

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

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

  24. Statements • Instruct the program to perform an action. • All statements end with a semicolon (;) • 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.

  25. w e l c o m e t o c + + ! \n 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"

  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

More Related