




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1197: 繼承與派生1Description請(qǐng)以點(diǎn)類Point為基類派生出一個(gè)圓類Circle。圓類Circle的數(shù)據(jù)成員為r(私有屬性,存儲(chǔ)圓的半徑,圓心的點(diǎn)坐標(biāo)通過繼承點(diǎn)類Point加以實(shí)現(xiàn)),成員函數(shù)有構(gòu)造函數(shù)Circle、計(jì)算圓的面積函數(shù)Area、計(jì)算圓的周長(zhǎng)函數(shù)Perimeter和輸出函數(shù)Display,其中構(gòu)造函數(shù)實(shí)現(xiàn)基類和圓類的數(shù)據(jù)成員的初始化,Display函數(shù)實(shí)現(xiàn)圓心坐標(biāo)(利用基類Point的Display實(shí)現(xiàn))、圓的半徑、圓的面積(利用Area函數(shù)實(shí)現(xiàn))和圓的周長(zhǎng)(利用Perimeter函數(shù)實(shí)現(xiàn))的輸出。請(qǐng)編寫圓類的定義及成員函數(shù)實(shí)現(xiàn),并在主函數(shù)中定義圓類對(duì)象,驗(yàn)證各個(gè)函
2、數(shù)的正確性。說明:圓周率PI的取值為3.14已知Point類的定義及main代碼如下:(不允許改動(dòng))class Point public: Point(double xx,double yy); /constructor void Display(); /display point private: double x,y; /平面的點(diǎn)坐標(biāo)x,y; int main() double x,y,r; cin>>x>>y>>r; /圓心的點(diǎn)坐標(biāo)及圓的半徑 Circle C(x,y,r); C.Display(); /輸出圓心點(diǎn)坐標(biāo),圓的半徑,圓的面積,圓的周長(zhǎng) r
3、eturn 0;InputOutputSample Input1.5 2.6 1.8Sample OutputCenter:Point(1.5,2.6)Radius:1.8Area:10.1736Perimeter:11.304*#include<iostream>using namespace std;class Point public: Point(double xx,double yy) /constructor x=xx; y=yy; void Display()/display point cout<<"Center:Point("<
4、;<x<<","<<y<<")"<<endl; private: double x,y; /平面的點(diǎn)坐標(biāo)x,y;class Circle:public Pointprivate:double r;public:Circle(double xx,double yy,double rr):Point(xx,yy)r=rr;double Area()return 3.14*r*r;double Perimeter()return 2*3.14*r;void Display()Point:Display();
5、cout<<"Radius:"<<r<<endl;cout<<"Area:"<<Area()<<endl;cout<<"Perimeter:"<<Perimeter()<<endl;int main() double x,y,r; cin>>x>>y>>r; /圓心的點(diǎn)坐標(biāo)及圓的半徑 Circle C(x,y,r); C.Display(); /輸出圓心點(diǎn)坐標(biāo),圓的半徑,圓的面積,圓的周長(zhǎng) r
6、eturn 0;1217: 繼承與派生2DescriptionPerson類派生大學(xué)生CollegeStu類(1)。設(shè)計(jì)一個(gè)Person類,其屬性包括姓名name和身份證號(hào)id,其中name為指針類型,id為整型,編寫成員函數(shù):構(gòu)造函數(shù)Person、Display函數(shù)(顯示數(shù)據(jù)成員信息)和析構(gòu)函數(shù);由Person類派生出大學(xué)生類CollegeStu,其屬性有專業(yè)subject(指針類型),C+程序設(shè)計(jì)課程成績(jī)score(double型),編寫構(gòu)造函數(shù)(實(shí)現(xiàn)數(shù)據(jù)初始化)、輸出函數(shù)Display(包括name,id,subject,score)。main的代碼如下:(不允許改動(dòng))int main(
7、) char name81,subject81; int id; double score; cin>>name>>id>>subject>>score; CollegeStu cs(name,id,subject,score); cs.Display(); return 0;InputOutputSample InputZhangsan 2 Computer 89.5Sample OutputName:ZhangsanID:2Subject:ComputerC+ Score:89.5*#include<iostream>#inclu
8、de<cstring>using namespace std;class Personprivate:char * name;int id;public:Person()name=NULL;id=0;Person(char *name1,int id1)name=new charstrlen(name1)+1;strcpy(name,name1);id=id1;Person()delete name;void Display()cout<<"Name:"<<name<<endl; cout<<"ID:&q
9、uot;<<id<<endl;class Collegestu : public Personprivate:char * subject;double score;public:Collegestu()subject=NULL;score=0;Collegestu(char * name1,int id1,char * subject1,double score1):Person(name1,id1)subject=new char strlen(subject1)+1;strcpy(subject,subject1);score=score1;Collegestu(
10、)delete subject;void Display()Person:Display();cout<<"Subject:"<<subject<<endl; cout<<"C+ Score:"<<score<<endl;int main() char name81,subject81; int id; double score; cin>>name>>id>>subject>>score; Collegestu cs(name,id,
11、subject,score); cs.Display(); return 0;1218: 繼承與派生3DescriptionPerson類派生大學(xué)生CollegeStu類(2)。設(shè)計(jì)一個(gè)Person類,其屬性包括姓名name和身份證號(hào)id,其中name為指針類型,id為整型,編寫成員函數(shù):構(gòu)造函數(shù)Person、Display函數(shù)(顯示數(shù)據(jù)成員信息)和析構(gòu)函數(shù);由Person類派生出大學(xué)生類CollegeStu,其屬性有專業(yè)subject(指針類型),C+程序設(shè)計(jì)課程成績(jī)score(double型),編寫構(gòu)造函數(shù)(實(shí)現(xiàn)數(shù)據(jù)初始化)、輸出函數(shù)Display(只輸出subject,score)。ma
12、in的代碼如下:(不允許改動(dòng))int main() char name81,subject81; int id; double score; cin>>name>>id>>subject>>score; /輸入學(xué)生的姓名、id號(hào)、專業(yè)、成績(jī) CollegeStu cs(name,id,subject,score); cs.Person:Display(); /輸出姓名,id cs.Display(); /輸出專業(yè)、成績(jī) return 0;InputOutputSample InputLixu 5 Software 87.5Sample Outpu
13、tName:LixuID:5Subject:SoftwareC+ Score:87.5*#include<iostream>#include<cstring>using namespace std;class Personprivate:char * name;int id;public:Person()name=NULL;id=0;Person(char *name1,int id1)name=new charstrlen(name1)+1;strcpy(name,name1);id=id1;Person()delete name;void Display()cout
14、<<"Name:"<<name<<endl; cout<<"ID:"<<id<<endl;class CollegeStu : public Personprivate:char * subject;double score;public:CollegeStu()subject=NULL;score=0;CollegeStu(char * name1,int id1,char * subject1,double score1):Person(name1,id1)subject=new
15、 char strlen(subject1)+1;strcpy(subject,subject1);score=score1;CollegeStu()delete subject;void Display()cout<<"Subject:"<<subject<<endl; cout<<"C+ Score:"<<score<<endl;int main() char name81,subject81; int id; double score; cin>>name>&
16、gt;id>>subject>>score; /輸入學(xué)生的姓名、id號(hào)、專業(yè)、成績(jī) CollegeStu cs(name,id,subject,score); cs.Person:Display(); /輸出姓名,id cs.Display(); /輸出專業(yè)、成績(jī) return 0;1219: 繼承與派生4Description已知Base為基類,派生出Derived類,兩個(gè)類的定義及main的代碼如下(不允許改動(dòng)),請(qǐng)完成Base類和Derived類的構(gòu)造函數(shù)和析構(gòu)函數(shù),能夠根據(jù)輸入獲取相應(yīng)的輸出。class Baseprivate: int b;public: Ba
17、se(int); Base();class Derived:public Baseprivate: int d;public: Derived(int,int); Derived();int main() int a,b; cin>>a>>b; Derived dr(a,b); return 0;InputOutputSample Input1 3Sample OutputBase 1 says helloDerived 3 says hiDerived 3 says byeBase 1 says goodbye*#include<iostream>usin
18、g namespace std;class Baseprivate: int b;public: Base(int c) b=c; cout<<"Base "<<b<<" says hello"<<endl; Base() cout<<"Base "<<b<<" says goodbye"<<endl; ;class Derived:public Baseprivate: int d;public:Derived(int
19、 c,int b):Base(c)d=b;cout<<"Derived "<<d<<" says hi"<<endl; Derived() cout<<"Derived "<<d<<" says bye"<<endl; ;int main() int a,b; cin>>a>>b; Derived dr(a,b); return 0;1220: 繼承與派生5Description由Array類派生
20、出有序數(shù)組SortArray類,SortArray類中實(shí)現(xiàn)有序數(shù)組的插入。已知Array類的定義如下(不允許增加成員函數(shù)):class Arraypublic: Array(); /構(gòu)造函數(shù),初始化為空數(shù)組(length置為0) int Length(); /獲取數(shù)組的實(shí)際長(zhǎng)度 double Get(int pos); /獲取data中下標(biāo)為pos的元素的值 void Insert(int pos, double x); /在下標(biāo)pos處插入x void Display(); /輸出線性表 private: double dataMaxSize; /存儲(chǔ)元素(MaxSize為常量) int l
21、ength; /數(shù)組的實(shí)際長(zhǎng)度; SortArray類定義如下(不允許增加成員函數(shù)):class SortArray:private Arraypublic: SortArray(); int Length(); /獲取數(shù)組的實(shí)際長(zhǎng)度 double Get(int pos); /獲取data中下標(biāo)為pos的元素的值 void Display(); /輸出線性表 void Insert(double x); /遞增有序數(shù)組中插入x,使序列仍有序;請(qǐng)實(shí)現(xiàn)Array類和SortArray類的成員函數(shù), main中輸入若干個(gè)實(shí)數(shù),以0結(jié)束,利用SortArray類中的Insert函數(shù)將它們插入data
22、中,得到有序序列,再利用Display函數(shù)輸出有序序列。代碼如下(不允許修改):int main() SortArray sa; double num; while(1) cin>>num; if(fabs(num)<=1e-6) break; try sa.Insert(num); / catch(char* message) cout <<message<<endl; /如失敗提示失敗信息 sa.Display(); return 0;InputOutputSample Input2.5 6.7 8.3 2.8 6.53 6.82 7.33 0Sa
23、mple OutputThe length:7The elements:2.5 2.8 6.53 6.7 6.82 7.33 8.3*#include <iostream>#include <cmath>using namespace std;const int MaxSize=100;/順序表的最大長(zhǎng)度class Arraypublic:Array(); /構(gòu)造函數(shù),初始化為空數(shù)組(length置為0)int Length(); /獲取順序表實(shí)際長(zhǎng)度double Get(int pos); /獲取下標(biāo)為pos的元素的值void Insert(int pos, doub
24、le x); /在下標(biāo)pos處插入xvoid Display();/輸出線性表private:double dataMaxSize; /存儲(chǔ)元素int length; /數(shù)組的實(shí)際長(zhǎng)度;Array:Array()length=0;int Array:Length()return length;double Array:Get(int pos)if (pos<0 | pos>length-1)/下標(biāo)不合法throw "Illegal position"return datapos;void Array:Insert(int pos, double x) /在下標(biāo)p
25、os處插入x int i; if (length>=MaxSize) /表滿不能插入 throw "Overflow" if (pos<0 |pos>length)/下標(biāo)不合法 throw "Illegal position" for (i=length-1;i>=pos;i-)/將下標(biāo)大于等于pos的元素后移 datai+1=datai; datapos=x; /在下標(biāo)pos處插入元素x length+; /線性表長(zhǎng)度增1void Array:Display()/輸出線性表 int i;cout<<"The
26、 length:"<<length<<endl;cout<<"The elements:" for (i=0;i<length;i+)cout<<datai<<" " cout<<endl;/class SortArrayclass SortArray:private Arraypublic:SortArray();int Length();double Get(int pos);void Display();void Insert(double x); /遞增有序數(shù)
27、組中插入x,使序列仍有序;SortArray:SortArray():Array()int SortArray:Length()return Array:Length();double SortArray:Get(int pos)return Array:Get(pos);void SortArray:Display()Array:Display();void SortArray:Insert(double x)/insert int i; if(Length()>=MaxSize) throw"Overflow"for(i=0;i<Length();i+)if
28、(Get(i)>x)break;Array:Insert(i,x); int main()SortArray sa;double num;while(1)cin>>num;if(fabs(num)<=1e-6) break;trysa.Insert(num); /catch(char* message)cout <<message<<endl; /如失敗提示失敗信息sa.Display(); return 0;1221: 繼承與派生6Description已知Array類的定義如下(不允許增加成員函數(shù)):class Arraypublic: Ar
29、ray(int size); /構(gòu)造函數(shù),初始化數(shù)據(jù)成員(為data分配內(nèi)存,MaxSize置為size,length置為0) int Length(); /獲取順序表實(shí)際長(zhǎng)度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Insert(int pos, double x); /在下標(biāo)pos處插入x void Display(); /輸出線性表 private: double *data; /存儲(chǔ)元素 int MaxSize; int length; /數(shù)組的實(shí)際長(zhǎng)度; SortArray類定義如下(不允許增加其它成員函數(shù)):class SortArray:
30、private Arraypublic: SortArray(int size); int Length(); /獲取順序表實(shí)際長(zhǎng)度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Display(); /輸出線性表 void Insert(double x); /遞增有序數(shù)組中插入x,使序列仍有序;main中的代碼如下(不允許改動(dòng)):int main() int size; cin>>size; SortArray sa(size); double num; while(1) cin>>num; if(fabs(num)<=1e
31、-6) break; try sa.Insert(num); catch(char* wrong) cout <<wrong<<endl; /如失敗提示失敗信息 sa.Display(); return 0;請(qǐng)實(shí)現(xiàn)Array類和SortArray類的成員函數(shù)。InputOutputSample Input20 2.5 6.7 8.3 2.8 6.53 6.82 7.33 0Sample OutputThe length:7The elements:2.5 2.8 6.53 6.7 6.82 7.33 8.3 *#include <iostream>#incl
32、ude <cmath>using namespace std;class Arraypublic: Array(int size); /構(gòu)造函數(shù),初始化數(shù)據(jù)成員(為data分配內(nèi)存,MaxSize置為size,length置為0) int Length(); /獲取順序表實(shí)際長(zhǎng)度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Insert(int pos, double x); /在下標(biāo)pos處插入x void Display(); /輸出線性表 private: double *data; /存儲(chǔ)元素 int MaxSize; int leng
33、th; /數(shù)組的實(shí)際長(zhǎng)度;Array:Array(int size)MaxSize=size;data=new doubleMaxSize;length=0;int Array:Length()return length;double Array:Get(int pos)if (pos<0 | pos>length-1)/下標(biāo)不合法throw "Illegal position"return datapos;void Array:Insert(int pos, double x) /在下標(biāo)pos處插入x int i; if (length>=MaxSize
34、) /表滿不能插入 throw "Overflow" if (pos<0 |pos>length)/下標(biāo)不合法 throw "Illegal position" for (i=length-1;i>=pos;i-)/將下標(biāo)大于等于pos的元素后移 datai+1=datai; datapos=x; /在下標(biāo)pos處插入元素x length+; /線性表長(zhǎng)度增1void Array:Display()/輸出線性表 int i;cout<<"The length:"<<length<<
35、endl;cout<<"The elements:" for (i=0;i<length;i+)cout<<datai<<" " cout<<endl;class SortArray:private Arraypublic: SortArray(int size); int Length(); /獲取順序表實(shí)際長(zhǎng)度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Display(); /輸出線性表 void Insert(double x); /遞增有序數(shù)組中插入x,
36、使序列仍有序;SortArray:SortArray(int size):Array(size)int SortArray:Length()return Array:Length();double SortArray:Get(int pos)return Array:Get(pos);void SortArray:Display()Array:Display();void SortArray:Insert(double x)/insert int i; if(Length()>=MaxSize) throw"Overflow"for(i=0;i<Length()
37、;i+)if(Get(i)>x)break;Array:Insert(i,x); int main() int size; cin>>size; SortArray sa(size); double num; while(1) cin>>num; if(fabs(num)<=1e-6) break; try sa.Insert(num); catch(char* wrong) cout <<wrong<<endl; /如失敗提示失敗信息 sa.Display(); return 0;1223: 繼承與派生7Description已知由
38、Automobille類派生出Car類和Wagon類,而后兩者共同派生出StationWagon類,各類的定義及main中的代碼(不允許改動(dòng))如下,請(qǐng)實(shí)現(xiàn)各個(gè)類的成員函數(shù),完成相應(yīng)的輸出:class Automobile /汽車類private: int power; /馬力public: Automobile(int p); void Display();class Car:virtual public Automobile /小客車類private: int seat; /座位public: Car(int p,int s); void Display();class Wagon:virtual public Automobile /小貨車類private: int load; /裝載量public: Wagon(int p,int l); void Display(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)員工服務(wù)意識(shí)培訓(xùn)
- 冷鏈物流項(xiàng)目運(yùn)營方案
- 教育培訓(xùn)在線教育培訓(xùn)機(jī)構(gòu)運(yùn)營與管理方案
- 品牌形象與營銷策略匹配度評(píng)估表
- 醫(yī)藥冷鏈運(yùn)輸國際
- 能源企業(yè)社會(huì)責(zé)任報(bào)告編制指南
- 季度項(xiàng)目進(jìn)展及成果匯報(bào)會(huì)議紀(jì)實(shí)
- 血液腫瘤練習(xí)試題及答案
- 保育師初級(jí)復(fù)習(xí)試題有答案
- 物流配送中心庫存管理優(yōu)化方案
- 2023光伏板索支承結(jié)構(gòu)技術(shù)規(guī)程
- 2024年全國“紀(jì)檢監(jiān)察”業(yè)務(wù)相關(guān)知識(shí)考試題庫(附含答案)
- 小學(xué)班主任工作經(jīng)驗(yàn)交流ppt
- 初中英語教學(xué)設(shè)計(jì)Its-time-to-watch-a-cartoon
- 2022年安徽高校教師崗前培訓(xùn)結(jié)業(yè)統(tǒng)考試題及參考答案
- 城市社區(qū)建設(shè)概論資料
- 水利監(jiān)理規(guī)劃(水利部)
- 數(shù)學(xué)-九宮數(shù)獨(dú)100題(附答案)
- 蘇教版四年級(jí)下冊(cè)科學(xué)全冊(cè)知識(shí)點(diǎn)總結(jié)
- 第三方單位考核管理辦法
- 造粒塔外壁清洗施工方案
評(píng)論
0/150
提交評(píng)論