數(shù)據(jù)的共享和保護(hù)以及多態(tài)性課案_第1頁(yè)
數(shù)據(jù)的共享和保護(hù)以及多態(tài)性課案_第2頁(yè)
數(shù)據(jù)的共享和保護(hù)以及多態(tài)性課案_第3頁(yè)
數(shù)據(jù)的共享和保護(hù)以及多態(tài)性課案_第4頁(yè)
數(shù)據(jù)的共享和保護(hù)以及多態(tài)性課案_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)四 數(shù)據(jù)的共享和保護(hù)以及多態(tài)性實(shí)驗(yàn)?zāi)康? 學(xué)習(xí)數(shù)據(jù)的共享和保護(hù)。2 學(xué)習(xí)使用虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)多態(tài)性。實(shí)驗(yàn)內(nèi)容1 聲明一個(gè) Dog 類,自行分析設(shè)計(jì)出其可以擁有的靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù), 設(shè) 計(jì)完整程序并測(cè)試這個(gè)類, 請(qǐng)分析寫出靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的功能和用 法。2. 聲明類X、Y、Z。實(shí)現(xiàn):Y的成員函數(shù)可以訪問 X的私有數(shù)據(jù)成員,Z的成員函 數(shù)可以訪問 X 的數(shù)據(jù)成員。用多文件結(jié)構(gòu)實(shí)現(xiàn)以上功能。 (各類中的數(shù)據(jù)成員和函 數(shù)成員 請(qǐng)自行思考設(shè)計(jì) )3. 聲明一個(gè) Vehicle (車)基類,有 Run、Stop等成員函數(shù),由此派生出bicycle (自 行車)類和 motorcar

2、(汽車)類,從 bicycle類和 motorcar類派生出 motorcycle (摩 托車)類,它們都有 Run、Stop等成員函數(shù)。實(shí)現(xiàn)并測(cè)試這些類,注意虛基類和虛 函數(shù)的使用。4. 對(duì)people類重載"=”運(yùn)算符和"=”運(yùn)算符,"=”運(yùn)算符判斷兩個(gè)people類對(duì)象的id屬性的大小;"=”運(yùn)算符實(shí)現(xiàn) people類對(duì)象的賦值操作。附:people類的屬性:number (編號(hào))、sex (性別)、birthday (出生日期)、id (身份證號(hào))等 等。其中“出生日期”聲明為一個(gè)“日期”類內(nèi)嵌子對(duì)象。三、實(shí)驗(yàn)注意事項(xiàng)程序中,需要編寫一個(gè)可執(zhí)行函

3、數(shù)與 main 主調(diào)函數(shù),自主設(shè)計(jì)輸入、輸出值,使得結(jié) 果可以由控制臺(tái)顯示輸出。四、實(shí)驗(yàn)程序代碼程序 1#include <iostream>#include <string>using namespace std;class Dogpublic:Dog() age = 1; sex = "male" weight = 1.0;Dog(int _age, string _sex, double _weight):age(_age), sex(_sex), weight(_weight) Dog()void setDog(int _age, strin

4、g _sex, double _weight)age = _age;sex = _sex;weight = _weight;void showDog()cout<<"age = "<<age<<", sex = "<<sex.c_str()<<", weight = "<<weight<<endl;private:int age; string sex;double weight;int main()Dog d1, d2(10, "femal

5、e", 20.0);d1.showDog();d2.showDog();d1.setDog(8, "female", 50);d1.showDog();cin.get();return 0;程序 2#include<iostream>using namespace std;class X;class Z;class Ypublic:void g(X* pa);class Xint i;public:X() i=0; void disp() cout <<"i=" <<i <<endl; frien

6、d void Y:g(X* pa);friend class Z;friend void h(X* pa);class Zpublic:void f(X* pa) pa->i += 5; ;void Y:g(X* pa) pa->i+; void h(X* pa) pa->i += 8; int main()X x;Y y;Z z;y.g(&x);x.disp();z.f(&x);x.disp();h(&x); x.disp();return 0;程序 3#include<iostream>using namespace std;class

7、 Vehiclepublic:virtual void Run()cout<<"V ehicle:Run calledn"virtual void Stop()cout<<"Vehicle:Stop calledn"class motorcar:public Vehiclepublic:void Run()cout<<"motorcar:Run calledn"virtual void Stop()cout<<"motorcar:Stop calledn" ;clas

8、s bicycle :public Vehiclepublic:virtual void Run()cout<<"bicycle:Run calledn"void Stop()cout<<"bicycle:Stop calledn"class motorcycle:public bicycle,public motorcarpublic:void Run()cout<<"motorcycle:Run calledn"void Stop()cout<<"mototrcycle:S

9、top calledn"int main()Vehicle ve; bicycle bi; motorcar mo; motorcycle mocy; Vehicle *pclass=&ve; pclass->Run(); pclass->Stop(); pclass=&bi; pclass->Run(); pclass->Stop(); pclass=&mo; pclass->Run(); pclass->Stop();程序 4 #include<iostream> #include<string>

10、 using namespace std; class date /日期類private:int year,month,day;public:void setdate(int y=0,int m=0,int d=0);/帶默認(rèn)形參值的成員函數(shù)void showdate();void date:setdate(int y,int m,int d)/內(nèi)聯(lián)成員函數(shù)cin>>y>>m>>d;year=y;month=m;day=d;void date:showdate() cout<<year<<"-"<<mo

11、nth<<"-"<<day<<endl;class people private:int num;char sex;date birth;/"日期類內(nèi)嵌子對(duì)象string id;public:people()/構(gòu)造函數(shù)people(int n,char s,date b,string i) num=n;sex=s;birth=b;id=i;people operator =(const people &p);/ 符號(hào)重載函數(shù)實(shí)現(xiàn)people operator =( const people &p);people(

12、people &p); / 拷貝構(gòu)造函數(shù)void setpeople(); / 屬性值錄入void showpeople(); / 屬性值顯示;people:people(people &p) / 定義拷貝構(gòu)造函數(shù)num=p.num;sex=p.sex;birth=p.birth;id=p.id;void people:setpeople()cout<<" 錄入人員信息 "<<endl;cout<<"請(qǐng)輸入編號(hào):";cin>>num;cout<<" 請(qǐng)輸入性別 (m/f

13、): "cin>>sex;cout<<"請(qǐng)輸入出生日期:";birth.setdate();cout<<" 請(qǐng)輸入身份證號(hào) : ";cin>>id;void people:showpeople() / 對(duì)象函數(shù)實(shí)現(xiàn)cout<<""<<num<<" 號(hào)人員信息 "<<endl;cout<<" 人員編號(hào) : "<<num<<endl;cout<<&

14、quot; 人員性別 : "<<sex<<endl;cout<<" 出生日期 : ";birth.showdate();cout<<" 身份證號(hào) : "<<id<<endl;people people:operator =(const people &p)if(p.id=id) cout<<"id 相等! "<<endl;else cout<<"id 不相等! "<<endl;

15、return *this;people people:operator =(const people &p) id=p.id;num=p.num;sex=p.sex;birth=p.birth; return *this;int main()people p1,p2;p1.setpeople();p1.showpeople();p2.setpeople();p2.showpeople(); cout<<""<<endl; cout<<"p1=p2:"<<endl;p1=p2; cout<<

16、;""<<endl; cout<<"p1=p2:"<<endl;p1=p2;p1.showpeople();p2.showpeople();return 0;五、實(shí)驗(yàn)運(yùn)行結(jié)果程序1' CiU s er$fa nyjDo cu mentsC - F reeT e mage = 1, sex = male.,訓(xùn)eight = 1age - 10, sex - femalej weight - 20ae = 8, sex = female, weight = 50請(qǐng)按任意鍵繼續(xù) 程序2 "C:U5ersfanyjDocumentsC- Frei = li = 14請(qǐng)按任意鍵繼續(xù).程序3" C:U ersf a nyjDo cu m enehic1e:Run called Vehicle:Stop called >icycle:Run called )icycle;:Stop called notorcar:Run called notorcar:Stop called

溫馨提示

  • 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)論