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
– 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.
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.
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
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
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
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
add
store
basepay
overpay
grosspay
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
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
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
C++ Development Environment
• Microsoft Visual Studio 2005 (VS 2005)
– 開始→程式集→Microsoft Visual Studio 2005
Step 1
• 檔案→新增→專案…
(1)
(2)
(3)
(4)
(5)
Step 2
• 請尋找「方案總管」(檢視→方案總管)
– 在原始程式檔上右按滑鼠,選擇「加入→ 新增項目」
Step 3
• 選擇(1) 程式碼,(2) C++檔,並(3) 輸入檔名
(main.cpp)
(2)
(1)
(3)
Step 4
• 在中央的編輯區中(上方會顯示你正編輯的檔名)
輸入程式:
Step 5
• 在編輯過程中,不要忘記存檔的動作(把存檔當習慣)
• 編譯並執行程式
– Ctrl+F5(非偵錯模式)
– 偵錯模式(可以追蹤出錯的程式碼)
• 編譯(compile):把程式語言「翻譯」成機器指令碼。
• 執行(execute):將機器指令碼載入記憶體,CPU開始逐行
執行指令。
Note
• 如果在編譯的過程中,有出現錯誤,編譯器會指出錯誤
的位置;請仔細檢查拼字錯誤。
按「否」,以修
正錯誤
在錯誤上方點兩下,會顯示出錯誤發生「大約」的位置。
2.2 First Program in C++
Printing a Line of Text
Ctrl+F5
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.
The Basic Framework of a Program
The main function is where a C++ program start to execute.
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.
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.
String Constant
• String is composed of characters.
w e
l
c
o
m e
t
o
c
+ + !
\n
• A string constant is enclosed by double quotation marks (").
• Escape characters
– A character preceded by "\"
• Indicates “special” character output
– Example
• "\n"
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.
More Examples of String Constants
• “Hello,\nWorld!”
Hello,
World!
• “Hello, World!\n”
Hello, World!
• “My name is\nJoe.”
My name is
Joe.
Exercise
Descargar

Chapter 02