計(jì)算機(jī)相關(guān)2013晶實(shí)例_第1頁(yè)
計(jì)算機(jī)相關(guān)2013晶實(shí)例_第2頁(yè)
計(jì)算機(jī)相關(guān)2013晶實(shí)例_第3頁(yè)
計(jì)算機(jī)相關(guān)2013晶實(shí)例_第4頁(yè)
計(jì)算機(jī)相關(guān)2013晶實(shí)例_第5頁(yè)
已閱讀5頁(yè),還剩47頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、C+C+實(shí)例實(shí)例王雪晶例1:?jiǎn)卧~和詞典類n建立一個(gè)由單詞及其含義組成的類Definition(每個(gè)單詞最多有10條含義),并使用該類的對(duì)象實(shí)現(xiàn)單詞及其含義的輸入輸出。n建立一個(gè)詞典,詞典由一個(gè)一個(gè)單詞組成,故Dictionary類中包含了Definition類的對(duì)象數(shù)組(可存放100個(gè)單詞)Definitionword: char *meaningsMAXMEANS: char*nMeanings: intDefinition()Definition(char * word, char * def)operator=(Definition& def): Definition&

2、PutWord(char *s): void GetWord(): char * AddMeaning(char *s): void GetMeaning(int level): char * GetnMeans(): intprint(): voidDefinition()單詞類class Definitionpublic:Definition() Definition(char * word, char * def)Definition & operator=(Definition& def)void PutWord(char *s)char * GetWord() ret

3、urn word;void AddMeaning(char *s)char * GetMeaning(int level) return nMeanings;int GetnMeans()void print()Definition()private:char * wordchar * meaningsMAXMEANSint nMeaningsDefiniton:Definition(char * w, char* def)int n=strlen(w)+1;word = new charn;strcpy(word,w);nMeanings = 0;for(; defnMeanings&

4、;nMeanings!=MAXMEANS; nMeanings+)meaningnMeanings = new charstrlen(defnMeanings)+1;strcpy(meaningsnMeanings, defnMeannings);Definiton& Definition:operator = ( Definition& def)int n=strlen(def.word)+1;word = new charn;strcpy(word,def.word);nMeanings = def.nMeanings;for(int i=0; inMeanings; i+

5、)meaningi = new charstrlen(def.meaningsi)+1;strcpy(meaningsi, def.meanningsi); Definition: Definition()delete word;for(int i=0; inMeanings; i+) delete meaningsi;void Definition: PutWord(char *s)int n=strlen(s)+1;word = new charn;nMeanings = 0;void Definition: AddMeaning(char *s)if ( nMeaningsMAXMEAN

6、S)meaningi = new charstrlen(s)+1;strcpy(meaningsnMeanings+, s);char * Definiton:GetMeaning(int level)if (0= level & level nMeanings)return meaningslevel;elsereturn NULL;void Definition: print()cout endlword“:n”;for (int i=0; inMeanings; i+)cout “ ”meaningsiendl;DictionarynWord: long maxWords: lo

7、ngword: Definition* Dictionary (int n=100)FindWord (char * s): longAddWord (Definition& word): voidGetDef (char* word, char* def): intPrint (char *): voidDictionary()詞典類Dictionary:Dictionary (int n)nWords = 0;maxWords = n;words = new Definitionn;Dictionary:Dictionary ()deletewords;long Dictionar

8、y:FindWord (char *s)for(int i=0; inWords; i+)if(strcmp(wordsi.Getword(), s)=0)return i;return -1;void Dictionary:AddWord (Definition& word)if (nWordsmaxWords)wordsnWords=word;+nWords;int Dictionary:GetDef (char *word, char* def)int nM=0;int wordIdx = FindWord(word);if (wordIdx!= -1) nM=wordsword

9、Idx.GetnMeans();for(int i=0; inM; i+) char * pMeaning = wordswordIdx.GetMeaning(i); int length = strlen(pMeaning); delete defi; defi = new charlength+1; strcpy(defi, pMeaning);void Dictionary:print (char * w)int wordIdx = FindWord(w);if (wordIdx!= -1)wordswordIdx.print();elsecout“couldnt find”wendl;

10、void main()Dictionary d(5); char * word1=“class”;char * indef1=“班級(jí)班級(jí)”,“類類”,“階級(jí)階級(jí)”,“課課”,“等等級(jí)級(jí)”,0;char * word2=“object”;char * indef2=“目的目的”,“對(duì)象對(duì)象”,“物體物體”,“賓語(yǔ)賓語(yǔ)”,“客觀客觀”,0;char * word3=“snow”;char * indef3=“雪雪”,“雪花效應(yīng)雪花效應(yīng)”,0;Definition def1(word1, indef1);Definition def2(word2, indef2);Definition def3(w

11、ord3, indef3);d.AddWord(def1);d.AddWord(def2);d.AddWord(def3);d.Print(word1);d.Print(word2);d.Print(word3);例2:鏈表n建立鏈表項(xiàng)Itemn一個(gè)數(shù)據(jù)項(xiàng)datan一個(gè)指針*next例2:鏈表n有關(guān)鏈表操作的類Listn顯示輸出一個(gè)已生成的鏈表;n對(duì)一個(gè)空表插入鏈表項(xiàng),插入項(xiàng)放在表頭;n對(duì)一個(gè)空表追加鏈表向,追加項(xiàng)放在表尾;n兩個(gè)鏈表相連接;n將一個(gè)鏈表的各鏈表項(xiàng)逆向輸出;n求得一個(gè)鏈表的數(shù)據(jù)項(xiàng)數(shù)。#include using namespace std;class List;class It

12、empublic:friend class List;private:Item(int d=0) data=d; next=0;Item * next;int data;class Listpublic:List() list=0;List(int d) list=new Item(d);int print();int insert(int d=0);int append(int d=0);void cat(List & il);void reverse();int length();private:Item * end();Item * list;int List:print()if

13、 (list = 0) cout“emptyn”;return 0;cout“(”;int cnt=0;Item *pt=list;while(pt)if(+cnt%40 = 1 & cnt!=1)coutendl;coutdatanext;coutnext=list;list = pt;return d;int List:append(int d)Item *pt = new Item(d);if(list = 0)list = pt;else(end()-next=pt;return d;Item * List:end( )Item *prv, *pt;for( prv=pt=li

14、st; pt; prv=pt, pt=pt-next);return prv;void List:cat( List& il)Item *pt=il.list;while( pt)append(pt-data);pt=pt-next;void List:reverse()Item *pt,*prv,*tmp;prv=0;pt=list;list=end();while(pt!=list)tmp=pt-next;pt-next=prv;prv=pt;pt=tmp;list-next=prv;int List:length()int cnt=0;Item *pt=list;for( ; p

15、t; pt=pt-next,cnt+);return cnt;void main()List list1;list1.print();for(int i=10;i18; i+)list1.insert(i);cout“l(fā)ist1: ”;list1.print();List list2;for(i=15;i20;i+)list2.append(i);cout“l(fā)ist2: ”;list2.print();cout“l(fā)ist1 length: ”list1.length()endl;list2.cat(list1);cout“l(fā)ist2: ”;list2.print();list2.reverse

16、();cout“l(fā)ist2: ”;list2.print();cout“l(fā)ist2 length: ”list2.length()endl;例3:虛函數(shù)例子n計(jì)算不同幾何形狀的面積和體積n類層次結(jié)構(gòu)如下:containercubespherecylinder#includeclass containerprotected:double radius;public:container(double radius)container:radius=radius;virtual double surface_area()=0;virtual double volume()=0;class cube:

17、public containerpublic:cube(double radius):container(radius);double surface_area()return radius*radius*6;double volume()return radius*radius*radius;class sphere:public containerpublic:sphere(double radius):container(radius);double surface_area()return 4*3.1416*radius*radius;double volume()return 3.1

18、416*radius*radius*radius*4/3;class cylinder:public containerdouble height;public:cylinder(double radius,double height):container(radius)cylinder:height=height;double surface_area()return 2*3.1416*radius*(height+radius);double volume()return 3.1416*radius*radius*height;void main()container *p;cube ob

19、j1(10);sphere obj2(6);cylinder obj3(4,5);p=&obj1;cout輸出結(jié)果:輸出結(jié)果:endl;cout正方體表面積:正方體表面積:surface_area()endl;cout正方體體積:正方體體積:volume()endl;p=&obj2;cout球體表面積:球體表面積:surface_area()endl;cout球體體積:球體體積:volume()endl;p=&obj1;p=&obj3;cout圓柱體表面積:圓柱體表面積:surface_area()endl;cout圓柱體體積:圓柱體體積:volume()pro

20、mpt(); / alert user/ .int i = pb-get_value();if (pb-was_changed() / new value; do something else / old value was fine/ . Ival_box類的使用void some_fct()Ival_box* p1 = new Ival_slider(0,5); interact(p1);Ival_box* p2 = new Ival_dial(1,12);interact(p2);一個(gè)傳統(tǒng)的層次結(jié)構(gòu)問(wèn)題:從哪獲取圖形要素?大部分圖形用戶界面系統(tǒng)都提供了一個(gè)類,其中定義了屏幕實(shí)體的基本性質(zhì)

21、。圖示:BBWindowIval_boxIval_sliderIval_dialPopup_ival_sliderFlashing_ival_slider一個(gè)傳統(tǒng)的層次結(jié)構(gòu)存在問(wèn)題:1. 版本問(wèn)題,不同用戶界面系統(tǒng)的Ival_box共存。2. 每個(gè)派生類都共享著在Ival_box里聲明的基本數(shù)據(jù)。3. 類BBwindow的修改將迫使Ival_box用戶重新編譯。抽象類重新設(shè)計(jì)類層次結(jié)構(gòu):1. 圖形用戶界面系統(tǒng)是一個(gè)實(shí)現(xiàn)細(xì)節(jié),對(duì)于用戶應(yīng)是隱蔽的。2. Ival_box類中不包含數(shù)據(jù)。3. 圖形用戶界面系統(tǒng)修改后,不應(yīng)該重新編譯那些使用Ival_box的代碼。4. 針對(duì)不同圖形用戶界面系統(tǒng)的Iva

22、l_box在程序中共存。抽象類class Ival_box public:virtual int get_value() = 0;virtual void set_value(int i) = 0;virtual void reset_value(int i) = 0;virtual void prompt() = 0;virtual bool was_changed() const = 0;virtual Ival_box() /虛析構(gòu)函數(shù); 把Ival_box定義為抽象界面抽象類class Ival_slider : public Ival_box, protected BBwindow

23、public:Ival_slider(int,int);Ival_slider();int get_value();void set_value(int i);/ .protected:/ functions overriding BBwindow virtual functions/ e.g. BBwindow:draw(), BBwindow:mouse1hit()private:/ data needed for slider; 抽象類void f(Ival_box* p)/ .delete p; 保證正確的清理方式是在基類里定義一個(gè)虛析構(gòu)函數(shù)Ival_box: Ival_box,并在派

24、生類適當(dāng)?shù)馗采w它。許多類都需要在它的對(duì)象消失之前做些清理工作。抽象類BBWindowIval_boxIval_sliderIval_dialPopup_sliderFlashing_sliderBBWindow圖示:Ival_box的層次結(jié)構(gòu)如下:其它實(shí)現(xiàn)方式class Ival_box /* . */ ;/ commonclass Ival_slider : public Ival_box, protected BBwindow /* . */ ; class Ival_slider : public Ival_box, protected CWwindow /* . */ ; 沒(méi)有解決版本

25、問(wèn)題:而且針對(duì)不同圖形用戶界面系統(tǒng)的Ival_slider的共存問(wèn)題。其它實(shí)現(xiàn)方式BBwindowIval_boxBB_ival_sliderCWwindowCW_ival_slider圖示:方案:采用不同的名字,定義幾個(gè)Ival_sliderclass Ival_box /* . */ ;/ commonclass BB_ival_slider : public Ival_box, protected BBwindow /* . */ ; class CW_ival_slider : public Ival_box, protected CWwindow /* . */ ;其它實(shí)現(xiàn)方式BBw

26、indowIval_sliderBB_ival_sliderCWwindowCW_ival_sliderIval_box進(jìn)一步將面向應(yīng)用的Ival_box類與實(shí)現(xiàn)細(xì)節(jié)分開(kāi),從Ival_box派生出一個(gè)抽象的Ival_slider類。其它實(shí)現(xiàn)方式BBsliderIval_sliderBB_ival_sliderCWsliderCW_ival_sliderIval_boxBBwindowCWwindow通常還可以利用實(shí)現(xiàn)層次方面的一些更特殊的類。其它實(shí)現(xiàn)方式完整的層次結(jié)構(gòu)是將原來(lái)面向應(yīng)用的概念層次結(jié)構(gòu)當(dāng)作界面而組成的。Ival_boxIval_sliderIval_dialPopup_ival_s

27、liderFlashing _ival_slider其它實(shí)現(xiàn)方式針對(duì)各種圖形用戶界面系統(tǒng)的實(shí)現(xiàn)。class BB_ival_slider : public Ival_slider, protected BBslider /* . */ ;class BB_flashing_ival_slider : public Flashing_ival_slider,protected BBwindow_with_bells_and_whistles /* . */ ;class BB_popup_ival_slider : public Popup_ival_slider, protected BBsl

28、ider /* . */ ;class CW_ ival_slider : public Ival_slider, protected CWslider /* . */ 其它實(shí)現(xiàn)方式BBsliderIval_sliderBBipopIval_dialipopupiflashCWipopCWslIval_boxBBsliderBBisliderBBiflCWiflCWslBBb&wCWisliderCWsl完整的層次結(jié)構(gòu)圖示:對(duì)象創(chuàng)建的局部化應(yīng)用中,可以利用Ival_box、Ival_slider等界面寫(xiě)出。對(duì)象的創(chuàng)建必須通過(guò)特定于實(shí)現(xiàn)的名字。如:創(chuàng)建CW_ival_dial和BB_flashing

溫馨提示

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

評(píng)論

0/150

提交評(píng)論