Presentation is loading. Please wait.

Presentation is loading. Please wait.

物件導向程式設計 目標 認識電腦運作的基本觀念。 了解程式設計的觀念與方法。 擁有利用電腦來解決計算的問題的能力。

Similar presentations


Presentation on theme: "物件導向程式設計 目標 認識電腦運作的基本觀念。 了解程式設計的觀念與方法。 擁有利用電腦來解決計算的問題的能力。"— Presentation transcript:

1 物件導向程式設計 目標 認識電腦運作的基本觀念。 了解程式設計的觀念與方法。 擁有利用電腦來解決計算的問題的能力。
建立物件導向程式設計方法的觀念。

2 課本 課本: C++ 程式設計藝術(第四版) C++ How to program(4th Edition)
作者:H. M. Deitel and P.J. Deitel

3 C++ How to program(4th Edition)
Textbook : C++ 程式設計藝術(第四版) C++ How to program(4th Edition) 作者:H. M. Deitel and P.J. Deitel

4 大綱 Introduction to computers and C++ programming Control Structures
Functions Arrays Pointers and strings Classes and data abstraction Classes: part II Operator overloading Inheritance Virtual functions and polymorphism (至少到此) C++ stream Input/Output Templates

5 課程大綱(continue) Exception Handling File processing Data structures Bits, characters, strings and structures The Preprocessor C legacy code topics Class string and string stream processing Standard Template Library(STL) Standard C++ language additions

6 參考書籍 挑戰 C++ 程式語言 C++ 程式語言 作者:蔡文輝 黃國峰 張真誠 出版社:旗標 這本書的內容比較簡單,適合這學期參考。
作者:Bjarne Stroustrup 出版社:儒林 此書內容豐富,可作為進深參考。

7 評分標準 Mid Term: 30% Final Term: 30% 平常分數 40% Quiz Exercise
Programming homework 出席……

8 授課老師 姓名:廖文淵 Tel: 轉 520 Office: YC-404 Homepage:

9 Chapter 1 –電腦和 C++ 程式設計簡介
目標 了解基本的電腦觀念 熟悉不同的程式語言 了解標準的 C++ 程式設計發展環境。 能夠用 C++ 寫出簡單的程式 能夠使用簡單的輸入、輸出敘述 能夠熟悉基本資料型態 會使用算術運算子(算術運算符號) To understand the precedence of arithmetic operators. To be able to write simple decision-making statements.

10 Chapter 1 – Introduction to Computers and C++ Programming
Outline 1.1 Introduction 簡介 1.2 What is a Computer? 何謂電腦? 1.3 Computer Organization 電腦架構 1.4 Evolution of Operating Systems 作業系統的演進 1.5 Personal Computing, Distributed Computing and Client/Server Computing 個人電腦、 分散式電腦與客戶端/伺服器端電腦 1.6 Machine Languages, Assembly Languages, and High-level Languages機器語言、組合語言與高階語言 1.7 History of C and C++ C 和 C++ 的歷史 1.8 C++ Standard Library C++標準程式庫 1.10 Java and Java How to Program Java 與 Java 程式設計藝術 1.11 Other High-level Languages 其他高階語言 1.12 Structured Programming 結構化程式設計 1.13 The Key Software Trend: Object Technology 軟體主要發展趨勢:物件技術 1.14 Basics of a Typical C++ Environment 典型 C++ 的基本環境 1.15 Hardware Trends 硬體發展趨勢

11 Chapter 1 – Introduction to Computers and C++ Programming
Outline 1.16 History of the Internet 網際網路的歷史 1.18 History of the World Wide Web WWW 的歷史 1.19 General Notes About C++ and This Book 關於C++與此書的一些注意事項 1.20 Introduction to C++ Programming C++程式設計簡介 1.21 A Simple Program: Printing a Line of Text 簡單程式:列印一行文字 1.22 Another Simple Program: Adding Two Integers 另一簡單程式:兩整數相加 1.23 Memory Concepts 記憶體的概念 1.24 Arithmetic 算術運算 1.25 Decision Making: Equality and Relational Operators 判斷的進行:等號運算子和關係運算子 1.26 Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language 物件的探索:物件技術和 UML 的介紹

12 1.1 簡介 在這門課中您將學習 C and C++ 語言,以及Structured programming(結構化程式設計方法) and object oriented programming(物件導向程式設計方法) 程式設計是命令電腦作事情的一種方式。 C++ 是目前應用最廣的語言之一。 C++ 包含 C 語言的所有敘述。 物件導向已經確定是未來十年間最重要的程式設計方法,而物件內部結構可以用結構化程式設計方式。 目前幾乎沒有單純的 C 語言的編譯器,大多同時可編譯 C/C++ 的產品。 C++ 成為建立高效率電腦系統所選用的實作語言。

13 1.2 何謂電腦? Computer Computer programs Hardware 硬體 Software 軟體
何謂電腦? Computer 快速執行運算與邏輯判斷的裝置 Computer programs 控制電腦處理資料的指令集合 Hardware 硬體 電腦是由許多裝置組合而成 Examples: keyboard, screen, mouse, disks, memory, CD-ROM, and processing units Software 軟體 電腦上執行的程式(看不見的部份) 硬體的成本已大幅降低,但軟體的研發費用持續在增加。 因此學習好的程式設計方法就很重要,可以降低軟體成本。

14 Six logical units in every computer:
1.3 電腦架構 Six logical units in every computer: Input unit 輸入單元 Obtains information from input devices (keyboard, mouse) Output unit 輸出單元 Outputs information (to screen, to printer, to control other devices) Memory unit 記憶單元 Rapid access, low capacity, stores input information Arithmetic and logic unit (ALU) 算術運算與邏輯單元 Performs arithmetic calculations and logic decisions Central processing unit (CPU) 中央處理單元,control unit Supervises and coordinates the other sections of the computer Secondary storage unit 輔助儲存單元 Cheap, long-term, high-capacity storage, stores inactive programs

15 Operating systems 作業系統
1.4 作業系統的演進 Batch processing 批次處理 Do only one job or task at a time 一次只能處理一件工作 Operating systems 作業系統 Manage transitions between jobs處理工作之間的轉換動作 Increased throughput 增加電腦的整體工作量 Amount of work computers process總輸出 Multiprogramming 多工程式設計,接多個I/O Many jobs or tasks sharing a computer’s resources能讓許多的工作共用電腦的資源 Timesharing分時 Perform a small portion of one user’s job then moves on to service the next user電腦只會執行某個使用者的一小部分工作,然後就服務下一位使用者

16 1.5 個人電腦、分散式電腦和客戶端/伺服器端電腦
Personal computers Economical enough for individual 最早的個人電腦是 Apple 電腦 Distributed computing Organizations computing is distributed over networks 分散在各地的電腦連結起來形成網路,彼此傳遞資料、計算結果 Client/server computing Sharing of information, across computer networks, between file servers and clients (personal computers) 透過客戶端的電腦連上伺服器,得到伺服器的服務。

17 Three types of programming languages
1.6 機器語言、組合語言與高階語言 Three types of programming languages Machine languages 機器語言 每部電腦有自己的機器語言、也只能直接了解自己的機器語言。 由一串數字組成(二進位數字) Example:(計算薪水的指令) Assembly languages 組合語言 English-like abbreviations representing elementary computer operations (translated via assemblers),需透過組譯器(assembler)翻譯才能執行 Example:(與機器語言是一對一的) LOAD BASEPAY ADD OVERPAY STORE GROSSPAY

18 1.6 機器語言、組合語言與高階語言 High-level languages 類似日常用的英語、或是數學式子 Example:
grossPay = basePay + overTimePay 需要透過編譯器(compiler)轉譯之後才能在電腦執行 以程式設計者的角度來看,用高階語言來寫程式會比較好。為什麼呢? 直譯器(interpreter):將一個高階語言的 statement翻譯後馬上執行、再翻譯下個 statement 再執行、….,除錯比較方便,但執行速度較慢。

19 1.7 C 與 C++ 的歷史 C++ 是從 C 語言演進而來 ANSI C(1983, 1988)
C evolved from two other programming languages, BCPL and B(typeless language), UNIX 作業系統,現在大部份的作業系統都是用 C 或 C++ 寫的。 C 與 UNIX 系統幾乎可說是一起發展的 今天大部份的作業系統都是由 C/C++ 語言撰寫 ANSI C(1983, 1988) 建立標準化的 C 語言定義、machine independent。 C++ “spruces up(打扮)” C 提供物件導向程式設計的能力 Objects are reusable software components that model things in the real world模擬真實世界的可重複使用軟體元件 Object-oriented programs are easy to understand, correct and modify

20 Small talk 是純物件導向語言,而 C++ 是混合的語言。可以按照傳統的寫法、也可以用物件導向的方式來寫
1.7 C 與 C++ 的歷史 Small talk 是純物件導向語言,而 C++ 是混合的語言。可以按照傳統的寫法、也可以用物件導向的方式來寫 可攜性的小技巧 1.1 因為 C 是經過標準化、與硬體設備無關且廣為人們使用的語言,利用 C 所撰寫出來的應用程式,只需一點點修改、有時甚至不需要修改,就可在許多不同的電腦系統上執行。

21 1.8 C++ 標準程式庫 C++ programs C++ standard library 軟體工程的觀點 1.1
由類別 classes 與涵式 functions 組成 我們需要學習 C++ 的語法,也需要學習如何使用 C++ 標準程式庫中的涵式和類別 C++ standard library 提供豐富的類別與涵式給人使用 軟體工程的觀點 1.1 使用「程式區段法」來建立您的程式。避免重複的設計工作。盡量使用現有的程式類別和涵式 - 稱為「軟體重複使用」,此為物件導向程式設計的中心理念。避免重新發明輪子Avoid reinventing the wheel.

22 軟體工程的觀點 1.2 使用 C++ 開發程式時,您通常會使用以下的程式區段:C++ 標準程式庫中的類別和函式、您自行建立的類別和函式以及由不同的軟體支援廠商所提供的類別和函式。 增進效能的小技巧 1.1 儘可能使用標準程式庫中的函式及類別,避免自行撰寫,可增進程式的效率,因為標準程式庫中的軟體是經過小心撰寫,可以有效和正確的執行。 可攜性的小技巧 1.2 儘可能使用標準程式庫中的函式及類別,如此可增進程式的可攜性,因為所有 C++ 的程式碼實作都包含了標準程式庫中的軟體。

23 Java how to program(third edition)
1.9 Java 和 《Java程式設計藝術》 Java used to 建立動態與互動性內容的網頁 發展大型企業應用程式 強化網路伺服器的功能 提供消費產品的應用程式 (如:手機cell phones, 呼叫器pagers and 個人數位助理 personal digital assistants) Java how to program(third edition) Closely followed the development of Java by sun 可用來教授學習程式設計語言的一年級學生有關繪圖 graphics, 影像images, 動畫animation, 聲音audio, 影片video, 資料庫database, 網路networking, 多執行緒multithreading and 合併計算collaborative computing 等基礎知識

24 Other high-level languages
1.11 其他高階語言 Other high-level languages FORTRAN 使用在科學與工程應用上面,大量運算 COBOL 商業應用,大量資料處理 Pascal 用來教授結構化程式設計的觀念

25 Structured programming
1.12 結構化程式設計 六○年代後漸漸發現軟體的發展成本總是超出預算、完成的時間總是比預期慢、完成後的產品也有很多錯蟲。大家漸漸意識到,軟體發展是非常複雜的。如何寫好程式比較像藝術、而不像科學。後來很多人研究程式設計的方法,於是產生所謂的結構化程式設計方法(structured programming),促成 Pascal 的出現。 Structured programming Disciplined approach to writing programs井然有序 Clear, easy to test and debug, and easy to modify Multitasking Many activities to run in parallel , Ada 第一個作出 multitasking 的觀念。

26 1.13 軟體主要的發展趨勢:物件技術 Objects 物件 模擬真實世界可重複使用的軟體元件
Meaningful software units有意義的軟體單元 Date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects, etc. 任何名詞都可以表示成物件 More understandable, better organized and easier to maintain than procedural programming(程序式程式設計) 物件導向的程式設計觀念重視的是「名詞」而非動詞,比較容易清楚表達真實世界。 第一個有物件觀念的語言:Simula 67;第一個完整的物件導向程式語言:Smalltalk。

27 軟體工程的觀點1.3 物件導向程式設計的觀念會是未來幾十年程式設計的主要方法。
大量擁有可重複使用軟體元件的類別程式庫,可從網際網路以及全球資訊網上獲得。許多這種類別庫都是免費提供的。

28 Phases of C++ Programs: Edit 編輯 Preprocess 前處理 Compile 編譯 Link 連結
1.14 典型 C++ 環境的基本架構 Phases of C++ Programs: Edit 編輯 Preprocess 前處理 Compile 編譯 Link 連結 Load 載入 Execute 執行 載入器 Primary Memory Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. 編譯器 Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk 編輯器 前處理器 連結器 CPU . Disk

29 常見的程式設計錯誤1.1 當程式執行時發生了像除以零般的錯誤,這些錯誤稱為執行時期錯誤(run-time error 或execution-time error)。除以零的錯誤一般都屬於致命性的錯誤(fatal error),也就是這種錯誤會讓程式立即停止操作,而無法成功地完成它的工作。非致命性的錯誤可以允許程式繼續執行完畢,但是通常會產生錯誤的結果。(注意:在某些系統上,除以零並不屬於致命性的錯誤。請參考你的系統說明文件)

30 1970年代末期微處理器晶片技術發明後,電腦的發展也愈來愈快速。
1.15 硬體發展趨勢 莫爾定律是:每十八個月 硬體的價格會降低一倍 速度會增快一倍 記憶體容量會增加一倍 輔助儲存體的量也會增加一倍 在通訊界也可適用類似的原則 1970年代末期微處理器晶片技術發明後,電腦的發展也愈來愈快速。

31 Packet switching (分封交換)
1.16 網際網路(internet)的歷史 網際網路能夠 透過 快速、方便地通訊 Packet switching (分封交換) 將資料分割成小區塊送出去,收到的部份再重組 允許多個使用者同時傳送資料 沒有中央控制 這表示網路的一部份當掉,其他部份仍可正常運作。 頻寬(bandwidth) 通訊纜線上的資訊傳送量

32 1.17 全球資訊網(World Wide Web)的歷史
允許使用者找到、觀看幾乎任何主題的多媒體文件 它使資訊能立即、方便地傳遞到全世界 使個別的小企業也能有全世界曝光的機會 改變了原先商業經營方式

33 此書希望適合初學程式設計的人,所以它很強調程式的清晰可讀性 clarity。 良好的程式設計習慣 1.1
以簡單直接的方式寫出你的 C++ 程式。有時我們稱之為KIS(keep it simple,保持單純化)。千萬別嘗試透過怪異的用法而「扭曲」語言的原意。

34 可攜性的小技巧 1.3雖然我們可以寫出可攜式的程式,但因為不同的C/C++編譯器之間和不同電腦之間,仍會有許多問題產生,使得可攜性仍很難完全達到。單單只用C/C++寫程式,並不能保證一定會具有可攜性。程式設計師通常需要直接處理編譯器及電腦的相異之處。 良好的程式設計習慣 1.2閱讀你目前使用C++版本的手冊。經常參考這些手冊,以便確定你完全了解C++豐富的功能,以及你是正確地使用它們。 良好的程式設計習慣1.3你的電腦跟編譯器都是你的良師。如果仔細讀完你的C++手冊後,你還是不確定某個C++功能是如何執行的話,先以一個小程式試驗看看會有什麼結果。將你的編譯器中的選項設定成「最大警告程式」(maximum warnings)。仔細研究每個你在編譯程式時得到的訊息,然後改正程式以便消除這些訊息。

35 1.20 C++程式設計簡介 C++ language 下面幾節會看到幾個範例 接下來我們以結構化的程式設計方式來介紹 C++ 語言。
每個例子都會一個敘述一個敘述分析

36 改善程式的可讀性,不會引起電腦作任何動作。 1. Comments 2. Load <iostream> 3. main
1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++,可以用中文寫 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << "Welcome to C++!\n"; 8 9 return 0; // indicate that program ended successfully 10 } 註解 Comments 放在 /* 與 */ 之間或 // 後面 改善程式的可讀性,不會引起電腦作任何動作。 1. Comments 2. Load <iostream> 3. main 3.1 Print "Welcome to C++\n" 3.2 exit (return 0) Program Output 前處理器指令 preprocessor directive # 字號開頭的程式是前處理器的指令,通知前處理器處理 #include <iostream> 告訴前處理器將 iostream 的內容載入,當您的程式要用到輸入或輸出時,大多需要載入 <iostream> 或 <iostream.h>。 C++ 程式包含一個以上的函式, 其中一個必須是 main,由 main 開始執行 括號指出這是一個函式 int 的意思是 main 傳回一個整數值,第三章會更詳細說明。 函式本體(body)必須由左括號 { 開始且用右括號 } 結尾。 將雙引號中間的字串(string)由螢幕印出來。 這整行,包括 std::cout, << 運算子operator, 字串 “Welcome to C++!\n” 以及分號 (;), 稱為敘述式 statement. 所有的敘述都必須用分號來結尾。 return 結束函式的一個方式return 0, 在這個例子, 意思是這個程式正常結束 Welcome to C++!

37 1.21 一個簡單的程式: 印出一行文字 std::cout << \ 標準輸出串流物件 “連接” 到螢幕
std:: 表示 cout 這名稱是屬於名稱空間(namespace) std std:: 透過使用 using 敘述,可去掉 std:: << 串流插入運算子 stream insertion operator 右邊的運算元會插到輸出串流(連接到螢幕) std::cout << “Welcome to C++!\n”; \ 脫序字元 Escape character 表示後面會有一個字元與它結合成脫序串列。

38 1.19 A Simple Program: Printing a Line of Text
輸出文字的方法有很多種 下面有更多的例子

39 1. Load <iostream> 2. main 2.1 Print "Welcome"
1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << "Welcome "; 8 std::cout << "to C++!\n"; 9 10 return 0; // indicate that program ended successfully 11 } 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Program Output 除非使用 newline ‘\n’不然文字都會出現在同一行 Welcome to C++!

40 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 newline
1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << "Welcome\nto\n\nC++!\n"; 8 9 return 0; // indicate that program ended successfully 10 } 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 newline 2.3 Print "to" 2.4 newline 2.5 newline 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0) Program Output 一個敘述可以輸出很多行的文字 Welcome to C++!

41 良好的程式設計習慣 1.4 每個程式的開端,都加上一行註解,說明這個程式的目的。 常見的程式設計錯誤 1.2 & 1.3
在需要從鍵盤輸入資料或者將資料輸出到螢幕的程式中,忘了載入 iostream 檔案,會使得編譯器產生錯誤訊息。 省略敘述式結尾的分號,這是語法錯誤syntax error 。當編譯器無法辨識某一個敘述式時,就會產生語法錯誤的訊息。編譯器一般都會發出錯誤訊息,幫助程式設計師找出並改正錯誤的敘述式。語法錯誤就是指違反所用程式語言的規定。語法錯誤又稱為編譯錯誤compile errors ,編譯時期錯誤compile-time errors ,或者編譯動作錯誤compilation errors ,因為它們都是出現在編譯時期。

42 良好的程式設計習慣 1.5 & 1.6 & 1.7 許多程式設計師會在函式所印出字串(\n)的結尾處,加上一個字行字元。這樣就能確保函式會讓游標回到下一行的開端處。這項很自然的傳統用法加強了軟體的重複使用性,是在軟體發展環境中重要的目標。 將函式整個本體內容在大括號之間縮排(indent)一層,就能夠明白的定義函式的主體內容的位罝。這樣就能讓程式中的函式架構突顯出來,共且幫助函式更容易閱讀。 規定你想要的縮排量,然後就統一使用這個規定。Tab鍵可以用來設定縮排量,但是tab定位點在不同的編輯器可能規定不同。牛們建議使用每隔1/4英吋一個定位點或者最好是以三個空白空間作為一層縮排量。

43 1.22 另一個簡單的程式:兩個整數的相加 變數 Variables 記憶體位置,是用來存程式中的某些值
變數使用之前一定要先宣告(包括資料型態) 合法的識別字 identifier,由 letters(字母), digits(數字) 與 underscores(底線) 組成,而不可由數字開頭。 case sensitive 大小寫不同 一些普通的資料型態: int – 整數 char – 字元 double – 雙精確度的浮點數 Example: int myvariable; 宣告 int 的變數 myvariable Example: int variable1, variable2; Declares two variables, each of type int

44 >> (串流擷取運算子stream extraction operator)
1.22 另一個簡單的程式:兩個整數的相加 >> (串流擷取運算子stream extraction operator) 當使用 std::cin, 程式會等待使用者輸入資料,並將所輸入的資料存在運算子右邊的變數裡面 使用者輸入資料、然後按 Enter (Return) 鍵來將資料送給電腦 Example: int myVariable; std::cin >> myVariable; 等待使用者輸入,將輸入存在 myVariable = (指定運算子 assignment operator) 將某個值存在變數裡面 Assigns value to a variable 二元運算子 Binary operator (有兩個運算元) sum = variable1 + variable2;

45 Load <iostream> 2. main
1 // Fig. 1.6: fig01_06.cpp 2 // Addition program 3 #include <iostream> 4 5 int main() 6 { 7 int integer1, integer2, sum; // declaration 8 9 std::cout << “Enter first integer\n”; // prompt 提示 10 std::cin >> integer1; // read an integer 11 std::cout << "Enter second integer\n"; // prompt 12 std::cin >> integer2; // read an integer 13 sum = integer1 + integer2; // assignment of sum 14 std::cout << "Sum is " << sum << std::endl; // print sum 15 16 return 0; // indicate that program ended successfully 17 } Load <iostream> 2. main 2.1 declare variables integer1, integer2, and sum 2.2 Print "Enter first integer" 2.2.1 Get input 2.3 Print "Enter second integer" 2.3.1 Get input 2.4 Add variables and put result into sum 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output 注意如何使用 std::cin 來得到使用者的輸入資料 變數可以如此輸出 std::cout << variableName. std::endl 清掉緩衝區並印出 newline. Enter first integer 45 Enter second integer 72 Sum is 117

46 良好的程式設計習慣 1.8~1.11 有些程式設計者喜歡每個變數用一單獨行來宣告,這樣方便為此變數寫下註解。
在每個逗號(,) 放一個空格,讓程式更具可讀性。 選擇有意義的變數名稱可以幫助程式有「自我說明」的效果,即只需要看一下變數名稱,就能了解程式的涵義,不必要去閱讀手冊或使用大量註解加以說明。 避免使用開頭是底線和雙底線的識別字,因為 C++ 本身內部常如此設定 C++ 自己的識別字。這可以避免你所選用的識別字和編譯器所選用的識別字互相混淆。

47 可攜性小技巧 1.4 C++ 允許任何長度的識別字,但是你所使用的系統和C++的實作應用程式可能會對識別字的長度加上某些限制。識別字的長度不要超過31個字元,較能確保可攜性。 良好的程式設計習慣 1.12~1.14 布宣告和接下來的可執行敘述式之間加上一空白行。這樣可以讓宣告在程式中,顯得較為突出,而讓程式較清楚。 若您喜歡將宣告放在函式的開頭,請記得用一空白行將宣告和之後的可執行敘述式加以隔離,可突顯出宣告的結束處和可執行敘述式的起頭處。(變數不一定要在涵數開始的地方宣告,但一定要在使用該變數之前宣告) 在二元運算子(例如 +, -, *, /, =, …)的兩邊都放一空白字元,可以使程式更具可讀性。

48 1.23 記憶體概念 變數名稱 Variable names 祝覺化表示 對應到電腦記憶體的位置
每個變數有名稱 name, 型態 type, 佔記憶體大小 size 與 值 value (還有位址 address, 使用範圍 scope, 壽命lifetime) 當新的值存到變數裡,就取代原來的值,也就是說原來的值被破壞了。 從變數中讀值出來後,此變數的值沒有改變 祝覺化表示 integer1 45

49 Operator precedence(優先順序, Appendix A)
算術運算 算術計算 * 表示乘法 and / 代表除法 整數除法會將餘數無條件丟掉 7 / 4 的值為 1 餘數運算子 % 7 % 4 的值為 3 Operator precedence(優先順序, Appendix A) 某些算術運算子會比其它的先作(i.e., 乘法在加法之前) 有需要時可以用括號 Example: 要算 a, b, c 的平均時 不能用: a + b + c / 3 必須用: (a + b + c ) / 3

50 1.22 算術運算 算術運算子: 優先順序的規則:

51 a = 2 b = 3 c = 7 x = 5

52 相等(equality)與關係運算子(relational) operators
1.25 進行決策:等號運算子和關係運算子 if 結構 測試條件成立與否,成立的話就執行某些指令、不成立就跳過該指令。 相等(equality)與關係運算子(relational) operators 比算術運算子的優先順序低 其優先順序如下表

53 1.25 進行決策:等號運算子和關係運算子

54 1.25 進行決策:等號運算子和關係運算子 using 敘述 可以不用寫 std:: ,也就是說
允許只寫 cout 來取代 std::cout 只要在程式開頭寫了下面幾個 statements 程式中就不需要再寫 std::。 using std::cout; using std::cin; using std::endl;

55 1. Load <iostream> 2. main 2.1 Initialize num1 and num2
1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 #include <iostream> 5 6 using std::cout; // program uses cout 7 using std::cin; // program uses cin 8 using std::endl; // program uses endl 9 10 int main() 11 { 12 int num1, num2; 13 14 cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; 16 cin >> num1 >> num2; // read two integers 17 18 if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << endl; 20 21 if ( num1 != num2 ) cout << num1 << " is not equal to " << num2 << endl; 23 24 if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl; 26 27 if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl; 29 30 if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl; 33 1. Load <iostream> 2. main 2.1 Initialize num1 and num2 2.1.1 Input data 2.2 if statements Notice the using statements. Enter two integers, and I will tell you the relationships they satisfy: 3 7 The if statements test the truth of the condition. If it is true, body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {}. 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7

56 2.3 exit (return 0) Program Output
34 if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl; 37 38 return 0; // indicate that program ended successfully 39 } 2.3 exit (return 0) Program Output Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7

57 常見的程式設計錯誤 1.6~1.10 如果在 ==, !=, >= 和 <= 這些運算子的兩個符號中間出現空格,會造成語法錯誤。 如果 !=, >= , <= 這些運算子的兩個符號顛倒,通常都會造成語法錯誤。某些狀況下 =! 不是語法錯誤,但是幾乎一定是邏輯錯誤 等號運算子 == 與指含運算子 = 常會混淆。特別容易將 == 寫成 =,可能不會造成語法錯誤,但卻更不容易找到錯在哪裡。 在 if 結構的條件式後面加一個分號,可能不會造成語法錯誤,但通常也是很難找的邏輯錯誤。 識別字中間插入空白字元,造成語法錯誤

58 1.26 物件的探索:物件技術(Object Technology)和UML(Unified Modeling Language) 的介紹
物件導向 Object orientation 一種思考世界與寫電腦程式的自然方式,將實際世界的各種物件,以軟體的對應元件加以模擬。 屬性 Attributes :物件的性質 properties of objects 例如:大小、形狀、顏色、重量等等 行為 Behaviors:動作 actions A ball rolls 滾動, bounces 反彈, inflates膨脹 and deflates 洩氣 物件可以執行這些動作 繼承 Inheritance 新物件的類別可以擁有現存類別的特性 物件道向程式設計(OOP)將資料與方法封裝成為物件,使其緊密結合。且達到資訊隱藏(information hiding)的效果。 Information hiding 只能透過物件所定義、良好的介面來使用該物件,無法知道其實作、細節方式

59 1.26 物件的探索:物件技術(Object Technology)和UML(Unified Modeling Language) 的介紹
OOP 重點放在如何建立他們自己定義的類別。 抽象 Abstraction – 巨觀(用較大的角度來看東西) 將一群點點看出一張照片 看成一間房子而不是一大堆磚頭 類別 Class – 程式的單位unit of programming 類別是物件的藍圖:物件可由類別建立出來。 類別包含函數:用來實作行為(behaviors) 類別包含資料:用來實作屬性(attributes) 類別可重複再使用(reusable):這是影響軟體發展最重要的原因

60 1.26 物件的探索:物件技術(Object Technology)和UML(Unified Modeling Language) 的介紹
物件導向的分析與設計 OOAD 用物件導向的觀點來分析和設計你的系統。 當問題很小時、用虛擬碼就可以解決分析與設計的問題。 當問題變大時,就需用到 OOAD 的方法。 UML(Unified Modeling Language) 是一種圖形化的語言,可以將 OOAD 程序的成果彼此分享。 Unified Modeling Language (UML) 1996 年提出初版;1997年 1.1 版;1999年 1.3版。 用來將物件導向系統作模組化、並幫助設計。 是一種複雜的、多功能的圖形語言。 在每一章的「物件的探索」這一節中,會使用簡化的 UML 來分析與討論問題。

61

62

63

64

65

66

67

68

69

70 1.34 Distinguish between the terms fatal error and non-fatal error. Why might you prefer to experience a fatal error rather than a non-fatal error?


Download ppt "物件導向程式設計 目標 認識電腦運作的基本觀念。 了解程式設計的觀念與方法。 擁有利用電腦來解決計算的問題的能力。"

Similar presentations


Ads by Google