25
Windows Processing Design 1 Chapter 1 C/C++ Chapter 1 C/C++ 概概 概概 畫畫畫畫畫畫畫畫畫 畫畫畫畫 畫畫畫畫畫畫畫 畫畫畫畫畫 畫畫畫畫畫 畫畫 畫畫 畫畫 BCB 畫畫畫畫

Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

  • View
    236

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 1

Chapter 1 C/C++ Chapter 1 C/C++ 概論概論畫面輸出與鍵盤輸入程式流程程式流程的迴圈函數的基礎指標與陣列字串類別利用 BCB 開發程式

Page 2: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 2

畫面輸出與鍵盤輸入畫面輸出與鍵盤輸入C++ 是以 C 語言為基礎發展的程式語言支援物件導向的程式語言程式基本架構#include <iostream.h> // 標頭 void main(void) // 程式的起始{ int X; // 宣告變數 cout << “ 第一個程式” ; // 畫面輸出 cin >> X; // 讀取鍵盤資料

return 0; // 回傳值 }

Page 3: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 3

畫面輸出與鍵盤輸入畫面輸出與鍵盤輸入 con~con~

程式執行過程 (Compiler)

User(Source Program)

Computer(Execute File)Compiler and Linker

Header

Page 4: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 4

畫面輸出與鍵盤輸入畫面輸出與鍵盤輸入 con~con~

Type1.integer ( 整數 ) ex : 5002.char ( 字元 ) ex : ‘A’3.float ( 浮點數 ) ex : 1.4144.bool ( 布林值 ) ex : True or False 5.String ( 字串 ) ex : “Teacher”

Operator+,-,*,/,%,=,&&,||,!,++,-- ,*=,+=,-=…

X

Page 5: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 5

程式流程程式流程規劃程式的流程結構化的程式If else statementif ( True ) { …}else if { …}else {…}

判斷式

True False

Program

Page 6: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 6

程式流程程式流程 con~con~

Switch statementswitch(x){ case 1: a=1;break; case 2: a=2;break; case 3: a=3;break; default: …}

判斷式

a=1

a=2

a=3

default

Break

No Break

Page 7: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 7

程式流程的迴圈程式流程的迴圈重複程式的流程方法While statementwhile (True){…}

Do statementdo{…}while(True)

判斷式

Statement

true

false

Page 8: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 8

程式流程的迴圈程式流程的迴圈 con~con~

For statementfor( 初始值 ; 判斷式 ; 運算式 ){…}

比較三個程式迴圈的不同1.while{…}2.do{…}while()3.for(;;;){…}

判斷式

Statement運算式

初始值

Exit

Page 9: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 9

函數的基礎函數的基礎Function definition1.function header2.function body 兩個部分構成

int max( int a,int b,int c){ … return 整數 ;}

Function body

Function header

傳回型態 函數名稱 參數宣告

Page 10: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 10

函數的基礎函數的基礎# include <iostream.h>int max(int a,int b,int c){ int max = a; if(b>max) max = b; if(c>max) max = c; return (max);}void main(void){ int show = max(1,2,3);}

Main( )

Max()

Page 11: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 11

指標與陣列指標與陣列指標& : 位址運算子是取出物件位址的運算子int x = 100;int* ptr = &x;

● ptr 指向 int 的指標型態 ● *ptr 變成 x 的型態和值

500 號100500 號

X

ptr

Page 12: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 12

指標與陣列指標與陣列 con~con~

Function Call and Pointervoid swap(int* x,int* y){ int temp = *x; *x = *y; *y = temp;}void main(void){ swap(&a,&b);}

&a &b

Swap

X Y

Page 13: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 13

指標與陣列指標與陣列 con~con~

Array ( 陣列 )int x [4];int* ptr;p = &x[0]; or p = x;p+1; p-1;

NULL ( 空指標 )

X[0]

X[1]

X[2]

X[3]

ptr

*ptr

*(p+1)

Page 14: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 14

字串字串字串值 “ ABC”

char str[4] = {‘A’,‘B’,‘C’,‘\0’};char str[4] = “ABC”;

Pointer 操作char* ptr = &str;

A B C \0

Page 15: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 15

字串字串 con~con~

由指標所產生的字串char* ptr = “ABC”;

處理字串的標準涵式庫#include<string.h> strcpy(),strcmp(),strlen()…

A B C \0ptr

Page 16: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 16

類別類別Object ( 物件 )

Class 的定義1.builtin type2.user-defined type class student { public: int no; // data member int id;};

no

id

Page 17: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 17

類別類別 con~con~

使用 Class1. student csie={30,12345678};2. csie.no = 20; csie.id = 87654321;3. student* ptr = &csie; ptr->no = 35; ptr->id = 98765432;

no

id

ptr

Page 18: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 18

類別類別 con~con~

不保證完全初始化不保證資料的保護Date hiding - 不該公開的資料則不公開 (private)

Construtor ( 建構元 )- 必須與 Class 同名不可- 防止不完全的初始化

私人成員

公用成員

Page 19: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 19

類別類別 con~con~Class student{private : int no; int id;public: student(int x,int y){ no = x; id = y;}}Student csie(40,123456789);

no

id

Page 20: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 20

類別類別 con~con~

Member function ( 成員函數 )- method ( 方法 )class student{private:…public:… void show(){…} void …}

no

id

Page 21: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 21

類別類別 con~con~

Class 建立class student{public: student(int x,int y); void show();};student::student(int x,int y){…}void student::show(){…}

傳回型態 x::func(引數 )

{//…

}

Page 22: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 22

類別類別 con~con~

HeaderMain( )

Member function Class

Include “ 自訂”

Page 23: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 23

利用利用 BCB BCB 開發程式開發程式開啟1.File -> New -> Console Wizard

2.File -> New -> Header File

Page 24: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 24

利用利用 BCB BCB 開發程式開發程式 con~con~

畫面

Page 25: Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式

Windows Processing Design 25

利用利用 BCB BCB 開發程式開發程式 con~con~

執行

存檔 File -> Save All