1、Chapter 6程式結構進階介紹目標n函式指標n例外情形及例外處理n同名函式撰寫n函式樣板n組合多個函式成一完整程式函式指標n以指標儲存函式位址n傳回值型態 (*指標名稱)(參數列型態);nEx:qdouble(*pfun)(char*,int);qdouble*pfun(char*,int);函式指標初始值設定nlong sum(long num1,long num2);nLong(*pfun)(long,long)=sum;nornlong product(long,long);nLong(*pfun)(long,long);npfun=product;Example Ex6_01.cp
2、pfigure p208npdo_it(product(3,pdo_it(4,5),6);以函式指標為引數n函式的參數是函式指標nEx6_02.cpp函式指標陣列ndouble sum(double,double);ndouble product(double,double);ndouble difference(double,double);ndouble(*pfun3)(double,double)=sum,product,difference;函式參數初始化/EX6_03.CPPvoid showit(char*=Something is wrong.);int main(void)ch
3、ar*mymess=The end of the world is nigh.;showit();/Display the basic message showit(Something is terribly wrong!);/Display an alternative showit();/Display the default again showit(mymess);/Display a predefined message cout endl;return 0;void showit(char*message)cout endl message;return;例外情形n處理錯誤或非預期
4、狀況的機制覆載函式(function overloading)double maxdouble(double array,int len)double max=array0 doe(int i=1;ilen;i+)if(max arrayi)max=arrayi;retrun max;nhow about long,int,etc.覆載函式(cont.)n相同名稱的一系列函式n各有不同的參數列n注意!僅有傳回值不同,並不足以區分函式!Example Ex6_06.cppint max(int array,int len);/Prototypes forlong max(long array,i
5、nt len);/a set of overloadeddouble max(double array,int len);/functions覆載函式的意義n相同運算式使用相同函式名稱處理不同運算元qEx:“+”對int,long,double,n實質上相同,但引數型態不同,都應覆載,使用相同函式名稱函式樣板(Function Templates)n函式具有不同變數及參數型態n程式碼相同n一群特定程式碼n依照參數型態不同自動產生不同的版本使用函式樣板改寫 max()n關鍵字template表示樣板定義nn由樣板建立的函式實體稱為實例(instantiation)/Template for fu
6、nction to compute the maximum element of an arraytemplate T max(T x,int len)T max=x0;for(int i=1;ilen;i+)if(maxxi)max=xi;return max;Example Ex6_07.cpp#include using namespace std;/Template for function to compute the maximum element of an arraytemplate T max(T x,int len)T max=x0;for(int i=1;ilen;i+)
7、if(maxxi)max=xi;return max;int main(void)int small=1,24,34,22;long medium=23,245,123,1,234,2345;double large=23.0,1.4,2.456,345.5,12.0,21.0;int lensmall=sizeof small/sizeof small0;int lenmedium=sizeof medium/sizeof medium0;int lenlarge=sizeof large/sizeof large0;cout endl max(small,lensmall);cout endl max(medium,lenmedium);cout endl max(large,lenlarge);cout endl;return 0;Homeworknreading example p224239n6-1,6-2Chapter 7舊式Windows的滋味目標n結構n視窗基本架構nWindows APInWindows messagenWindows程式常用表示法nWindows程式基本架構