面向?qū)ο笈cvisual c程序設(shè)計(jì)案例教程_第1頁(yè)
面向?qū)ο笈cvisual c程序設(shè)計(jì)案例教程_第2頁(yè)
面向?qū)ο笈cvisual c程序設(shè)計(jì)案例教程_第3頁(yè)
面向?qū)ο笈cvisual c程序設(shè)計(jì)案例教程_第4頁(yè)
面向?qū)ο笈cvisual c程序設(shè)計(jì)案例教程_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

C++中,本章主要介紹單一繼承中類的定義和使用方式以及類構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用順序,對(duì)多重繼承也將做簡(jiǎn)單介紹。在多態(tài)性方面主要介紹多態(tài)性的概念及其實(shí)現(xiàn)的關(guān)鍵技系統(tǒng)應(yīng)該可以運(yùn)行在市面上流行的任何一款數(shù)據(jù)庫(kù)管理系統(tǒng)上,如Access、MySQL、SQLServer等。所有的數(shù)據(jù)庫(kù)管理系統(tǒng)使用之前都要連接,使用完成后要斷開連接。不同 //操作Access數(shù)據(jù)庫(kù) //操作MySQL數(shù)據(jù)庫(kù) Account_DB_Access、Account_DB_MySQLAccount_DB_SQLServer都分關(guān)系,如圖4.1所示。過下面的代碼來Account_DB_Access的connect()函數(shù)。4.1 Account_DB*db= intint{Account_DB_MySQLdb_Mysql;}Account_DBconnect(),則會(huì)導(dǎo)致編譯connect()成員函數(shù)。在上例中,pdbAccount_DB_Access對(duì)象、Account_DB_MySQLAccount_DB_SQLServer對(duì)象。在執(zhí)行期間的每個(gè)調(diào)用點(diǎn)上,pdbconnect()實(shí)例,這稱為動(dòng)態(tài)綁定。public級(jí)別的劃分上對(duì)于實(shí)現(xiàn)細(xì)節(jié)的一般性,C++供了另一種級(jí)別:protected。一個(gè)類的d區(qū)域中的數(shù)據(jù)成員和成員函數(shù),對(duì)一般程序是不可的,而對(duì)于派生類是可的。為d是e該類的派生類直接其成員,則把它為private。如果希望一個(gè)成員為“后來的、要求這個(gè)成員為protected。class{virtualboolconnect()class{virtualboolconnect() }{returnclassclass{virtualboolconnect(0Account_DB最基本的兩個(gè)功能——連接和斷開連接開始,在后續(xù)章節(jié)中提供的操作,所以必須在Account_DB類定義中為虛擬的,每個(gè)派生類必須重寫這個(gè)函數(shù),以提供自己的連接數(shù)據(jù)庫(kù)方式。Account_DB基類提供公有接口供編程使用。Account_DB中定義為一個(gè)唯一的實(shí)例(非虛擬函數(shù)),讓每個(gè)子類繼承該操作。在這種設(shè)計(jì){ }類型。由此定義出Account_DB類的第一個(gè)版本。class{virtualboolconnect()=0; virtualbooldisconnect()=0; bool 基類Account_DB將函數(shù)connect()成純虛函數(shù),那是因?yàn)闆]有有意義的通用連接Account_DB中提供定義(Account_DB類的最后版本中該函數(shù)也是純虛"classAccount_DB_Access:public"classAccount_DB_Access:public{boolAccount_DB_Access還可以增加自己特有的處理函數(shù)和數(shù)據(jù)。connect()等成員函數(shù)的定義涉及到數(shù)據(jù)庫(kù)相關(guān)知識(shí),所以將在后續(xù)的相關(guān)章節(jié)中完成 classderived_class:access_level 其中:class為關(guān)鍵字,表示要新建一個(gè)類類型,derived_class是新建子類的名稱。冒號(hào)之后稱為派生表,base_class是父類或基類的名字。access_levelpublic、protected或private之一,表示子類到父類的派生方式,如上述定義中表明類Account_DB_Access從Account_DB類公有派生。基類可以只有一個(gè)(單一繼承),也可以有多個(gè)(多重繼承),有多數(shù)據(jù)成員構(gòu)成的。例如Account_DB_Access類對(duì)象由Account_DB子對(duì)象以及bool{cout<<"hostname:"<<_host<<endl;cout<<"dbname:"<<_db_name<<endl;cout<<"username:"<<_user<<endl;returntrue;}bool{cout<<"hostname:"<<_host<<endl;cout<<"dbname:"<<_db_name<<endl;cout<<"username:"<<_user<<endl;returntrue;}boolboolclassclass{ classShy:public{string_mumbleDiffident::_mumbleShy的作用域中,非限定修飾地使用_mumbleShystringvoidvoid{_mumbleexcuseme"}類的成員。例如,下面是turn_eyes_down()的正確實(shí)現(xiàn):voidvoid{_mumble="excuseme";//正確Diffident::_mumble1正確:限定修飾基類的實(shí)例}classclass{voidmumble(intclassShy:public{ShyShyclass{voidclass{voidclassShy:public{classclass{從Account_DB派生的類可以直接數(shù)據(jù)成員_host,而在基類和派生類的外部不可 另一個(gè)獨(dú)立的基類對(duì)象的protected成員,例如:voidvoid y(constAccount_DB{cout<<"hostname:"<<_host<<endl;//正確 自己的Account_DB子對(duì)象 }考慮下列初始化,用一個(gè)派生類Account_DB_Access對(duì)象的地址初始化一個(gè)Account_DB*Account_DB*pdb=newAccount_DB_Access Account_DB_Access的成員如果Account_DB和Account_DB_Access都 通過pdb調(diào)用的總是Account_DB的實(shí)例。則通過pdb總是Account_DB的實(shí)例。圖通過pdb調(diào)用它就會(huì)導(dǎo)致一個(gè)編譯時(shí)刻錯(cuò)誤。如果試圖通過pdbAccount_DB_Access的數(shù)據(jù)成員或非虛擬成員函數(shù),也會(huì)總之,在C++中,基類指針只能在該基類中被(或繼承)的數(shù)據(jù)成員和成員函pdb指向的實(shí)際類類型對(duì)于要調(diào)用的實(shí)例的解析過程”。Account_DBAccount_DB_Access會(huì)創(chuàng)建第2個(gè)靜態(tài)成員的實(shí)例嗎?答案是否定的,所有派生類對(duì)象都相同的、單一的、classAccount_DB_Access:public{//... classAccount_DB_Access:public{//... 象由一個(gè)Account_DB子對(duì)象和一個(gè)string成員類對(duì)象以及一個(gè)bool成員構(gòu)成。 Account_DB_Access 便初始化基類數(shù)據(jù)成員、string默認(rèn)構(gòu)造函數(shù)以便初始化數(shù)據(jù)成員_name,以及Account_DB_Access的默認(rèn)構(gòu)造函數(shù)以便初始化_present。Account_DB2位,但是它總是在“與_name相關(guān)class{的string構(gòu)造函數(shù)被自動(dòng)調(diào)用。下面是Account_DB類構(gòu)造函數(shù)的定義:Account_DB類來說意義不大,因?yàn)橐粋€(gè)沒有指定類中數(shù)據(jù)Account_DB::Account_DB(constAccount_DB::Account_DB(conststring&db_name,conststring&user,conststring&passwd,conststring&host,constint&port)Account_DB類的構(gòu)造函數(shù)分別把傳過來的參數(shù)通過初始化成員列表傳遞給現(xiàn)在有個(gè)問題:應(yīng)該把構(gòu)造函數(shù)成什么樣的級(jí)別?因?yàn)锳ccount_DB類對(duì)象publicAccessclass{passwd="",conststring&host="",constint&port=0);SLServerOracleclass{passwd="",conststring&host="",constint&port=0);classAccount_DB_Access:public{Account_DB_Access(constclassAccount_DB_Access:public{Account_DB_Access(conststring&db_name,conststring&user="",conststring&passwd="",conststring&host="",constint&port=0);user,conststring&passwd,conststring&host,constint&port)Account_DB_AccessAccount_DB_AccessAccount_DB的基Account_DB_AccessAccount_DB_Access資源,所以不需要提供顯式的析構(gòu)函數(shù)來釋放資源。對(duì)于基類Account_DB中的_passwd,_user和_db_namestring的析構(gòu)函數(shù)來清除它們所占用的內(nèi)void void cout<<pb->IsUserExit()Account_DB的非虛擬成員函數(shù)Account_DB_Accessdba("PTPS_DB","PTPS_Name","PTPS_Passwd");Account_DBdbdbaAccount_DB_Accessdba("PTPS_DB","PTPS_Name","PTPS_Passwd");Account_DBdbdbadba被"切割"Account_DB子對(duì)象boolboolg_connect(Account_DBobj,Account_DB*pobj,Account_DB&{robj.connect()connect()實(shí)例returntrue;}int{Account_DB_Accessdba;g_connect(dba,&dba,dba);}通過pobj和robj的調(diào)用被解析為它們的動(dòng)態(tài)類型,在這個(gè)例子中,它們都調(diào)用系統(tǒng)相關(guān)聯(lián)的類。因此現(xiàn)在定義了3個(gè)實(shí)體類類型:Account_DB_Access,{returntrue;}Account_DB{returntrue;}Account_DB類的公有接口的目的是,定義一組“足夠支持當(dāng)前和將來的所有數(shù) Account_DB*pdb=newAccount_DB*pdb2newAccount_DB錯(cuò)誤:new表達(dá)式分配Account_DBAccount_DB*pdb=newAccount_DB*pdb=newpdb->isA();//通過虛擬機(jī)制動(dòng)態(tài)調(diào)用Account_DB_Access::isA()實(shí)例class{virtualvoidprint()=Account_DB以及它的派生類中Account_DBclass{virtualvoidprint()=Account_DB中定義print()voidvoidclassAccount_DB_Access:public{classAccount_DB_Access:public{}void{cout<<"Account_DB_Access::print()"<<endl;}{(constchar*n,constint&{name=newchar[strlen(n)+1];} classstudent:{student(constchar*n,constchar*p,constint&{profession=newchar[strlen(p)+1];strcpy(profession,p);} void*pb){delete{virtual//{virtual//析構(gòu)函數(shù)。例如,如果pb指向一個(gè)student對(duì)象,則下面的語(yǔ)句: delete {virtualclassstudent{virtualclassstudent:{當(dāng)通過student對(duì)象調(diào)用student析構(gòu)函數(shù)時(shí),它的 級(jí)別是public,但是,當(dāng)通過intint{*pq=newstudent(" ","計(jì)算機(jī)科學(xué)技術(shù)系",25);deletepq;// :析構(gòu)函數(shù)是protected}所以作為一般規(guī)則,建議將類層次結(jié)構(gòu)的根基類的析構(gòu)函數(shù)為虛擬的,級(jí)別一般為public。在實(shí)際的C++應(yīng)用程序中,主要的繼承模型是“單個(gè)基類的公有繼承”模型。一般可它提供的模型會(huì)增加不必要的復(fù)雜性,而且不直觀。此時(shí),多繼承,或者它的特例——虛園動(dòng)物類(ZooAnimal)和瀕臨動(dòng)物類(Endangered)。類(Bear)ZooAnimal類派生而來,熊貓種類(Panda)Bear類和Endangered4.2 classBear:public 每個(gè)被列出的基類還必須指定其級(jí)別:public、protectedprivate之一。與單繼承一樣,只有當(dāng)一個(gè)類的

4.2多繼承PandaPanda ying_yang由一個(gè)Bear類子對(duì)象它又含有一個(gè)ZooAnimal基類子對(duì)象)、一個(gè)EndangeredPanda類中ying_yang來說,構(gòu)造函數(shù)被調(diào)用的順序是:Bear構(gòu)造函數(shù)(BearZooAnimalBear構(gòu)造函數(shù)執(zhí)行之前,ZooAnimal的構(gòu)造函數(shù)先被調(diào)用),EndangeredPandaBearEndangered的雙參數(shù)構(gòu)造函數(shù)之前被調(diào)用。如,如果BearEndangered中都定義了一個(gè)非私有成員函數(shù)print(),則如下語(yǔ)句:print()調(diào)用,或?qū)ο?。?duì)于多繼承,這也是正確的。例如,一個(gè)Panda指針、或?qū)ο罂梢员?Error:dis y(ying_yang Error:dis y(ying_yang)--ambiguous,oneofexternvoiddis y(constBear&);externvoid y(constPanda externexternvoid y(constexternvoidhighlight(constEndangered&);externvoidprint(constZooAnimal&);Pandaying_yang; print(ying_yang);//正確externvoidy(constexternvoidy(constPanda的每個(gè)classclass{virtualclassBear:public{virtualvirtualvoidprint()const;virtualboolisA()const;class{virtual~Endangered();virtualvoidprint()const;virtualvoidhighlight()classclassPanda:publicBear,public{virtualvirtualvoidprint()const;virtualvoidcuddle();print()isA()highlight()接口中“Panda特有的部分”以及“Endangered部分”就不能再被了。例如:Bear*Bear*pb=newPanda;deletepb;錯(cuò)誤:不是Bear接口的部分錯(cuò)誤:不是Bear接口中“Panda特有的部分”以及“Bear部分”就不能再被了。例如:Endangered*Endangered*pe=newpe->print();//pe->isA();//錯(cuò)誤:不是Endangered的接口部分pe->cuddle();//錯(cuò)誤:不是Endangered的接口部分pe->highlight()Endangered::highlight()deletepe;//正確:Panda::~Panda()ZooAnimal*ZooAnimal*pz=newPanda;deletepz;Bear*pb=newPanda;deletepb;Panda*pp=newPanda;deletepp;Endangered*pe=newdeletePanda析構(gòu)函數(shù)通過虛擬機(jī)制調(diào)用,再靜態(tài)調(diào)用Endangered、BearZooAnimal的析構(gòu)函classclassBear:publicBearZooAnimalclassclassPolarBear:public象的所有非靜態(tài)數(shù)據(jù)成員和ZooAnimal子對(duì)象的所有非靜態(tài)數(shù)據(jù)成員。明顯的就是標(biāo)準(zhǔn)C++的輸入/輸出流類庫(kù):iostream庫(kù),其層次結(jié)構(gòu)如圖4.3所示。4.3iostream任何未限定修飾地ios的成員都將導(dǎo)致編譯時(shí)刻錯(cuò)誤。被消除了。參看如圖4.4所示的虛擬繼承Panda層次結(jié)構(gòu)圖。4.4Panda和Raccoon沒有實(shí)現(xiàn)虛擬派生,則Panda類就不能在實(shí)際中應(yīng)用了。classBearpublicvirtualZooAnimal{...publicvirtualclassBearpublicvirtualZooAnimal{...publicvirtual的順序classRaccoon:virtualpublic盡管Panda被設(shè)計(jì)為虛擬繼承層次結(jié)構(gòu),下面的Panda基類轉(zhuǎn)換也可以正確執(zhí)行。externvoiddance(constBear*);externexternvoiddance(constBear*);externvoidrummage(constRaccoon*);externvoidprint(constZooAnimal&);voidmain(){Pandayin_yang;print(yin_yang);//正確}class{class{virtual~ZooAnimal();virtualvoidprint()const;stringname()const{returnstringfamily_name()const{return_fam_name;} 是Bear類的定義:classclassBear:publicvirtual{enumDanceType{two_left_feet,macarena,fandango,waltz};Bear(stringname,boolonExhibit=true):ZooAnimal(name,onExhibit,"Bear"),_dance(two_left_feet){}virtualvoidprint()const; classclassRaccoon:publicvirtual{Raccoon(stringname,bool:ZooAnimal(name,onExhibit,"Raccoon"),_pettable(false){}virtualvoidprint()const;boolpettable()const{returnvoidpettable(boolpetval){_pettable=petval;} classPanda:publicBear,publicRaccoon,classPanda:publicBear,publicRaccoon,public{Panda(stringname,boolonExhibit=true);virtualvoidprint()const;boolsleevoid()const{return_slee(boolnewval){_slee=;BearRaccoon的基類構(gòu)造函數(shù)都提供了一個(gè)帶有顯式實(shí)參集合的虛擬派生中,只有Panda可以直接調(diào)用其虛擬基類的構(gòu)造函數(shù)。Bear定義來決定的。例如,在BearBearRaccoonRaccoonPanda現(xiàn)在,在定義Panda函數(shù)的調(diào)用不再被執(zhí)行;②ZooAnimalPanda的初始化列表中被指定的。下面是Panda構(gòu)造函數(shù)的具體定義::ZooAnimal(name,onExhibit,"Panda"),Bear(name,onExhibit),Raccoon(name,onExhibit), PandaZooAnimal構(gòu)造函數(shù)指定實(shí)參,則會(huì)發(fā)生下面兩ZooAnimal的默認(rèn)構(gòu)造函數(shù),②如果沒有默認(rèn)構(gòu)造函數(shù)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論