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

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

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

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

Chapter 02 (Part I)

Introduction to C++ Programming

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

Goal

• Introduction

• C++ Development Environment

• First Program in C++:– Printing a line of text

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

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.

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

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.

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

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

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

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

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

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

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

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)

• Exampleload basepayadd overpaystore grosspay

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

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

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

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

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

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 progr

amming• Objects: reusable software components

– Model items in the real world

• Object-oriented programs– Easier to understand, correct and modify

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

C++ Development Environment• Microsoft Visual Studio 2005 (VS 2005)

– 開始→程式集→ Microsoft Visual Studio 2005

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

Step 1• 檔案→新增→專案…

(1)

(2)

(3)

(4)

(5)

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

Step 2• 請尋找「方案總管」 ( 檢視→方案總管 )

– 在原始程式檔上右按滑鼠,選擇「加入→ 新增項目」

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

Step 3

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

(1) (2)

(3)

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

Step 4• 在中央的編輯區中(上方會顯示你正編輯的檔

名)輸入程式:

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

Step 5• 在編輯過程中,不要忘記存檔的動作(把存檔當習慣)

• 編譯並執行程式– Ctrl+F5 (非偵錯模式)– 偵錯模式(可以追蹤出錯的程式碼)

• 編譯( compile ):把程式語言「翻譯」成機器指令碼。• 執行( execute ):將機器指令碼載入記憶體, CPU 開始逐

行執行指令。

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

Note

• 如果在編譯的過程中,有出現錯誤,編譯器會指出錯誤的位置;請仔細檢查拼字錯誤。

按「否」,以修正錯誤

在錯誤上方點兩下,會顯示出錯誤發生「大約」的位置。

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

2.2 First Program in C++

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

Printing a Line of Text

Ctrl+F5

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

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.

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

The Basic Framework of a Program

The main function is where a C++ program start to execute.

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

Statements

– Note:• Missing the semicolon at the end of a C++ statement is a syn

tax error.

• Instruct the program to perform an action• All statements end with a semicolon (;)

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

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 (;)

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

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"

w e l c o m e t o c + + ! \n

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

Escape sequences.

Escape sequence

Description

\n Newline. Position the screen cursor to the beginning of the next line.

\t Horizontal tab. Move the screen cursor to the next tab stop.

\r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.

\a Alert. Sound the system bell.

\\ Backslash. Used to print a backslash character.

\' Single quote. Use to print a single quote character.

\" Double quote. Used to print a double quote character.

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

More Examples of String Constants

• “Hello,\nWorld!”Hello,

World!

• “Hello, World!\n”Hello, World!

• “My name is\nJoe.”My name is

Joe.

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

Exercise