第13章 多態(tài)性(例子)_第1頁
第13章 多態(tài)性(例子)_第2頁
第13章 多態(tài)性(例子)_第3頁
第13章 多態(tài)性(例子)_第4頁
第13章 多態(tài)性(例子)_第5頁
已閱讀5頁,還剩26頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、例13.5 在類中,用字符數組實現(xiàn)字符串。#include #include class Student char Num10; /學號,注意:用數組實現(xiàn) char Name10; /姓名,注意:用數組實現(xiàn) int Score; /成績public:Student(char num =NULL, char name =NULL, int score=0)if(num)strcpy(Num, num);else Num0=0;if(name)strcpy(Name, name);else Name0=0;Score=score;void Show( )coutNum=NumtName=Namet

2、Score=Scoreendl;void main( )Student stud1(01201, Mary, 88), stud2;stud2=stud1; /Astud1.Show( );stud2.Show( );程序的運行結果是:Num=01201 Name=Mary Score=88Num=01201 Name=Mary Score=88返回ppt講稿 例13.7 += 運算符重載函數的返回值為void#include class Complexdouble Real, Image;public:Complex(double r=0, double i=0) Real=r; Image

3、=i; void operator+=(const Complex &c)/返回值為void型Real+=c.Real;Image+=c.Image;void Show( ) cout0) cout+Imagei; /如果是正數,必須輸出正號else if(Image0) coutImagei; /如果是負數,自動輸出負號coutendl;void main( )Complex c1(2, 3), c2(4, -2);coutc1=;c1.Show( );coutc2=;c2.Show( );c1+=c2; / Acoutc1+=c2;c1=;c1.Show( );運行結果如下:c1=2+3i

4、 c2=4-2i c1+=c2;c1=6+1i 程序中的A行被處理成 c1. operator+=(c2)。如果有連續(xù)賦值的表達式,如c1+=c2+=c3,編譯器會處理成 c1. operator+=( c2. operator+=(c3) ),注意劃線部分是第一次賦值,表達式的結果是void類型,此時若欲做第二個賦值就不行了。將上例中的operator+=( )重載函數改寫成:Complex operator+=(const Complex &c) / 返回值為本類類型的對象Real+=c.Real;Image+=c.Image;return *this;此時處理c1+=c2+=c3就沒有問

5、題了。返回ppt講稿 例13.8 對于字符串類,重載 = 運算符,返回對象自身的引用。就本例而言,可以不定義拷貝構造函數,程序能正確運行。#include #include class Stringchar *Strp; public:String(char *s=0) / 構造函數if(s)Strp=new charstrlen(s)+1;strcpy(Strp, s);else Strp=0; String & operator = (String & s) / A賦值運算符重載函數if(Strp) delete Strp;if(s.Strp)Strp=new charstrlen(s.S

6、trp)+1;strcpy(Strp, s.Strp);else Strp=0;return * this; String( ) / 析構函數if(Strp) delete Strp;void Show( )coutStrpendl; ;void main( )String str1(Sample String1); String str2; str2=str1; / 對象賦值運算str2.Show( ); / 輸出 Sample String1但是若將程序中A行的函數頭部改為:String operator = (String & s) ,即返回本類對象,因類中有動態(tài)申請的字符串空間,則必須

7、增加定義拷貝構造函數。因為要使用返回的對象去初始化內存臨時對象,所以應在類體中增加定義拷貝構造函數如下:String(String & s) / 拷貝構造函數if(s.Strp)Strp=new charstrlen(s.Strp)+1;strcpy(Strp, s.Strp);else Strp=0;返回ppt講稿 例13.11 成員函數和類型轉換函數的比較#include class RMB /定義一個“人民幣”類 int Yuan, Jiao, Fen; /元、角、分 public:RMB(int y=0, int j=0, int f=0) Yuan=y; Jiao=j; Fen=f;

8、 operator int( ) /轉換成整數值(結果單位:分)return (Yuan*100+Jiao*10+Fen); operator double( ) /轉換成實數值(結果單位:元)return (Yuan+double(Jiao)/10+double(Fen)/100); int GetFen( ) /用成員函數返回“分”值return (Yuan*100+Jiao*10+Fen);void main(void)RMB r(23, 8, 6);int r1, r2, r3;r1=r ; /A 處理成 r1 = int(r);本質與B行一致r2=int(r); /B 處理成 r2

9、= r.operator int( );r3=r.GetFen( ); /C 調用成員函數得到分值 coutr1=r1endl;coutr2=r2endl;coutr3=r3endl;double d;d = r; /D 處理成 d = r.operator double( ); coutd=dendl;程序的運行結果如下:r1=2386r2=2386r3=2386d=23.86返回ppt講稿 假定一種貨幣只有元和角兩種單位,一元等于10角,+和 - 表示加減1角。定義一個類Money 用于實現(xiàn)+和 - 運算。例13.12 實現(xiàn)+和-的前置和后置運算符重載#include + - - 表示家減

10、1角 class Money int Yuan, Jiao; /元、角public:Money(int y=0, int j=0) Yuan=y; Jiao=j;Money(double d) Yuan=int(d); Jiao=int(d-Yuan)*10);Money operator+( ); /用成員函數實現(xiàn)前置+Money operator+(int); /用成員函數實現(xiàn)后置+friend Money operator-(Money &m); /用友元函數實現(xiàn)前置- -friend Money operator-(Money &m, int); /用友元函數實現(xiàn)后置- -void S

11、how(char *s)couts:Yuan,Jiao=10)Yuan+=1;Jiao-=10;return *this;Money Money:operator+(int) /用成員函數實現(xiàn)后置+ Money temp = *this; /保存對象原始的值 調用方式:m2 = m+;解釋為m.operator+(int )Jiao+;if(Jiao=10)Yuan+=1;Jiao-=10;return temp; /返回對象原始的值Money operator-(Money &m) /用友元函數實現(xiàn)前置- if(m.Jiao=0)m.Yuan -= 1;調用方式:m3 = -m;解釋oper

12、ator-(m)m.Jiao=10;m.Jiao-;return m;Money operator-(Money &m, int) /用友元函數實現(xiàn)后置-Money temp=m; /保存對象原始的值if(m.Jiao=0)調用方式:m4 = m-;解釋為operator- (m, int )m.Yuan-=1;m.Jiao=10;m.Jiao-;return temp; /返回對象原始的值void main(void)Money m(15, 3), m1, m2, m3, m4;coutm: ; m.Show(m);m1 = +m; coutm1=+m;n;m1.Show(m1); m.Sh

13、ow(m);m2=m+;coutm2=m+;n;m2.Show(m2); m.Show(m);m3=-m;coutm3=-m;n; m3.Show(m3); m.Show(m);m4=m-;coutm4=m-;n; m4.Show(m4); m.Show(m);Money m5(8.5); m5.Show(m5); 返回ppt講稿 例13.18 定義字符串類String,并測試重載的運算符以及成員函數/Li1318.h#include #include class Stringprotected:int Length; /字符串的長度 char *Strp; /指向字符串的指針 public:

14、String( ) Strp=NULL; Length=0; /缺省構造函數String(const char *s); /構造函數,以字符指針作為參數String(const String &);/拷貝構造函數,以字符串對象的引用作為參數?String( ) if(Strp) delete Strp; const char *IsIn( const char ) const; /返回參數字符在字符串中第一次出現(xiàn)的地址int IsSubStr(const char *) const; /判斷參數字符串是否為子串void Show( ) /輸出字符串 cout Strp n; int GetLe

15、n( ) return Length; /返回字符串的長度const char * GetString( ) return Strp; /取字符串首指針operator const char * ( ) const / A 類型轉換函數,返回指向常量的字符串指針 return (const char *) Strp; String & operator = (String &); /重載賦值運算符 friend String operator+(const String &, const String & ); /友元實現(xiàn)+重載 friend String operator-(const St

16、ring &, const char * ); /友元實現(xiàn)重載 int operator ( const String &) const;int operator = ( const String &) const;String:String(const char *s) /構造函數,以字符指針作為參數 if(s)Length = strlen(s);Strp = new charLength+1;strcpy(Strp, s);else Strp = NULL; Length = 0; String:String(const String &s) /拷貝構造函數,以字符串對象的引用作為參數需

17、要刪除原空間嗎?與賦值運算符對照?Length = s.Length;if(s.Strp)Strp = new charLength+1;strcpy(Strp, s.Strp);else Strp = NULL;const char *String:IsIn( const char c) const / B判斷字符c是否在字符串中, 如果在,返回其第一次出現(xiàn)時的地址, 否則返回空指針NULLchar *p=Strp;while(*p) if(*p+ = c) return -p;return NULL;int String:IsSubStr(const char *s) const /判斷s

18、所指向的字符串是否為類中字符串的子串。若是,返回1;若不是,返回0。if(strstr(Strp, s) return 1; / C strstr( ) 為字符串處理庫函數else return 0;String & String:operator = (String &s) /實現(xiàn)賦值運算if(Strp) delete Strp; /釋放對象自身的空間Length = s.Length;if(s.Strp)Strp = new charLength+1;strcpy(Strp, s.Strp);else Strp = NULL;return *this;String operator+(co

19、nst String &s1, const String &s2 ) /拼接兩個字符串String t;t.Length = s1.Length + s2.Length;t.Strp = new char t.Length + 1 ;strcpy(t.Strp, s1.Strp);strcat(t.Strp, s2.Strp);return t;String operator-(const String &s1, const char *s2 ) / D 刪除s1中第一次出現(xiàn)的s2String t;char *p1=s1.Strp, *p2;int i=0, len=strlen(s2);if

20、(p2=strstr(s1.Strp, s2) / 如果是子串t.Length=s1.Length - len;t.Strp = new chart.Length+1;while(p1p2)t.Strpi+=*p1+;p1+=len;while(t.Strpi+=*p1+); return t;else return s1 ; int String:operator ( const String &s) const / 實現(xiàn)小于比較,成員函數實現(xiàn) return(strcmp(Strp, s.Strp) ( const String &s) const / 實現(xiàn)大于比較,成員函數實現(xiàn) retur

21、n(strcmp(Strp, s.Strp)0 ); int String:operator = ( const String &s) const / 實現(xiàn)恒等于比較,成員函數實現(xiàn) return(strcmp(Strp, s.Strp)=0 ); /Li1318.cpp#include Li1318.hvoid Test_IsIn(char c, String &s)coutc in ; s.Show( );if(s.IsIn(c) /測試類的成員函數IsIn( ) /s.IsIn(c)返回的是c第一次在s中出現(xiàn)的地址cout Yes:s.IsIn(c)endl; else cout No:n

22、;void Test_IsSubStr(char *sp, String &s)coutsp in ; s.Show( );if(s.IsSubStr(sp) /測試類的成員函數IsSubStr( )cout Yes:n;else cout No:n;void main(void)String s1(C+程序設計 ), s2, s3(學生學習 );String s, s5;s1.Show( ); s2=s1; / 測試賦值運算符“=”s2.Show( );s=s3+s2; / 測試字符串拼接運算符“+”以及賦值運算符“=”s.Show( ); s5=s-s1; /測試字符串相減運算符“”s5.

23、Show( );String s6=C+ programming! ;Test_IsIn(g, s6); /判斷一個字符是否在“串”中Test_IsIn(k, s6);Test_IsSubStr(prog, s6); /判斷字符串是否是“串”的子串Test_IsSubStr(red, s6);couts1=;s1.Show( );couts3) /字符串比較couts3 OK!n;else if(s1s3) /字符串比較couts1s3 OK!n;else if(s1=s3) /字符串比較couts1=s3 OK!n;const char *sp1, *sp3;sp1=s1; sp3=s3;

24、/調用A行的類型轉換函數,由String類轉換成const char *類coutsp3sp1endl;couts3.GetString( )endl;程序的運行結果如下:C+程序設計C+程序設計學生學習 C+程序設計學生學習g in C+ programming!Yes: gramming!k in C+ programming!No:prog in C+ programming!Yes:red in C+ programming!No:s1=C+程序設計s3=學生學習s1s3 OK!學生學習 C+程序設計學生學習返回ppt講稿 例13.21 類的成員函數的靜態(tài)聯(lián)編#include clas

25、s Pointprotected:double x, y; /點的坐標值public:Point(double a=0, double b=0) x=a; y=b; double Area( ) /函數1 return 0.0; ;class Rectangle : public Pointprotected:double x1, y1;/長方形右下角點的坐標值,基類中x, y為左上角坐標點public:Rectangle(double a=0, double b=0, double c=0, double d=0):Point(a, b) x1=c; y1=d; double Area( )

26、 /函數2 return (x-x1)*(y-y1); ;class Circle:public Pointprotected:double r; /半徑,基類中x, y為圓心坐標點public:Circle(double a=0, double b=0, double c=0):Point(a, b) r=c; double Area( ) /函數3 return 3.14*r*r; ;double CalcArea(Point &p) return(p.Area( ); /A 編譯連接時確定調用函數1void main( )Rectangle r(0, 0, 1, 1);Circle c(

27、0, 0, 1);coutCalcArea(r)t;coutCalcArea(c)n;返回ppt講稿 例13.22 將成員函數定義成虛函數,以實現(xiàn)動態(tài)聯(lián)編#include class Pointprotected:double x, y; /點的坐標值public:Point(double a=0, double b=0) x=a; y=b; virtual double Area( ) /虛函數1 return 0.0; ;class Rectangle:public Pointprotected:double x1, y1;/長方形右下角點的坐標值,基類中x, y為左上角坐標點public

28、:Rectangle(double a=0, double b=0, double c=0, double d=0):Point(a,b) x1=c; y1=d; virtual double Area( ) /虛函數2 return (x-x1)*(y-y1); ;class Circle:public Point protected:double r; /半徑,基類中x, y為圓心坐標點public:Circle(double a=0, double b=0, double c=0):Point(a, b) r=c; virtual double Area( ) /虛函數3 return

29、3.14*r*r; ;double CalcArea(Point &p) return(p.Area( ); /A void main( )Point p(1, 2);Rectangle r(0, 0, 1, 1);Circle c(0, 0, 1);coutCalcArea(p)tCalcArea(r)tCalcArea(c)n;例13.23 成員函數調用虛函數 #include class Apublic:virtual void fun1( ) / D 虛函數 cout A:fun1 t; fun2( );void fun2( ) cout A:fun2 t; fun3( ); / E

30、virtual void fun3( ) / F 虛函數 cout A:fun3 t; fun4( );void fun4( ) cout A:fun4 n; ;class B: public A public:void fun3( ) cout B:fun3 t; fun4( );void fun4( ) cout B:fun4 n; ;void main( ) A a;a.fun1( );B b;b.fun1( ); / G程序的運行結果是:A:fun1 A:fun2 A:fun3 A:fun4A:fun1 A:fun2 B:fun3 B:fun4返回ppt講稿 BaseAB例13.26

31、虛析構函數#include #include class basechar *baseptr;public:base( ) baseptr=new char100;strcpy(baseptr, In class base);fc( ); virtual void fc( ) / D coutbaseptrendl; virtual base( ) / E delete baseptr;coutDelete baseptrendl; ;class A : public basechar *Aptr;public:A( ) Aptr=new char100;strcpy(Aptr, In cla

32、ss A);fc( ); / F void f( ) fc( ); / G A( ) delete Aptr; coutDelete Aptrendl; ;class B : public A char *Bptr; public: B( ) Bptr=new char100; strcpy(Bptr, In class B); void fc( ) coutBptr endl; B( ) delete Bptr; coutDelete Bptrendl; ;void main(void)B b; / H b.f( ); / K base *p=new B; / M delete p; / N 程序的輸出結果是:In class baseIn class baseIn class BIn class baseIn class baseDelete BptrDelete AptrDelete baseptrDelete BptrDelete AptrDelete baseptr返回ppt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論