繼承與派生參考代碼_第1頁
繼承與派生參考代碼_第2頁
繼承與派生參考代碼_第3頁
繼承與派生參考代碼_第4頁
繼承與派生參考代碼_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1197: 繼承與派生1Description請以點類Point為基類派生出一個圓類Circle。圓類Circle的數(shù)據(jù)成員為r(私有屬性,存儲圓的半徑,圓心的點坐標(biāo)通過繼承點類Point加以實現(xiàn)),成員函數(shù)有構(gòu)造函數(shù)Circle、計算圓的面積函數(shù)Area、計算圓的周長函數(shù)Perimeter和輸出函數(shù)Display,其中構(gòu)造函數(shù)實現(xiàn)基類和圓類的數(shù)據(jù)成員的初始化,Display函數(shù)實現(xiàn)圓心坐標(biāo)(利用基類Point的Display實現(xiàn))、圓的半徑、圓的面積(利用Area函數(shù)實現(xiàn))和圓的周長(利用Perimeter函數(shù)實現(xiàn))的輸出。請編寫圓類的定義及成員函數(shù)實現(xiàn),并在主函數(shù)中定義圓類對象,驗證各個函

2、數(shù)的正確性。說明:圓周率PI的取值為3.14已知Point類的定義及main代碼如下:(不允許改動)class Point public: Point(double xx,double yy); /constructor void Display(); /display point private: double x,y; /平面的點坐標(biāo)x,y; int main() double x,y,r; cin>>x>>y>>r; /圓心的點坐標(biāo)及圓的半徑 Circle C(x,y,r); C.Display(); /輸出圓心點坐標(biāo),圓的半徑,圓的面積,圓的周長 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; /平面的點坐標(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; /圓心的點坐標(biāo)及圓的半徑 Circle C(x,y,r); C.Display(); /輸出圓心點坐標(biāo),圓的半徑,圓的面積,圓的周長 r

6、eturn 0;1217: 繼承與派生2DescriptionPerson類派生大學(xué)生CollegeStu類(1)。設(shè)計一個Person類,其屬性包括姓名name和身份證號id,其中name為指針類型,id為整型,編寫成員函數(shù):構(gòu)造函數(shù)Person、Display函數(shù)(顯示數(shù)據(jù)成員信息)和析構(gòu)函數(shù);由Person類派生出大學(xué)生類CollegeStu,其屬性有專業(yè)subject(指針類型),C+程序設(shè)計課程成績score(double型),編寫構(gòu)造函數(shù)(實現(xiàn)數(shù)據(jù)初始化)、輸出函數(shù)Display(包括name,id,subject,score)。main的代碼如下:(不允許改動)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è)計一個Person類,其屬性包括姓名name和身份證號id,其中name為指針類型,id為整型,編寫成員函數(shù):構(gòu)造函數(shù)Person、Display函數(shù)(顯示數(shù)據(jù)成員信息)和析構(gòu)函數(shù);由Person類派生出大學(xué)生類CollegeStu,其屬性有專業(yè)subject(指針類型),C+程序設(shè)計課程成績score(double型),編寫構(gòu)造函數(shù)(實現(xiàn)數(shù)據(jù)初始化)、輸出函數(shù)Display(只輸出subject,score)。ma

12、in的代碼如下:(不允許改動)int main() char name81,subject81; int id; double score; cin>>name>>id>>subject>>score; /輸入學(xué)生的姓名、id號、專業(yè)、成績 CollegeStu cs(name,id,subject,score); cs.Person:Display(); /輸出姓名,id cs.Display(); /輸出專業(yè)、成績 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號、專業(yè)、成績 CollegeStu cs(name,id,subject,score); cs.Person:Display(); /輸出姓名,id cs.Display(); /輸出專業(yè)、成績 return 0;1219: 繼承與派生4Description已知Base為基類,派生出Derived類,兩個類的定義及main的代碼如下(不允許改動),請完成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類中實現(xiàn)有序數(shù)組的插入。已知Array類的定義如下(不允許增加成員函數(shù)):class Arraypublic: Array(); /構(gòu)造函數(shù),初始化為空數(shù)組(length置為0) int Length(); /獲取數(shù)組的實際長度 double Get(int pos); /獲取data中下標(biāo)為pos的元素的值 void Insert(int pos, double x); /在下標(biāo)pos處插入x void Display(); /輸出線性表 private: double dataMaxSize; /存儲元素(MaxSize為常量) int l

21、ength; /數(shù)組的實際長度; SortArray類定義如下(不允許增加成員函數(shù)):class SortArray:private Arraypublic: SortArray(); int Length(); /獲取數(shù)組的實際長度 double Get(int pos); /獲取data中下標(biāo)為pos的元素的值 void Display(); /輸出線性表 void Insert(double x); /遞增有序數(shù)組中插入x,使序列仍有序;請實現(xiàn)Array類和SortArray類的成員函數(shù), main中輸入若干個實數(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;/順序表的最大長度class Arraypublic:Array(); /構(gòu)造函數(shù),初始化為空數(shù)組(length置為0)int Length(); /獲取順序表實際長度double Get(int pos); /獲取下標(biāo)為pos的元素的值void Insert(int pos, doub

24、le x); /在下標(biāo)pos處插入xvoid Display();/輸出線性表private:double dataMaxSize; /存儲元素int length; /數(shù)組的實際長度;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+; /線性表長度增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(); /獲取順序表實際長度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Insert(int pos, double x); /在下標(biāo)pos處插入x void Display(); /輸出線性表 private: double *data; /存儲元素 int MaxSize; int length; /數(shù)組的實際長度; SortArray類定義如下(不允許增加其它成員函數(shù)):class SortArray:

30、private Arraypublic: SortArray(int size); int Length(); /獲取順序表實際長度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Display(); /輸出線性表 void Insert(double x); /遞增有序數(shù)組中插入x,使序列仍有序;main中的代碼如下(不允許改動):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;請實現(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(); /獲取順序表實際長度 double Get(int pos); /獲取下標(biāo)為pos的元素的值 void Insert(int pos, double x); /在下標(biāo)pos處插入x void Display(); /輸出線性表 private: double *data; /存儲元素 int MaxSize; int leng

33、th; /數(shù)組的實際長度;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+; /線性表長度增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(); /獲取順序表實際長度 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中的代碼(不允許改動)如下,請實現(xiàn)各個類的成員函數(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等.壓縮文件請下載最新的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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論