二进位也可以很有趣-交通大学

上传人:sha****en 文档编号:16243694 上传时间:2020-09-24 格式:PPT 页数:146 大小:874.50KB
返回 下载 相关 举报
二进位也可以很有趣-交通大学_第1页
第1页 / 共146页
二进位也可以很有趣-交通大学_第2页
第2页 / 共146页
二进位也可以很有趣-交通大学_第3页
第3页 / 共146页
点击查看更多>>
资源描述
1,Object Oriented Concept,蔡文能 交通大學資訊工程學系 tsaiwncsie.nctu.edu.tw,OOA,OOD,OOP,http:/www.csie.nctu.edu.tw/tsaiwn/oop/,2,Agenda,Review: Data Representation in the computer Why using struct? What is Object? What is class? OO Concept? Why using class? How to build a component? OO features Encapsulation, Information Hiding, Inheritance, Polymorphism,3,電腦內如何儲存資料? 如何表示文字? ASCII? EBCDIC? 如何表示數值? 整數? (char, short, int, long) long long, boolean ? 實數? (float, double) 數值的範圍? 實數數值的準確度(precision) 其它?,問題與思考 (Data Representation),long long is introduced in C99,bool is introduced in C+,4,問題與思考 (如何表示文字? ),#include unsigned char x999 = 0 ; main( ) int m = 0 x41, n=97; x0 = m; x1=n; x2 = 98; x3=99; x7 = 063; x8=0 x38; /* 063 =51 */ x4 = Y; x5= o; x6= u; printf(=%s=n, x); ,5,問題與思考 (如何表示文字?),ccbsd2: tsaiwn gcc testc.c ccbsd2: tsaiwn ./a.out =AabcYou38= ccbsd2: tsaiwn,先把前面程式存入 testc.c,編 譯 與 連 結,執 行,6,問題與思考 (中文碼?),#include unsigned char x9 = 0 ; main( ) int m = 0 xa4, n=0 x6a; x0 = m; x1=n; x2 = 0 xae, x3=97; x4 = 0 xa6, x5=0 x6e; printf(=%s=n, x); ,7,問題與思考 (中文碼?),ccbsd2: tsaiwn gcc testc.c ccbsd2: tsaiwn ./a.out =大家好= ccbsd2: tsaiwn,先把前面程式存入 testc.c,編 譯 與 連 結,執 行,8,實數與準確度(precision),#include float x, xdelta; int i; /*precision.c */ main( ) double y; x = 1234567.2, xdelta = 0.0001; printf(Before loop, x=%fn, x); for(i=1; i= 8000; i+) y = x + xdelta; /*/ if(i = 1) printf(first y = %fn, y); x = y; printf(After loop, x=%fn, x); ,9,float 實數準確度七位多,ccbsd2:precision/ gcc precision.c ccbsd2:precision/ ./a.out Before loop, x=1234567.250000 first y = 1234567.250100 After loop, x=1234567.250000 ccbsd2:precision/ float 實數佔用 32 bits,10,double 實數準確度,#include double x, xdelta; int i; /*precdbl.c */ main( ) double y; x = 1234567.2, xdelta = 0.0001; printf(Before loop, x=%fn, x); for(i=1; i= 8000; i+) y = x + xdelta; /*/ if(i = 1) printf(first y = %fn, y); x = y; printf(After loop, x=%fn, x); ,11,double 實數準確度十五位多,ccbsd2:precision/ gcc precdbl.c ccbsd2:precision/ ./a.out Before loop, x=1234567.200000 first y = 1234567.200100 After loop, x=1234568.000001 ccbsd2:precision/ double 實數佔用 64 bits,12,Summary about Data Representation,變數(variable)就是佔住記憶體一小塊地方有個名字 變數名字要用字母開頭 變數的種類(類型): 整數: char, (byte), short, int, long, long long (C99才有) 實數: float, double, long double 數值的絕對值太大無法存入規定格式稱 Overflow 數值的絕對值太小無法存入規定格式被電腦當作 0 存起來稱 Underflow , 實數才會! 整數沒有underflow Java 語言有 8 種primitive data type boolean, byte, char, short, int, long, float, double float 的準確度只有二進位24位, 約十進位 7 位強 double 的準確度只有二進位53位, 約十進位 15 位強 自訂型別 ( User Defined Data Type),13,整數 overflow (溢位),#include int main( ) short ans = 32765; int k; for(k=1; k= 6; +k) printf (= %dn, ans+ ); ,= 32765 = 32766 = 32767 = -32768 = -32767 = -32766,物極必反?,14,float實數 overflow (溢位),/* s eeeeeee efffffff gggggggg hhhhhhhh */ /* +/- 1.fffffffgggggggghhhhhhhh * 2*(eeeeeeee - 127) */ /* eeeeeeee: Exponent base 2 excess 127 */ #include #include int main( ) float ans = pow(2,125); /* 2 的 125 次方 */ int k; for(k=1; k= 6; +k) printf(= %fn, ans ); ans *= 2.0; ,Inf = 無窮大,= 42535295865117307932921825928971026432.000000 = 85070591730234615865843651857942052864.000000 = 170141183460469231731687303715884105728.000000 = Inf = Inf = Inf,15,float實數 underflow(虧失),/* s eeeeeee efffffff gggggggg hhhhhhhh */ /* +/- 1.fffffffgggggggghhhhhhhh * 2 *(eeeeeeee - 127) */ /* eeeeeeee: Exponent base 2 excess 127 */ #include #define DLOOP 146 int main( ) int k; float ans = 1.0; for(k=1; k= DLOOP; +k) ans = ans/2.0; /* ans = pow(2, -146);*/ for(k=1; k= 7; +k) printf(= %12.8gn, ans ); ans /= 2.0; return 0; ,= 1.1210388e-44 = 5.6051939e-45 = 2.8025969e-45 = 1.4012985e-45 = 0 = 0 = 0,16,再 談 變數 Variable 變 變 變,變數當然會變才叫變數 廢話! 變數命名規則與慣例? averageScore, n, x 變數放在電腦的記憶體 何處? 與程式一起, 與程式分離? 由Linker 負責 堆疊區 (STACK) ? 嘿洗蝦密碗糕啊? 變數的能見範圍(Scope)? 或說有效範圍 函數(函式 )內的變數:Local variable 局部(區域)變數 函數外的變數: Global variable整體變數 變數何時生出?何時死掉? 生命期 (Life time) ?,17,變數分類,依能見範圍(Scope, 或說有效範圍)? Local 區域 vs. Global 整體 依何時給它記憶體位置 (稱作binding) Static variable : 靜態變數, Run 之前 (compile / Link) Dynamic variable: 動態變數, Run 之後才會做 binding(就是Run 之後才給它位置) 自動要自動還: auto 變數 (function內沒寫static) 手動要: 使用malloc 或 calloc 要, 用free還 (C+用new 和delete) auto 變數在 stack 堆疊 手動要的(malloc, calloc)在 Heap 堆積,18,變數 (Variable) 的 Scope, Life time,變數的能見範圍(Scope, 或說有效範圍) 函數(函式 )內的變數:Local variable 局部(區域)變數 : 只有在函數內有效 函數外的變數: Global variable (整體變數) Global variable : 宣告之後就一直有效 變數生命期 (Life time) ? 靜態變數從程式開始 Run 就“出生” Auto 變數在進入函數時生出, 離開函數時死掉(把記憶體還給系統) 沒有宣告 static 的:Local變數就是Auto 變數,19,Global 變數 vs. Local變數,#include int x = 38, y=250; /* Global */ void sub1(int); /* 見後面程式 */ int main( ) int x=49; /* Local */ sub1(:x); /* Global 的 x = 38 */ sub1(x); /* Local 的 x = 49 */ cout x= x endl; cout outside x= :x endl; cout y= y endl; return 0; /* 0 in main( ) means OK */ ,20,Global 變數 vs. Local變數,void sub1(int y) cout y= y endl; x+; /* Global 的 x , 因為 Local 沒有 x*/ :x+; /* 也是 Global 的 x */ y+; /*因為y是 pass by value, 回去不會變*/ :y+; /* 是 Global 的 y */ return ; /* 不寫也可以 */ ,(Cont),pass by value 就是 call by value 講法不同而已 ,21,Static Local變數,#include int fa( ) int x = 1; return x+; /*先取其值, 再做 + */ int fb( ) static int x = 1; /* 注意 static int x = 1; */ return x+; int main( ) cout fa( )= fa( )fa( )fa( ) endl; cout fb( )= fb( )fb( )fb( ) endl; return 0; /* 0 in main( ) means OK */ ,return x+; 和 return +x; 不同 !,22,Static Local變數,int fa( ) int x = 1; return x+; /*先取其值, 再做 + */ int fb( ) static / 把 static 寫在下列左方也一樣 int x = 1; / 注意 static int x = 1; return x+; ,(Cont),23,Static Local變數 , evaluation 順序,#include int fa( ); /*宣告*/ int fb( ); int main( ) /* 不同系統可能不同答案*/ printf( fa( )=%d %d %d n, fa( ), fa( ), fa( ) ); printf( fb( )=%d %d %d n, fb( ), fb( ), fb( ) ); return 0; /* 0 in main( ) means OK */ / int fa( ) 也可以寫在另一個檔案內,24,Evaluation 順序,#include int gg(int x) static ans=0; ans = x + ans*ans; return ans; int main( ) /* 不同系統可能不同答案 !*/ int haha = gg(1) + gg(2) + gg(3); /*先做哪個gg() ?*/ printf(haha=%dn, haha); return 0; ,expression中有多個函數叫用會先做哪個 gg( )? C/C+ 沒有規定! 所以看寫 compiler的人高興,25,Auto 變數不可佔太多memory,Auto 變數就是沒寫 static 的 Local 變數 Auto 變數是在進入函數時才在STACK區安排記憶體, 在離開函數(return)時就還掉(改變 Stack Pointer) STACK區一般不會很大(幾拾 K Bytes) Auto 變數用STACK區, 所以太大的array不能用 叫用函數時, return address 也會被推入 STACK 參數傳遞也是用 STACK區 C/C+推入參數時是先推入最後一個參數, 這使得第一個參數會在堆疊的最上方, 進入函數時, STACK中 return address 之下就是第一個參數 C/C+離開函數時, 函數不負責拿掉STACK中 的參數, 那是叫用函數那個程式的責任! (與其它語言不同),26,Auto 變數佔用STACK區memory,Auto 變數就是沒寫 static 的 Local 變數,CPU,IP,SP,Instruction Pointer,Stack Pointer,系統區,系統區,程式+靜態data,HEAP堆積 malloc( ), new( ),STACK (參數與Auto變數),Heap 由上往下長 Stack 由下往上長,27,Static變數?,Global 變數都是static 的 變數 Local 變數就是寫在函數內的變數 有補 static 修飾詞則為 static變數 (靜態變數) 沒有補 static 修飾詞則為 Auto 變數 (自動變數) static 的 變數在程式開始 RUN 之前就存在, 且已經設好初值, 程式結束後才會還掉所佔記憶體(生命期) 寫了 extern 表示只是 宣告, 不是定義(define) Local 變數就只能在該函數內存取 Global 變數則只要看得見它的任一函數都能存取它 宣告之後就看得見, 沒宣告就定義 則看作 同時宣告了 注意 main( ) 也是函數, 沒有特別偉大!,28,Global, Static Local, Auto 變數,#include extern int x; /* 只有宣告 , 還不知道位置在何處?*/ int fa( ) ; int fb( ) int ans=0; return +ans; int main( ) int kk=123; cout fa( )= fa( )fa( )fa( ) kkendl; cout fb( )= fb( )fb( )fb( ) endl; return 0; /* 0 in main( ) means OK */ int x, y; /* 真的 x 在這, 也可以寫在另一個 file 中*/ int fa ( ) /* ,寫了 extern 表示只是 宣告, 不是定義(define),29,Static Global 變數,#include #define BUFSIZE 100 static char bufBUFSIZE; static int bufp = 0; int getch( ) /* . . . */ void ungetch(int c) /* . . . */ ,Information Hiding?,也參考stack的push和pop寫在同一獨立 file 中, push和pop共享 data,30,再談Static Global 變數,#include #define RAND_MAX 65535 static unsigned long seed=0; /* global */ int rand( ) seed = seed * 1103515245 + 12345; return seed % (RAND_MAX+1); void srand(int newseed) seed = newseed; ,程式庫中的 Pseudo random number,Information Hiding?,只有rand 和srand 看得見 seed,31,register 變數 , volatile 變數,#include enum BUFSIZE=100, NSTU=60; register int wrong; /* this is wrong, global不可*/ volatile int haha; void myfun(register int x ) register int yy, i; /* OK */ int * p; /* . . . */ p = /* this is wrong */ ,參考K /* tell compiler */ int main( ) int k; double ans; for(k=1; k=99; +k) /* . . . */ ans = haha * haha; /* do not optimize*/ printf( ans=%fn , ans); ,參考K /* */ tmp = xi; xi = xk; xk = tmp; 增加程式可讀性 程式更容易維護,35,What is Object? Class? (1/2),object 就是“東西”, 就是以前的“變數” class 就是“類別”, 某種東西的型別 int m, n; /* int 是整數類別 */ /* m, n 都是變數 */ Student x, y; /* Student 是我們自訂類別 */ /* x, y 都是 object */,36,What is Object? Class? (2/2),object is an instance of some class 文法上class 就是以前的 struct class Student public: /* 與寫 struct 同 */ ; struct Node private: /* 與寫 class 同 */ ;,37,Object Concept (1/3),Why using class 支援 OOP (物件導向Object Oriented Programming) 又譯作 個體導向程式設計 考慮 Stack 堆疊 : 讓東西用起來更像東西 ADT (Abstract Data Type) 把 data 以及對 該些 data 有關的方法(method)或稱函數(function, 函式)封裝(encapsulate)在一個程式單元方便使用,38,Object Concept (2/3),Object-Oriented Analysis (OOA) Goal: Understand the domain Object-Oriented Design (OOD) Goal: Design a solution, a model of the domain in which the desired activities occur Object-Oriented Programming (OOP) Goal: Implement the solution Note: A Good Design is 2/3 Before You Hit the Keyboard,39,OO concept (3/3),#include mystk.h #include int main( ) Stack xo; /* xo is an object, it is a Stack */ xo.push(880); /* 要求 xo 把 880 push 進去 */ xo.push(770); xo.push(53); while(! xo.isempty( ) ) cout xo.pop( ); /* 要求 xo 吐出頂端元素 */ /* . . . */ cout endl; return 0; ,How? 如何製做 Stack?,40,Struct 自訂資料型別(1/5),#include struct Student long sid; char name9; /*可存四個Big5中文 */ float score13; /*每人最多修13 科 */ ; /*注意struct與class之 分號不能省掉*/ int main( ) struct Student x; /* C+ 和 C99 不用寫 struct */ x.sid = 123; /* dot notation */ strcpy(x.name, 張大千) ; /*注意字串不能= */ /* 用 loop 把成績讀入 x.score? */ ,考慮寫個程式處理學生的成績資料, 如何表示一個學生的資料? 想一想. . .,41,struct - Structure (2/5),struct Date int day, month, year; ; void main ( ) struct Date birthday = 14, 2, 1999 ; struct Date today; today.day = 28; today.month = 2; today.year = 2003; printf(This year is %d, today.year); ,today,today.day,today.month,today.year,birthday,birthday.day,birthday.month,birthday.year,birthday,today,42,Structure (3/5),Group related fields into a structure,typedef struct date Date;,struct date int day, month, year; ;,struct date today;,Date today;,C99 和 C+則只要寫 date today;,43,Structure (4/5),兩種定義一次寫完,typedef struct date int day, month, year; Date; /* Date為type*/,struct date today; or Date today;,注意這意思不同! struct date int day, month, year; Date; /*Date為變數*/,44,Structure (5/5),Student 是 type,typedef struct student long sid; char name9; float height; double wet; /*weight*/ Student;,struct student long sid; char name9; float height; double wet; Student, stmp, x99;,What about this?,Student 是 變數,45,Nested Struct, Array of Struct,typedef struct int d, m, y; Date; typedef struct char name49; /* enough? */ double averageScore; Date dob; /* date of birth */ Student; /*一學生 */ typedef struct Student stud66; int numStud; SomeClass; /*一班 */,SomeClass csie1a;,You can also create an array as dynamic variable using malloc( ). The array size need not be fixed at compile time.,46,Structure as Output Parameter,void scan_date (Date *aday) int dd, mm, yy; scanf(%d %d %d, . ,We need to pass pointer to structure to a function that modifies the structure, similar to integer.,typedef struct date int d, m, y; Date;,47,Pointer to Structure(1/2),void scan_date (Date *aday) int dd, mm, yy; scanf(%d %d %d, ,A shorthand: (*aday).d same as aday-d (*aday).m same as aday-m,typedef struct date int d, m, y; Date;,48,Pointer to Structure(2/2),void scan_date (Date *aday) scanf(%d %d %d, ,If we want to set the fields of a structure by scanf, we need to pass the address of these fields. scan_date(,typedef struct date int d, m, y; Date;,49,Structure as Parameter,void print_date (Date aday) printf(%d/%d/%d, aday.d, aday.m, aday.y); /* call by value */ void scan_date (Date *aday) scanf(%d %d %d, /* pass address to pointer */,typedef struct date int d, m, y; Date;,50,Comparing content of structure,typedef struct int d, m, y; Date; int main ( ) Date day1 = 11, 2, 1999 ; Date day2 = 1, 12, 1999 ; . if (day1=day2) /* wrong */ ,Although we can use = to copy structure, we cannot compare structure by =.,51,問題與思考 (How?),如何表示 Linked List? 如何表示Stack, Queue? Stack 與 Queue 的應用 如何表示Tree? 如何儲存 - Binary Tree Representation of Tree Tree 的應用? Tree 的 traversal Expression tree vs. parsing tree,52,2002梅竹開幕,Take a Break!,53,From C to C+ (1/2),C+ 最重要的就是 class (類別) class 其實是 struct 的延伸 In C+, we can put related functions with the related data in struct ( class ) 所以 C+ 的 class 就是 C 的 struct 加上一些規定 Java 把 struct 拿掉了! 且 union 也沒了! Why using struct in C ? Group related data together class 有何用? 支援 OOP (物件導向Object Oriented Programming) 又譯作 個體導向程式設計 考慮 Stack 堆疊 . . .,54,From C to C+ (2/2),class 類別 vs. struct 結構 C+ 除了 class 外還有一些雕蟲小技 I/O stream facility and other Class Library Parameter passing? (C+可指定call by reference) function name overloading (函數名稱重複使用) Operator overloading, n= 5+38; x= 25.8+3.69; inline function ? (只影響編譯與執行效率, 不影響答案) template function, template class,55,Function name Overloading,/* 再寫一個函數可以將兩double數對調, C+專有*/ #include void swap( int ,(C+專有),C+允許同名的 function, 但注意參數,56,用 array 來做 STACK (1/2),sptr,Stack Pointer int sptr;,0,1,-1,int x99;,void push (int y) +sptr; xsptr = y; ,int ans = pop( );,Initialization: sptr = -1; /*empty*/,需要一個array 和一個整數,push (456) ;,push (3388) ;,456,3388,int pop ( ) int tmp=xsptr; -sptr; return tmp; ,98,57,用 array 來做 STACK (2/2),static int x99; /* 別的檔案看不到這*/ static int sptr = -1; /* empty stack */ void push(int y) +sptr; /* move stack pointer */ xsptr = y; /* put the data y there */ int pop( ) return xsptr-; /*注意 - 寫後面*/ /* 其它相關 function例如 isempty( ) */,58,使用 STACK,extern void push(int); /*宣告*/ extern int pop( ); #include int main( ) push(543); push(881); printf(%dn, pop( ) ); /* 可以用! 但若要兩個 Stack 呢? */,59,More about STACK (1/3),/* 若要兩個 Stack 呢? */ 解法之一 = 使用兩個 array, 並把 array 當參數 push(ary2, x); ans = top(ary2); 但是這樣該些 array 必須 開放讓使用者知道 要讓宣告 extern就看得到, 不可再用 static Global 違反不必讓使用者知道push到哪去的 Information Hiding 原則 ! (或是該些 array 要定義在使用者程式裡面),60,More about STACK (2/3),/* 若要兩個 Stack 呢? */ 解法之二 = 整個檔案複製到另一個 file, 並把 各相關函數名稱都改掉, 例如 push2, pop2, = array 與變數名稱不用改! Why? (因為 static Global) 但是這樣 也不是很方便, 函數名稱一大堆 若要三個 Stack 呢? 四個呢? (不過這比前面使用不同 array 的方法好!),61,More about STACK (3/3),/* 若要兩個 Stack 呢? */ /* 若要三個 Stack 呢? 四個? 五個 . . . */ /* 有沒有更方便直接的方法? */ = using C+ Class to define a Stack as a Software Component (軟體元件或零件) Stack x; Stack y; x.push(13579); y.push(258); y.push( x.pop( ) );,62,Stack - class example ( 1/2) mystk.h,class Stack / private: long data99; /* 直接寫 99 不是好習慣*/ int sptr; public: Stack( ) ; /* constructor */ void push( long ); long pop( ); bool isempty( ); ; /* class Stack */,bool 要新版的 C+ 才有,63,Stack - class example (2/2) mystk.cpp,#include mystk.h #include Stack:Stack( ) sptr = -1; /*Constructor*/ void Stack:push( long xx) /*注意 Stack: */ data +sptr = xx; long Stack:pop( ) return datasptr-; bool Stack:isempty( ) return sptr = -1; / . . .,bool 要新版的 C+ 才有,這是實作出 Stack, 前面 mystk.h只是宣告,Initialization 工作 要寫在特殊函數,64,Stack (使用) mymain.cpp,#include mystk.h #include int main( ) Stack xo, brandy; xo.push(880); xo.push(770); xo.push(53); while(! xo.isempty( ) ) cout xo.pop( ); cout endl; return 0; ,65,Stack (使用) 如何執行?,#include mystk.h #include int main( ) Stack xo, brandy; xo.push(880); xo.push(770); xo.push(53); while(! xo.isempty( ) ) cout xo.pop( ); cout endl; /* new Line*/ return 0; ,53 770 880,gcc mymain.cpp mystk.cpp ./a.out,gcc c mystk.cpp gcc mymain.cpp mystk.o ./a.out,66,Stack (使用) 可全寫在一file, 但?,class Stack / private: long data99; /* 直接寫 99 不是好習慣*/ int sptr; public: Stack( ) sptr = -1; ; void push( long xx) data +sptr = xx; long pop( ) return datasptr-; bool isempty( ) return sptr = -1; ; #include int main( ) Stack xo, brandy; xo.push(880); xo.push(770); xo.push(53); while(! xo.isempty( ) ) cout xo.pop( ); cout endl; return 0; ,壞處: 若要 reuse Stack 呢? 切下來另存檔案? 不方便! 不符合 OOP 原則,67,class 內要用 enum 生出常數,/* g+ thisfile.cpp ; ./a.out */ #include using namespace std; #include class Stack / private: public: enum size=99 ; /* 正確的好習慣*/ private: long datasize; /* 直接寫 99 不是好習慣*/ int sptr; /* stack pointer */ public: Stack( ) ; void push( long ); long pop( ); bool isempty( ); ; /* class Stack */ Stack:Stack( ) void Stack:push( long y) long Stack:pop( ) bool Stack:isempty( ) ,int main( ) Stack x; cout Ha ha ha endl; cout stack size = Stack:size endl; cout Size of x = x.size endl; ,68,問題與思考 (Stack size),Stack 內的 array 一定要固定 99元素嗎? = using pointer 就可做出可變大小的array class Stack long * data; int mySize; Stack(int sz=99); /* . . . */ ; Stack:Stack(int sz) sptr = -1; mySize=sz; data = (long*) malloc(sz * sizeof(long) ); ,使用時指定大小: Stack xo(66); Stack brandy;,69,Stack 應用 again (1/2),Infix expression Postfix expression Infix expression: 12+(2*3)/5 Prefix : +12 / * 2 3 5,* + /,Postfix: 12 2 3 5,70,問題與思考 (Why?),現在用 class 製作軟體零件(元件), 雖可以有許多個 long 的 Stack 若要一個 long 的 Stack 以及一個 double 的 Stack 呢? 甚至一個 Student 的 Stack 呢? ? Copy 來改並換 class 名稱嗎? No = using C+ template Class (樣版類別),71,C+ Template (樣版),Templates provide direct support for generic programming The C+ template mechanism allows a type to be a parameter in the definition of a class or a function definer specifies the container class in terms of that argument users specify what the type of contained objects is The template implementation is a mechanism that generates types when needed based on the users specification (compiler 幫忙copy去改 ) Every major standard library abstraction is represented as a template,72,C+ Function Template (樣版函數),template void swap( T /* */ ,When a template function is called, the type of the function arguments determine which version of the template is used. That is the template arguments are deduced from the function arguments,如何使用 swap( ) 函數?,73,C+ Class Template,如何有多個可處理不同 data type的堆疊?,Template class Stack T data99; int sptr; public : Stack( ); void push(T x); T top(void); void pop(void); int empty( ) ; / . . . ; Stack:Stack( ) sptr = -1; /* */,template declaration, T is type argument,T is uesd exactly like other type names,Stack xo; Stack brandy; Stack haha; /* */,Class name is used exactly like others But you have to specify the type in ,不該改的不要改, 例如 int sptr; 當然不改,74,Stack (使用Library 的 stack),#include #include using namespace std; /* where the Library in */ int main( ) stack xo; /* 注意用法 stack */ stack brandy; /* 注意用法 */ xo.push(880); xo.push(770); xo.push(53); while(! xo.empty( ) )/* 注意 empty 不是 isempty */ cout xo.top( ); /* 注意用 top( ) */ xo.pop( ); /* pop is void type */ cout endl; /* new Line*/ return 0; ,53 770 880,gcc thisfile.cpp ./a.out,C+程式庫的stack是 Template Library,75,Stack 應用 again (2/2),使用堆疊把 infix postfix Stack 內放運算符號(operator)和左括號 使用堆疊計算 postfix 的答案 Stack 內放運算元素(operand)或說 value (整數或實數),76,認真看 class,C語言的struct 目的是把相關資料 group 在一起 class (類別)就是原來的 struct with some regulations class is a user-defined data type, 自訂的, 抽象的 所以 class 是 Abstract Data Type (ADT) ADT 就是把 data 以及對這些 data有關的動作(method, 就是 function)一起封藏(Encapsulate)在一個程式單元(program unit)之內, 例如 C+ 用 class來封藏 Class 可用來設計軟體元件(Software Component) Class 內的 data member/method member 存取控制: private, protec
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!