




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實驗四 數(shù)據(jù)的共享和保護以及多態(tài)性實驗?zāi)康? 學習數(shù)據(jù)的共享和保護。2 學習使用虛函數(shù)實現(xiàn)動態(tài)多態(tài)性。實驗內(nèi)容1 聲明一個 Dog 類,自行分析設(shè)計出其可以擁有的靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù), 設(shè) 計完整程序并測試這個類, 請分析寫出靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的功能和用 法。2. 聲明類X、Y、Z。實現(xiàn):Y的成員函數(shù)可以訪問 X的私有數(shù)據(jù)成員,Z的成員函 數(shù)可以訪問 X 的數(shù)據(jù)成員。用多文件結(jié)構(gòu)實現(xiàn)以上功能。 (各類中的數(shù)據(jù)成員和函 數(shù)成員 請自行思考設(shè)計 )3. 聲明一個 Vehicle (車)基類,有 Run、Stop等成員函數(shù),由此派生出bicycle (自 行車)類和 motorcar
2、(汽車)類,從 bicycle類和 motorcar類派生出 motorcycle (摩 托車)類,它們都有 Run、Stop等成員函數(shù)。實現(xiàn)并測試這些類,注意虛基類和虛 函數(shù)的使用。4. 對people類重載"=”運算符和"=”運算符,"=”運算符判斷兩個people類對象的id屬性的大?。?quot;=”運算符實現(xiàn) people類對象的賦值操作。附:people類的屬性:number (編號)、sex (性別)、birthday (出生日期)、id (身份證號)等 等。其中“出生日期”聲明為一個“日期”類內(nèi)嵌子對象。三、實驗注意事項程序中,需要編寫一個可執(zhí)行函
3、數(shù)與 main 主調(diào)函數(shù),自主設(shè)計輸入、輸出值,使得結(jié) 果可以由控制臺顯示輸出。四、實驗程序代碼程序 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);/帶默認形參值的成員函數(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)嵌子對象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);/ 符號重載函數(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<<"請輸入編號:";cin>>num;cout<<" 請輸入性別 (m/f
13、): "cin>>sex;cout<<"請輸入出生日期:";birth.setdate();cout<<" 請輸入身份證號 : ";cin>>id;void people:showpeople() / 對象函數(shù)實現(xiàn)cout<<""<<num<<" 號人員信息 "<<endl;cout<<" 人員編號 : "<<num<<endl;cout<<&
14、quot; 人員性別 : "<<sex<<endl;cout<<" 出生日期 : ";birth.showdate();cout<<" 身份證號 : "<<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;五、實驗運行結(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請按任意鍵繼續(xù) 程序2 "C:U5ersfanyjDocumentsC- Frei = li = 14請按任意鍵繼續(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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025餐飲服務(wù)員勞動合同書模板
- 海南物業(yè)服務(wù)合同范本
- 2025年個體工商戶轉(zhuǎn)手合同
- 2025租房合同協(xié)議樣本
- 琴行簽勞務(wù)合同范本
- 2025年合同續(xù)簽申請表
- 2025臨時租約房屋租賃合同范本
- 采購效率培訓(xùn)課件
- 中藥貨架轉(zhuǎn)讓合同范本
- 部門職責與崗位說明書培訓(xùn)
- GB/T 7307-200155°非密封管螺紋
- 粵教版2022年小學六年級科學下冊期中測試試卷及答案2022-2023
- 北師大六年級下冊數(shù)學第三單元《圖形的運動》教學設(shè)計
- 帶狀皰疹及后遺神經(jīng)痛科普課件
- 肺動脈高壓的指南分類及精選課件
- 自考06216中外建筑史大綱知識點匯總
- C139營銷模型簡介(含案例)課件
- x-net運動總線手冊
- 橋梁加固改造工程施工質(zhì)量管理體系與措施
- 第二十六章慢性腎小球腎炎演示文稿
- 設(shè)施設(shè)備維修記錄表
評論
0/150
提交評論