windows processing design1 chapter 1 c/c++ 概論 畫面輸出與鍵盤輸入 程式流程...
Post on 21-Dec-2015
236 views
TRANSCRIPT
Windows Processing Design 1
Chapter 1 C/C++ Chapter 1 C/C++ 概論概論畫面輸出與鍵盤輸入程式流程程式流程的迴圈函數的基礎指標與陣列字串類別利用 BCB 開發程式
Windows Processing Design 2
畫面輸出與鍵盤輸入畫面輸出與鍵盤輸入C++ 是以 C 語言為基礎發展的程式語言支援物件導向的程式語言程式基本架構#include <iostream.h> // 標頭 void main(void) // 程式的起始{ int X; // 宣告變數 cout << “ 第一個程式” ; // 畫面輸出 cin >> X; // 讀取鍵盤資料
return 0; // 回傳值 }
Windows Processing Design 3
畫面輸出與鍵盤輸入畫面輸出與鍵盤輸入 con~con~
程式執行過程 (Compiler)
User(Source Program)
Computer(Execute File)Compiler and Linker
Header
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
Windows Processing Design 5
程式流程程式流程規劃程式的流程結構化的程式If else statementif ( True ) { …}else if { …}else {…}
判斷式
True False
Program
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
Windows Processing Design 7
程式流程的迴圈程式流程的迴圈重複程式的流程方法While statementwhile (True){…}
Do statementdo{…}while(True)
判斷式
Statement
true
false
Windows Processing Design 8
程式流程的迴圈程式流程的迴圈 con~con~
For statementfor( 初始值 ; 判斷式 ; 運算式 ){…}
比較三個程式迴圈的不同1.while{…}2.do{…}while()3.for(;;;){…}
判斷式
Statement運算式
初始值
Exit
Windows Processing Design 9
函數的基礎函數的基礎Function definition1.function header2.function body 兩個部分構成
int max( int a,int b,int c){ … return 整數 ;}
Function body
Function header
傳回型態 函數名稱 參數宣告
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()
Windows Processing Design 11
指標與陣列指標與陣列指標& : 位址運算子是取出物件位址的運算子int x = 100;int* ptr = &x;
● ptr 指向 int 的指標型態 ● *ptr 變成 x 的型態和值
500 號100500 號
X
ptr
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
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)
Windows Processing Design 14
字串字串字串值 “ ABC”
char str[4] = {‘A’,‘B’,‘C’,‘\0’};char str[4] = “ABC”;
Pointer 操作char* ptr = &str;
A B C \0
Windows Processing Design 15
字串字串 con~con~
由指標所產生的字串char* ptr = “ABC”;
處理字串的標準涵式庫#include<string.h> strcpy(),strcmp(),strlen()…
A B C \0ptr
Windows Processing Design 16
類別類別Object ( 物件 )
Class 的定義1.builtin type2.user-defined type class student { public: int no; // data member int id;};
no
id
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
Windows Processing Design 18
類別類別 con~con~
不保證完全初始化不保證資料的保護Date hiding - 不該公開的資料則不公開 (private)
Construtor ( 建構元 )- 必須與 Class 同名不可- 防止不完全的初始化
私人成員
公用成員
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
Windows Processing Design 20
類別類別 con~con~
Member function ( 成員函數 )- method ( 方法 )class student{private:…public:… void show(){…} void …}
no
id
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(引數 )
{//…
}
Windows Processing Design 22
類別類別 con~con~
HeaderMain( )
Member function Class
Include “ 自訂”
Windows Processing Design 23
利用利用 BCB BCB 開發程式開發程式開啟1.File -> New -> Console Wizard
2.File -> New -> Header File
Windows Processing Design 24
利用利用 BCB BCB 開發程式開發程式 con~con~
畫面
Windows Processing Design 25
利用利用 BCB BCB 開發程式開發程式 con~con~
執行
存檔 File -> Save All