《C++程序設(shè)計(jì)教程 第2版》教學(xué)素材PPT講稿(教學(xué)課件)第13章 多態(tài)性(例子)_第1頁
《C++程序設(shè)計(jì)教程 第2版》教學(xué)素材PPT講稿(教學(xué)課件)第13章 多態(tài)性(例子)_第2頁
《C++程序設(shè)計(jì)教程 第2版》教學(xué)素材PPT講稿(教學(xué)課件)第13章 多態(tài)性(例子)_第3頁
《C++程序設(shè)計(jì)教程 第2版》教學(xué)素材PPT講稿(教學(xué)課件)第13章 多態(tài)性(例子)_第4頁
《C++程序設(shè)計(jì)教程 第2版》教學(xué)素材PPT講稿(教學(xué)課件)第13章 多態(tài)性(例子)_第5頁
已閱讀5頁,還剩24頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、例13.5 在類中,用字符數(shù)組實(shí)現(xiàn)字符串。#include #include class Student char Num10; /學(xué)號,注意:用數(shù)組實(shí)現(xiàn) char Name10; /姓名,注意:用數(shù)組實(shí)現(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( );程序的運(yùn)行結(jié)果是:Num=01201 Name=Mary Score=88Num=01201 Name=Mary Score=88返回ppt講稿 例13.7 += 運(yùn)算符重載函數(shù)的返回值為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; /如果是正數(shù),必須輸出正號else if(Image0) coutImagei; /如果是負(fù)數(shù),自動(dòng)輸出負(fù)號coutendl;void main( )Complex c1(2, 3), c2(4, -2);coutc1=;c1.Show( );coutc2=;c2.Show( );c1+=c2; / Acoutc1+=c2;c1=;c1.Show( );運(yùn)行結(jié)果如下:c1=2+3i

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

5、題了。返回ppt講稿 例13.8 對于字符串類,重載 = 運(yùn)算符,返回對象自身的引用。就本例而言,可以不定義拷貝構(gòu)造函數(shù),程序能正確運(yùn)行。#include #include class Stringchar *Strp; public:String(char *s=0) / 構(gòu)造函數(shù)if(s)Strp=new charstrlen(s)+1;strcpy(Strp, s);else Strp=0; String & operator = (String & s) / A賦值運(yùn)算符重載函數(shù)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( ) / 析構(gòu)函數(shù)if(Strp) delete Strp;void Show( )coutStrpendl; ;void main( )String str1(Sample String1); String str2; str2=str1; / 對象賦值運(yùn)算str2.Show( ); / 輸出 Sample String1但是若將程序中A行的函數(shù)頭部改為:String operator = (String & s) ,即返回本類對象,因類中有動(dòng)態(tài)申請的字符串空間,則必須

7、增加定義拷貝構(gòu)造函數(shù)。因?yàn)橐褂梅祷氐膶ο笕コ跏蓟瘍?nèi)存臨時(shí)對象,所以應(yīng)在類體中增加定義拷貝構(gòu)造函數(shù)如下:String(String & s) / 拷貝構(gòu)造函數(shù)if(s.Strp)Strp=new charstrlen(s.Strp)+1;strcpy(Strp, s.Strp);else Strp=0;返回ppt講稿 例13.11 成員函數(shù)和類型轉(zhuǎn)換函數(shù)的比較#include class RMB /定義一個(gè)“人民幣”類 int Yuan, Jiao, Fen; /元、角、分 public:RMB(int y=0, int j=0, int f=0) Yuan=y; Jiao=j; Fen=f;

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

9、 = r.operator int( );r3=r.GetFen( ); /C 調(diào)用成員函數(shù)得到分值 coutr1=r1endl;coutr2=r2endl;coutr3=r3endl;double d;d = r; /D 處理成 d = r.operator double( ); coutd=dendl;程序的運(yùn)行結(jié)果如下:r1=2386r2=2386r3=2386d=23.86返回ppt講稿 假定一種貨幣只有元和角兩種單位,一元等于10角,+和 - 表示加減1角。定義一個(gè)類Money 用于實(shí)現(xiàn)+和 - 運(yùn)算。例13.12 實(shí)現(xiàn)+和-的前置和后置運(yù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+( ); /用成員函數(shù)實(shí)現(xiàn)前置+Money operator+(int); /用成員函數(shù)實(shí)現(xiàn)后置+friend Money operator-(Money &m); /用友元函數(shù)實(shí)現(xiàn)前置- -friend Money operator-(Money &m, int); /用友元函數(shù)實(shí)現(xiàn)后置- -void

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

12、rator-(m)m.Jiao=10;m.Jiao-;return m;Money operator-(Money &m, int) /用友元函數(shù)實(shí)現(xiàn)后置-Money temp=m; /保存對象原始的值if(m.Jiao=0)調(diào)用方式: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.S

13、how(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,并測試重載的運(yùn)算符以及成員函數(shù) /Li1318.h#include #include class Stringprotected:int Length; /字符串的長度 char *Strp; /指向字符串的指針 publi

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

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

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

17、數(shù) 需要?jiǎng)h除原空間嗎?與賦值運(yùn)算符對照?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 / 判斷字符c是否在字符串中, 如果在,返回其第一次出現(xiàn)時(shí)的地址, 否則返回空指針NULLchar *p=Strp;while(*p) if(*p+ = c) return -p;return NULL;int String:IsSubStr(const char *s) const /判

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

19、lse Strp = NULL;return *this;String operator+(const String &s1, const String &s2 ) /拼接兩個(gè)字符串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;

20、char *p1=s1.Strp, *p2;int i=0, len=strlen(s2);if(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 / 實(shí)現(xiàn)小于比較,成員函數(shù)實(shí)現(xiàn) return(strcmp(Strp, s.Strp)

21、 ( const String &s) const / 實(shí)現(xiàn)大于比較,成員函數(shù)實(shí)現(xiàn) return(strcmp(Strp, s.Strp)0 ); int String:operator = ( const String &s) const / 實(shí)現(xiàn)恒等于比較,成員函數(shù)實(shí)現(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) /測試類的成員函數(shù)IsIn( ) /s.IsIn(c)返回的是c第一

22、次在s中出現(xiàn)的地址cout Yes:s.IsIn(c)endl; else cout No:n;void Test_IsSubStr(char *sp, String &s)coutsp in ; s.Show( );if(s.IsSubStr(sp) /測試類的成員函數(shù)IsSubStr( )cout Yes:n;else cout No:n;void main(void)String s1(C+程序設(shè)計(jì) ), s2, s3(學(xué)生學(xué)習(xí) );String s, s5;s1.Show( ); s2=s1; / 測試賦值運(yùn)算符“=”s2.Show( );s=s3+s2; / 測試字符串拼接運(yùn)算符“+

23、”以及賦值運(yùn)算符“=”s.Show( ); s5=s-s1; /測試字符串相減運(yùn)算符“”s5.Show( );String s6=C+ programming! ;Test_IsIn(g, s6); /判斷一個(gè)字符是否在“串”中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

24、=s3 OK!n;const char *sp1, *sp3;sp1=s1; sp3=s3; /調(diào)用A行的類型轉(zhuǎn)換函數(shù),由String類轉(zhuǎn)換成const char *類coutsp3sp1endl;couts3.GetString( )endl;程序的運(yùn)行結(jié)果如下:C+程序設(shè)計(jì)C+程序設(shè)計(jì)學(xué)生學(xué)習(xí) C+程序設(shè)計(jì)學(xué)生學(xué)習(xí)g in C+ programming!Yes:gramming!k in C+ programming!No:prog in C+ programming!Yes:red in C+ programming!No:s1=C+程序設(shè)計(jì)s3=學(xué)生學(xué)習(xí)s1s3 OK!學(xué)生學(xué)習(xí) C+程

25、序設(shè)計(jì)學(xué)生學(xué)習(xí)返回ppt講稿 例13.23 成員函數(shù)調(diào)用虛函數(shù) #include class Apublic:virtual void fun1( ) / D 虛函數(shù) cout A:fun1 t; fun2( );void fun2( ) cout A:fun2 t; fun3( ); / E virtual void fun3( ) / F 虛函數(shù) 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(

26、 ) cout B:fun4 n; ;void main( ) A a;a.fun1( );B b;b.fun1( ); / G程序的運(yùn)行結(jié)果是:A:fun1 A:fun2 A:fun3 A:fun4A:fun1 A:fun2 B:fun3 B:fun4返回ppt講稿 BaseAB例13.26 虛析構(gòu)函數(shù)#include #include class basechar *baseptr;public:base( ) baseptr=new char100;strcpy(baseptr, In class base);fc( ); virtual void fc( ) / D coutbasep

27、trendl; virtual base( ) / E delete baseptr;coutDelete baseptrendl; ;class A : public basechar *Aptr;public:A( ) Aptr=new char100;strcpy(Aptr, In class 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,

28、 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 程序的輸出結(jié)果是:In class baseIn class baseIn class BIn class baseIn class baseDelete BptrDelete AptrDelete baseptrDelete BptrDelete AptrDelete baseptr返回ppt講稿

29、例13.27 定義抽象類,派生出若干類,在派生類中實(shí)現(xiàn)純虛函數(shù) #include class Shape public:virtual double Area( )=0; /抽象的求面積的純虛函數(shù)virtual void Draw( )=0; /抽象的繪制圖形的純虛函數(shù);class Point:public Shapeprotected:double x, y; /點(diǎn)的坐標(biāo)值public:Point(double a=0, double b=0) x=a; y=b; double Area( ) /基類純虛函數(shù)Area( )的具體實(shí)現(xiàn) return 0.0; void Draw( ) /基類純虛函數(shù)Draw( )的具體實(shí)現(xiàn) cout Draw Point!n; ;class Rectangle:public Pointprotected:double x1, y1;/長方形右下角點(diǎn)的坐標(biāo)值,基類中x,

溫馨提示

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

評論

0/150

提交評論