1 / 111

Review: C++ 物件導向程式設計 Introduction to C++ with OOP

http://www.csie.nctu.edu.tw/~tsaiwn/oop/. Review: C++ 物件導向程式設計 Introduction to C++ with OOP. 3/3. 蔡文能 tsaiwn@csie.nctu.edu.tw tsaiwn@cs.nctu.edu.tw 交通大學資訊工程學系. 2010 April. Agenda. From C to C++ (C++ == a better C + Objec-Oriented support)

emilyadams
Télécharger la présentation

Review: C++ 物件導向程式設計 Introduction to C++ with OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. http://www.csie.nctu.edu.tw/~tsaiwn/oop/ Review: C++物件導向程式設計Introduction to C++ with OOP 3/3 蔡文能 tsaiwn@csie.nctu.edu.tw tsaiwn@cs.nctu.edu.tw 交通大學資訊工程學系 2010 April 交通大學資訓工程學系 蔡文能

  2. Agenda • From C to C++ (C++ == a better C + Objec-Oriented support) a. I/O stream, b. pass-by-reference c. function name overloading, d.operator overloading, e. default parameter, f.inline function, g.template function • Data Abstraction: Why using struct? Why using class? • An ADT example: How to design/construct/use a Stack? • Introduction to OOA, OOD, and UML • Inheritance and examples: Animal and Mankind, Vector and Stack • virtual function and Polymorphism: How it works? • Generic programming: template function/class, and STL • Exceptions: Scope of exceptions, raising exception, exception handlers • More tips about class and ADT: a. constructor/Destructor, b. class members vs. instance members, c.friend and access control of class members 交通大學資訓工程學系 蔡文能

  3. 問題與思考 (Why template?) • 現在用 class 製作軟體零件(元件), 雖可以有許多個內部是 long 的 Stack • 若要一個 long的 Stack 以及一個 double的 Stack 呢? 甚至一個 Student的 Stack 呢? • ? Copy 來改並換 class 名稱嗎? 太麻煩了! solution ==> using C++ template Class (樣版類別)  Generic programming 交通大學資訓工程學系 蔡文能

  4. 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 user’s specification (compiler 幫忙copy去改) • Every major standard library abstraction is represented as a template 交通大學資訓工程學系 蔡文能

  5. C++ Function Template (樣版函數) template <class T> void swap( T & x, T & y) { T temp; temp=x; x = y; y = temp; } int main( ) { int m=38, n=49; double x=12.34, y=567.135; swap(x, y); swap(m, n); /** … **/ } 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( ) 函數? 交通大學資訓工程學系 蔡文能

  6. template function Template function 與 OO 無關 template <class T> void swap( T & x, T & y) { T temp; temp=x; x = y; y = temp; } int a=38, b=49, m=123, n=456; double x=23.5, y = 33.88; swap( a, b); /* 會生出一份 swap function */ swap(x, y); swap(m, n); /* 不會再生出一份 function */ swap( ) in STL (Standard Template Library) 交通大學資訓工程學系 蔡文能

  7. 不該改的不要改, 例如 int sptr; 當然不改 C++ Class Template • 如何有多個可處理不同 data type的堆疊? template declaration, T is type argument template <class T> class Stack { T data[99]; int sptr; public : Stack( ); void push(T x); T top(void); void pop(void); int empty( ) ; // . . . }; template <class T> Stack<T>::Stack( ) { sptr = -1; } /* … */ Class name is used exactly like others But you have to specify the type in < > T is uesd exactly like other type names Stack<int> xo; Stack<double> brandy; Stack<Student> haha; /* … */ 交通大學資訓工程學系 蔡文能

  8. Summary Template class • To define Generic data type • Allow the creation of reusable code • Types are used as parameter in < > • General form: template<class T, class T2> class class_name { // code here }; // do NOT forget “;” 交通大學資訓工程學系 蔡文能

  9. Standard Template Library (STL) • Many template classes, functions • Abstract Data Types • Three general categories: • Containers • Iterators • Algorithms • Three kinds of containers: • Sequences • Associative • Adapted data structures in computer science 交通大學資訓工程學系 蔡文能

  10. 使用 C++ STL Library 的 stack C++程式庫的stack是 Template Library #include <stack> #include <iostream> using namespace std; /* where the Library in */ int main( ) { stack<int> xo; /* 注意用法 stack<int> */ stack<double> 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; } gcc thisfile.cpp ./a.out 53 770 880 交通大學資訓工程學系 蔡文能

  11. “first-class”Containers in STL • Sequences: • vector: Dynamic-array-backed • const-time random-access • const-time insert/delete at back • deque: double-ended queue • fast random-access - how? • fast insert/delete at front and back • list: doubly-linked list • fast insert/delete anywhere • Associative: • set: non-sequential, unique • multiset: non-sequential, non-unique • map: maps from keys to unique values • multimap: maps to non-unique values map<string,int> phone_book; cout << phone_book["Jack"] //prints Jack’s phone number Map is also known as Associative array or “dictionnary” 交通大學資訓工程學系 蔡文能

  12. Container member ops & functions • copy constructor • empty( ) • size( ) • swap( ) • First-class: • begin( ) • end( ) • rbegin( ) • rend( ) • erase( ) • clear( ) Consistent interface 交通大學資訓工程學系 蔡文能

  13. STL iterators • Standard way to traverse through container: iteration • Abstraction of both “index” and “pointer” • just: means of iterating • forward, back, etc. • Iterator direction types: • Forward iterator • Reverse iterator • both supported by vector, list, etc. • Random-access iterator • supported by vector • Can be const or not 交通大學資訓工程學系 蔡文能

  14. Types of iterators • I/O iterators are “one-pass” • can only move in one direction • can only traverse once –p++ • Other types: • bidirectional: p++, p-- • random-access: p + i, p - i, p[i] *(p+i), p1 < p2 • vector: random-access • deque: random-access • list: bidirectional • set/multiset: bidirectional • map/multimap: bidirectional 交通大學資訓工程學系 蔡文能

  15. STL iterators – iterio.cpp • Access set of values from one place • Usually, place is a container • But: input stream may be construed as a place #include <iostream> #include <iterator> using namespace std; void main() { cout << “Enter two nums: “; istream_iterator<int> intIn(cin); int x = *intIn; intIn++; x += *intIn; ostream_iterator<int> intOut(cout); cout << “The sum is: “; *intOut = x; cout << endl; } 交通大學資訓工程學系 蔡文能

  16. vector : 會自己長大的 array • Most commonly used container class • Fast random access • random-access iterators • Can access mems • with []s like arrays – unsafe • with at(i)– checks bounds, throws exception – safer • Essentially: dynamic array hidden in obj • add to/delete from back: very fast, const time • unless run out of space •  automatically copy to larger array • insert/del from middle: linear time • must move half of mems forward/back 交通大學資訓工程學系 蔡文能

  17. vectors <vector> • Similar to Java’s Vector in that: • dynamic-array-backed list • same complexities • Different in that: • takes instance of specified type • vector<int> nums; • vector<double> vals(20); • size-20 vector of doubles • vector<Base> objs; • takes Base objects • vector<Base*> ptrs; • takes Base*s 交通大學資訓工程學系 蔡文能

  18. vector 使用範例 #include <iostream> #include <vector> using namespace std; int main(){ vector<int> haha; //vector of integers //append elements 1 ->6 for(int i=1; i<=6; ++i){ haha.push_back(i); } // print all elements followed by a space for(int i=0; i< haha.size(); ++i){ cout << haha[i] << ‘ ‘ << endl; } } //output = “1 2 3 4 5 6” 交通大學資訓工程學系 蔡文能

  19. STL – deque • Deque 當名詞唸作 de-k ; 若唸 dee-queue 意為從queue拿第一個 • Term Deque is an abbreviation for “Double Ended Queue” • A dynamic array implemented so it can grow in both dimensions. • Inserting elements at beginning and the end is fast • Inserting elements in the middle takes time since elements must be moved • It is almost exactly the same as the vector example except that… • push_front() is used. Therefore order of elements is reversed on output. • Could use push_back()member function also. • push_front is not provided for vectors (Why?) 交通大學資訓工程學系 蔡文能

  20. deque example #include <iostream> #include <deque> using namespace std; int main(){ deque<float> haha; //deque of floats //append elements 1 ->6 for(int i=1; i<=6; ++i){ haha.push_front(i*1.1); } // print all elements followed by a space for(int i=0; i< haha.size(); ++i){ cout << haha[i] << " " << endl; } } // Output = “6.6 5.5 4.4 3.3 2.2 1.1” 交通大學資訓工程學系 蔡文能

  21. STL – List • Implemented as a doubly linked list of elements. • Each element has its own section in memory, and refers to its next and previous elements. • Insertion and removal of elements are fast at ANY position. Only the links must be changed. • Moving elements in the middle of a list is very fast, when compared to a vector or a deque. • Lists don’t provide random access. • Eg. – To get at the 10th element, you must traverse the first 9 by following a chain of links. – Slow! 交通大學資訓工程學系 蔡文能

  22. list example #include <iostream> #include <list> using namespace std; int main( ) { list<char> hehe; //list of characters //append elements 'a' -> 'z' for(char i= 'a'; i<= 'z' ; ++i) { hehe.push_front(i); } // print all elements followed by a space while(!hehe.empty( )){ cout << hehe.front( ) << " "; hehe.pop( ); } } // Output = “a b c d e f g h I j k l m n o p q r s t u v w x y z ” 交通大學資訓工程學系 蔡文能

  23. Other Containers in STL • Container adapters: • use “first-class” containers by composition • stack: LIFO deque • queue: FIFO deque • Priority_queue --- queue sorted by value • Near-containers: • arrays • string • bitset • valarray 交通大學資訓工程學系 蔡文能

  24. C++ string #include<string> // 不是 <string.h> using namespace std; string s1 = "James "; string s2 = " Bond "; string s3 = s1 + " " + s2; // concatenate 串接起來 int main ( ) { //… if (s1.substr(0,3) == "Jim ") { s1.replace(1,2, "ames ") } printf ( " the name is %s\n ", s1.c_str( ) ); //轉為 C 型 string // . . . 交通大學資訓工程學系 蔡文能

  25. STL – Container Adapters • In addition to the fundamental container classes, C++provides special predefined container adaptersthat meet special needs. • These are implemented on top of the fundamental container classes, and consist of: • Stacks • Manage elements with a Last in First Out (LIFO) policy. • Queues • First In First Out (FIFO) policy management. Act just like a normal buffer. • Priority Queues • Elements may have different priorities. Priority is based upon a sorting criterion. (default < is used). • Act just like a buffer, where the next element out is always the element with the highest priority. 交通大學資訓工程學系 蔡文能

  26. Iterators - Set Example #include <iostream> #include <set> int main() { typedef std::set<int> IntSet; IntSet hey; hey.insert(3); hey.insert(1); hey.insert(5); hey.insert(4); hey.insert(1); hey.insert(6); hey.insert(2); IntSet::const_iterator pos; for (pos=hey.begin(); pos!=hey.end(); ++pos){ std::cout << *pos << ' '; } } 交通大學資訓工程學系 蔡文能

  27. Maps as associative arrays • A collection of key/value pairs with unique keys (as guaranteed by map) can act just as an associative array. Consider: map<string, float> theData; theData[“NULL”] = 0; theData[“PI”] = 3.14157; theData[“E”] = -1.6E-19; map<string, float>::iterator pos; for(pos=theData.begin(); pos!=theData.end(); ++pos){ cout << “key: [“ << pos->first << “]” << “value: “<< pos->second << endl; } 交通大學資訓工程學系 蔡文能

  28. Standard Algorithms (1/3) • #include <algorithm> • Needed in order to be able to call the algorithms • Functions that read or modify a sequence: find(), count(), replace(), copy(), sort(), merge() Examples: sort(my_list.begin(),my_list.end()); find(my_list.begin(),my_list.end(),key); count(str.begin(),str.end(),”EGG”); 交通大學資訓工程學系 蔡文能

  29. Standard Algorithms (2/3) • Functions that apply a given function on each element of a sequence: foreach( ), find_if( ), count_if( ), replace_if( ) Example: void draw(Shape* p) {p->draw();} void g(list<Shape*>& sh) { foreach(sh.begin(),sh.end(),draw); } 交通大學資訓工程學系 蔡文能

  30. Standard Algorithms (3/3) • Algorithms are not member functions of the containers. • Implemented as global functions that operate with iterators. • Thus implemented only once. • +ve Reduces code size, • +ve Increases flexibility • Against OO principles - it is a generic functional programming paradigm. • -ve Operation is not intuitive • -ve Some container / algorithm combinations don’t work • -ve Worse still some may work but be hideously slow. 交通大學資訓工程學系 蔡文能

  31. 問題與思考 • 使用 vector 做 stack ? • 使用 list 做 stack ? • Inheritance == extends public class MyApplet extends Applet { // Java Applet program // Applet 主程式不是 main( ) } 交通大學資訓工程學系 蔡文能

  32. 用 vector 做出 stack (1/2) [tsaiwn@ccbsd3] vectorSTK> cat -n mystk3.h 1 #include <vector> 2 using namespace std; 3 template <class T> 4 class MyStack { 5 vector <T> x; 6 public: 7 void push(T y) { 8 x.push_back(y); 9 } 10 T top( ) { 11 return x.back( ); 12 } 13 void pop( ) { 14 x.pop_back( ); 15 } 16 bool empty( ) { return x.begin() == x.end(); } 17 }; Stack contains a vector 交通大學資訓工程學系 蔡文能

  33. 用 vector 做出 stack (2/2) [tsaiwn@ccbsd3] vectorSTK> cat -n mymain3.cpp 1 // mymain3.cpp; g++ mymain3.cpp ; ./a.out 2 using namespace std; 3 #include "mystk3.h" 4 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 <iostream.h> 5 #include<iostream> 6 int main( ) { 7 MyStack <int> x; // 注意這! 8 x.push(880); 9 x.push(770); 10 x.push(53); 11 while(!x.empty( ) ) { 12 cout << x.top(); x.pop(); 13 } 14 cout << endl; 15 } [tsaiwn@ccbsd3] vectorSTK> g++ mymain3.cpp [tsaiwn@ccbsd3] vectorSTK> ./a.out 53770880 Stack contains a vector 交通大學資訓工程學系 蔡文能

  34. 用 list 做出 stack (1/2) [tsaiwn@ccbsd3] vectorSTK> cat -n mystk5.h 1 #include <list> 2 using namespace std; 3 template <class T> 4 class MyStack { 5 list <T> x; 6 public: 7 void push(const T& y) { 8 x.push_front(y); 9 } 10 T top( ) { 11 return x.front( ); 12 } 13 void pop( ) { 14 x.pop_front( ); 15 } 16 bool empty( ) { return x.begin() == x.end(); } 17 }; Stack contains a list 交通大學資訓工程學系 蔡文能

  35. 用 list 做出 stack (2/2) [tsaiwn@ccbsd3] vectorSTK> cat -n mymain5.cpp 1 // mymain5.cpp; g++ mymain5.cpp ; ./a.out 2 using namespace std; 3 #include "mystk5.h" 4 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 <iostream.h> 5 #include<iostream> 6 int main( ){ 7 MyStack <int> x; // 注意這! 8 x.push(880); 9 x.push(770); 10 x.push(53); 11 while(!x.empty()){ 12 cout << x.top(); x.pop(); 13 } 14 cout << endl; 15 } [tsaiwn@ccbsd3] vectorSTK> g++ mymain5.cpp [tsaiwn@ccbsd3] vectorSTK> ./a.out 53770880 Stack contains a list 交通大學資訓工程學系 蔡文能

  36. Stack extends list 把 list 擴充成 stack (1/2) [tsaiwn@ccbsd3] vectorSTK> cat -n mystk6.h 1 #include <list> 2 using namespace std; 3 template <class T> 4 class MyStack: list<T> { 5 public: 6 void push(const T& y) { 7 MyStack<T>::push_front(y); 8 } 9 T top( ) { 10 return MyStack<T>::front( ); 11 } 12 void pop( ) { 13 MyStack<T>::pop_front( ); 14 } 15 bool empty( ) { 16 return MyStack<T>::begin() == MyStack<T>::end( ); 17 } 18 }; // class 交通大學資訓工程學系 蔡文能

  37. 把 list 擴充成 stack (2/2) [tsaiwn@ccbsd3] vectorSTK> cat -n mymain6.cpp 1 // mymain6.cpp; g++ mymain6.cpp ; ./a.out 2 using namespace std; 3 #include "mystk6.h" 4 //注意以下是 1999之後的 C++ 新寫法, 舊法使用 <iostream.h> 5 #include<iostream> 6 int main( ){ 7 MyStack <int> x; // 注意這! 8 x.push(880); 9 x.push(770); 10 x.push(53); 11 while(!x.empty()){ 12 cout << x.top(); x.pop(); 13 } 14 cout << endl; 15 } [tsaiwn@ccbsd3] vectorSTK> g++ mymain6.cpp [tsaiwn@ccbsd3] vectorSTK> ./a.out 53770880 Stack extends list 交通大學資訓工程學系 蔡文能

  38. Exceptions handling (1/2) • For program error conditions, there is usually a need to propagate information from lower level to higher level routines. • With C, there is typically four ways this can be accomplished: • Global variables if (den == 0) g_error = BAD_DEN; • Return code values if (den == 0) return BAD_DEN; • Exit if (den == 0) exit(1); • None of these above really HANDLE the error. It merely reports it. • signal ,setjmp,andlongjmp • signal(SIGSEGV, *myisr); 可註冊聲明若 SIGSEGV (signal#11)發生之時須跳到myisr( )這我們寫的Interrupt Service Routine去處理;處理完後, 必要時可用 longjmp跳回事前用 setjmp做記號之處. (須#include <signal.h>) ( see “man signal” 以及 “man setjmp” ) 交通大學資訓工程學系 蔡文能

  39. Exceptions handling (2/2) C++ introduces exception for error handling/exception handling. (to prevent bad behavior from getting worse) • An exception is an event that is triggered by low level code to inform upper level code that an error condition has occurred. (an event is an Object) • Exceptions require the use of three new C++ keywords: try catch throw • Triggering an error condition is called throwing an exception. • Handling the error is called catching the exception. • try blocks are used to bound a set of statements that are to be monitored for all or a subset of exceptions which can be caught. 交通大學資訓工程學系 蔡文能

  40. sig.c (1/3) signal / setjmp / longjmp C 程式的 exception handling /** sig.c --- demo how to catch/handle the system signal ** by Wen-Nung Tsai. ** @CopyLeft reserved *** gcc sig.c *** a.out *******from "man signal": SYNOPSIS #include <signal.h> void (*signal(int sig, void(*func)(int))() *** In signal(), it will call func in this way: (*func)(sig); **********************/ extern long time(); /* in <sys/time.h> */ #include <signal.h> #include <setjmp.h> #include <stdio.h> void myisr1(int); /* an ISR to handle some interrupts */ void myisr2(int tni){printf("he he he!\n");}; /* for other signals */ long k=0, status=0, *p; jmp_buf env; /* for use in setjmp(), longjmp() */ // int main( ) 交通大學資訓工程學系 蔡文能

  41. sig.c (2/3) int main( ) { /*** tell the system we want to catch some signals ***/ signal(SIGINT, (void (*)(int)) myisr1 ); /* See K&R Appendix B9 */ signal(SIGSEGV, *myisr1 ); /* signal(SIGSEGV, (void (*)(int)) myisr1 ); */ printf("\nWelcome to Disney Land...\n"); srand(time(0)); /* use current time to set seed of rand() */ if(rand()%100 < 50){ if(setjmp(env)==0) /* See K&R Appendix B8 */ *p=38; /* 這本來應會segmentation fault而引起 core dump ! */ else goto eoj; /* 後面的longjmp() 會跑回此處 else ! */ } printf("Here we go!\n"); loop_a_while: while(k<12345){ printf("%ld ",k++); } printf("\n=== bye bye ===\n"); return 0; eoj: system("echo -n Now:"); system("date"); printf("\n=== abnormal stop:-(\n"); return 1; } 交通大學資訓工程學系 蔡文能

  42. sig.c (3/3) void myisr1(int x) /* Interrupt Service Routine */ { fflush(stdout); /* try to flush current buffer */ switch(x){ case SIGINT: printf("\nHey, [sig no=%d] Do NOT hit Control_C\n", x); k=12340; break; /* don't forget to take a break :-) */ default: printf("Outch! Seems Segmentation Falt or Fatal error\n"); status = 49; longjmp(env, 123); /* return a non-zero value */ } /*switch case*/ /* longjmp() will goto recent if(setjmp... */ return; } 交通大學資訓工程學系 蔡文能

  43. sig.c 說明 • 這個程式是用來示範 exception handling 的。 • 首先我們先用 signal 函數告訴系統說如有發生錯誤的情形要叫那個 ISR 函式, 在這裡是指 user敲Control_C (SIGINT) 和 segmentation falt的情形。 其中 SIGSEGV falt是會由 *p=38 引起(because沒有allocate位置給 *p), 而會不會執行該statement 乃是由亂數決定(if(rand()%100<50)... ) • 在 ISR 中顯示出錯誤訊息後, 若是 SIGSEGV則顯示 Outch! Seems Segmentation Falt or Fatal error, 然後因 longjmp會跳回第一次執行 setjmp 的地方, 此時因 setjmp()得到非 0 (由longjump 函數返回) 就會跳到eoj顯示時間的訊息, (system call) • 若是 Control_C 的中斷, 顯示 Hey, [sig no=2] Do NOT hit Control_C 後, 則跳回原發生中斷處(此時 k == 12340), 繼續印到程式結束。 • 若無發生任何錯誤, 程式便從 1 印到 12344, 然後結束。 交通大學資訓工程學系 蔡文能

  44. These signals are defined in the file <signal.h> • No Name Default Action Description • 1 SIGHUP terminate process terminal line hangup • 2 SIGINT terminate process interrupt program • 3 SIGQUIT create core image quit program • 4 SIGILL create core image illegal instruction • 5 SIGTRAP create core image trace trap • 6 SIGABRT create core image abort program (formerly SIGIOT) • 7 SIGEMT create core image emulate instruction executed • 8 SIGFPE create core image floating-point exception • 9 SIGKILL terminate process kill program • 10 SIGBUS create core image bus error • 11 SIGSEGV create core image segmentation violation • 12 SIGSYS create core image non-existent system call invoked • 13 SIGPIPE terminate process write on a pipe with no reader • 14 SIGALRM terminate process real-time timer expired • 15 SIGTERM terminate process software termination signal • 16 SIGURG discard signal urgent condition present on socket 交通大學資訓工程學系 蔡文能

  45. <signal.h> (cont.) • 17 SIGSTOP stop process stop (cannot be caught or ignored) • 18 SIGTSTP stop process stop signal generated from keyboard • 19 SIGCONT discard signal continue after stop • 20 SIGCHLD discard signal child status has changed • 21 SIGTTIN stop process background read attempted from control terminal • 22 SIGTTOU stop process background write attempted to control terminal • 23 SIGIO discard signal I/O is possible on a descriptor (see fcntl(2)) • 24 SIGXCPU terminate process cpu time limit exceeded (see setrlimit(2)) • 25 SIGXFSZ terminate process file size limit exceeded (see setrlimit(2)) • 26 SIGVTALRM terminate process virtual time alarm (see setitimer(2)) • 27 SIGPROF terminate process profiling timer alarm (see setitimer(2)) • 28 SIGWINCH discard signal Window size change • 29 SIGINFO discard signal status request from keyboard • 30 SIGUSR1 terminate process User defined signal 1 • 31 SIGUSR2 terminate process User defined signal 2 Signals allow the manipulation of a process from outside its domain as well as allowing the process to manipulate itself or copies of itself (children). (see “man fork”, “man pthread”) 交通大學資訓工程學系 蔡文能

  46. Stack with Exception handling void Stack::push(const long item) throwsBoundExp { if ( (sp+1) >= sizeof(data)/sizeof(data[0])) throwBoundExp(“stack overflow”); data[++sp] = item; //ok if here } //assume empty condition: sp == -1 • What is BoundExpin this example? • A class we defined (see next slide) 交通大學資訓工程學系 蔡文能

  47. An exception class example class BoundExp: public exception { public: BoundExp(const string &s): exception( (const exception&)s ) { } // const char* what( ){ return "Bad news!"; } }; • It’s just a class • Its parent is exception, but needn’t be ! • exceptionhas function what() • maybe other information 交通大學資訓工程學系 蔡文能

  48. Throwing and catching exception Stack s; try { //… s.push(38); // may cause exception } catch (BoundExp &ex) { cout << “Error: “ << ex.what() << ‘\n’; } catch (Exception_Type2 &ex) { // can catch multiple kinds of exception //… } catch (…) { //真的寫點點點… is a wildcard! cout << “Unknown exception caught.\n”; } 交通大學資訓工程學系 蔡文能

  49. Exception classes in C++ • #include <exception> • class exception • #include <stdexcept> • runtime_error, logic_error • bad_alloc: when new failed • bad_cast: when dynamic_cast failed • Can throw non-exception objects • And even primitives • But handling easer if don’t 交通大學資訓工程學系 蔡文能

  50. The C++ stdexcept Hierarchy exception bad_alloc bad_cast bad_typeid bad_exception ios::failure logic_error runtime_error range_ error out_of_ range domain_error invalid_ argument overflow_error underflow_error 交通大學資訓工程學系 蔡文能

More Related