張逸 CH5 程式語言(設計)
(二).function 介紹
1. function define
return type function name (parameter list) {
function code
or
body
}
. return-type:回傳型態,當不回傳值以"void"表示之。
. 於 C、C++中 function-name 若為"main"代表主程式
. 主程式代表程式一開始的進入點
. parameter list:用來接收外界傳入的參數值
2. 參數的傳遞
一. 圖示
int main ( ) {
Test ( x, y ) ;
…
…
int Test ( int A, int B ) {
實際參數 (Actual Parameter)
}
形式參數
(Formal Parameter)
}
3. 方式 (call by value 常考!)
call-by-value call-by-address call-by-name
1. 實際
、
形式參數佔用不同
的 memory space
2. Binding speed 最慢
3. No side effect
4. 不適用於結構型別的傳
遞
1. 實際、形式參數佔用相
同的 memory space
2. Binding speed 最快
3. 有 side effect
4. 用於 Array struct
1. 實際參數取代函式
中的形式參數
2. 速度介於上述 2 者
3. 有 side effect
3.
張逸 CH5 程式語言(設計)
範例1
int main ( ) { void fun (int x , int y , int z) {
int a = 2 , b = 3 , c=4; x = y ;
fun (a , b , c) ; z = x ;
printf (a , b , c) ; }
return 0;
}
問:(1) 採 call-by-value
(2) 採 call-by-address
解:
(1) 印出 2 , 3 , 4。
(2) 印出 3 , 3 , 3
範例 2
int main ( ) { void change (int a , int b) {
int x = 1 , y = 0; int temp;
change (x , y) ; temp = a ;
printf (x , y) ; a = b;
return 0; b = temp;
} }
解:
1 0
範例 3
int main ( ) { void fun (int x , int y) {
int a = 10 , b = 20 ; x = 3 ;
fun (a , b) ; y = 6 ;
printf (a , b) ; }
return 0;
}
問:(1) 採 call-by-value
(2) 採 call-by-address
(3) 採 call-by value result (有 side effect)
解:
(1) 印出 10 , 20。
(2) 印出 3 , 6。
(3) 印出 3 , 6。
4.
張逸 CH5 程式語言(設計)
範例4 : (call by name)
int main ( ) { void fun (int x , int y , int z) {
int a = 4 , b = 6 ; x = 7 ;
fun (a , b , a + b) ; y = 10 ;
return 0; int t = z * z ;
} printf (t) ;
}
問:採 call-by-name
解:
t = a + b * a + b= 7 + 10 * 7 + 10= 87
(三) 結構化程式語言 (選擇題常考)
要素 說明 圖示
Sequential(循序) 以循序執行程式指令
step 3
step 1
step 2
Selective / Conditional
(選擇)
1. 利用條件式決定欲執行的
指令
2. eg : if…else, swith…case
step 2
step 4
step 3
step 1
true false
Repeat / Iterative(重複)
1. 利用 loop 來重複執行部分
指令
2. eg : for loop , while loop ,
do…while loop
step 1
step 2
step 3
step 4
--程式基本概論 試閱完畢--
張逸 CH5 程式語言(設計)
(四)Mapping
傳統
O. O. 類別 物件
int x
type variable
;
;
Book
att.
op.
op.
att.
Reader
class model object model
A
B
C
甲
乙
丙
1. 說明
(1) object 間的溝通採"Message Passing"。
(2) class 佔用一份 Memory space,而各 object 佔用不同的 Memory space。
--物件導向 試閱完畢--
9.
張逸 CH5 程式語言(設計)
範例1
class A {
public :
int oid ;
A (int i) {oid = i ;}
~A ( ) {cout << oid ;}
} ;
int main ( ) { void funl ( ) {
A a1(1) ; A a2(2) ;
fun1( ) ; static A a3(3) ;
return0 ; }
} .static 物件在宣告時建立物件於 static 區塊
.於主程式執行完畢才將之回收
解:
2 1 3
補充: static 變數
宣告方式:static△型別△變數名稱;
範例 1
int a( int i ){
static int v=1;
int t = v;
v+=i;
return t;
}
int b( int i){ int main( ){
int v =1; int i;
int t =v; for( i =1; i<=10; i<<1){
v+=i; a(i); b(i); c(i);
return t; }
static int v; cout<<”a=”<<a(i)<<endl;
int c( int i ){ cout<<”b=”<<b(i)<<endl;
int t =v; cout<<”c=”<<c(i)<<endl;
v+=I; return 0;
return t; }
}
10.
張逸 CH5 程式語言(設計)
解:
a=16
b=1
c=15
範例2
Give the class definition in C++ language
class CD {
public :
CD ( ) {cout << "cons" ;}
~CD ( ) {cout << "del" ;}
} ;
what will the following program output ?
int main ( ) {
CD c1 ; .object 用 stack 管理,故後進先出。
CD c2 ; c2 → c1(回收順序)
return 0 ;
}
解:
cons , cons , del , del
範例 3
class A {
public:
A( ){ p( );}
~A( ){ p( );}
virtual void p( ){ q( ); }
virtual void q( ){ cout<<”A”;} int main( ){
}; A*ptr=new B( );
delete ptr;
class B { }
public:
B( ){ p( );}
~B( ){ p( );}
void q( ){ cout<<”B”;}
解: ABA
--C++章節試閱完畢--
11.
張逸 CH5 程式語言(設計)
CH5程式語言 第五章 Java
(一). Java 介紹
1. java 的特色
(1). Sun(昇陽)推出的 O.O. Language
(2). easy、simple
(3) 沒有 pointer 操作。
(4) O.O.特色可協助了解真實生活。
2. interpreted environment
(1) Speed of development。
(2) Cross platform。
Compare JAVA vs C++
Java C++
pure O.O. language Not pure O.O. language
No pointer have pointer
support single inheritance multiple inheritance
use garbage collection 須自行回收 Memory
利用 applet 開發 web 系統 Web 開發支援少
interpreter language Speed slow Compile language Speed fast
Cross platform None
安全性佳(∵有 class verifier) 安全性較差
Open source 支援多 Open source 少
3. class
(1). 格式
class class _ name {
constructor
attribute 變數宣告
method function
}
(2). java 中採 garbage collection,故不須 destructor
12.
張逸 CH5 程式語言(設計)
範例1
class recursion{
int func(int n){
int result;
result = func(n-1);
return result;
}
}
class Output{
public static void main (string args[]){
recursion obj = new recursion( );
system.out.print(obj.func(5));
}
}
解:本題會有無窮遞迴的問題
補充:記憶體配置
System:系統環境參數
Stack
Heap
Uninitial data: 又稱 bss
Initial data
Text:程式碼
範例 2(特殊題型)
class Excute{
public static void main (string args[]){
PriorityQueue pq = new PriorityQueue( );
pq.add(“elephant”);
pq.add(“tiger”);
pq.add(“monkey”);
system.out.print(pq.poll( ) + “,”+ pq.peak( ));
}
}
解: elephant, monkey
--Java 題型 試閱完畢--