




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1197: 继承与派生1Description请以点类Point为基类派生出一个圆类Circle。圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,其中构造函数实现基类和圆类的数据成员的初始化,Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函
2、数的正确性。说明:圆周率PI的取值为3.14已知Point类的定义及main代码如下:(不允许改动)class Point public: Point(double xx,double yy); /constructor void Display(); /display point private: double x,y; /平面的点坐标x,y; int main() double x,y,r; cin>>x>>y>>r; /圆心的点坐标及圆的半径 Circle C(x,y,r); C.Display(); /输出圆心点坐标,圆的半径,圆的面积,圆的周长 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; /平面的点坐标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; /圆心的点坐标及圆的半径 Circle C(x,y,r); C.Display(); /输出圆心点坐标,圆的半径,圆的面积,圆的周长 r
6、eturn 0;1217: 继承与派生2DescriptionPerson类派生大学生CollegeStu类(1)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C+程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数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类派生大学生CollegeStu类(2)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C+程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(只输出subject,score)。ma
12、in的代码如下:(不允许改动)int main() char name81,subject81; int id; double score; cin>>name>>id>>subject>>score; /输入学生的姓名、id号、专业、成绩 CollegeStu cs(name,id,subject,score); cs.Person:Display(); /输出姓名,id cs.Display(); /输出专业、成绩 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; /输入学生的姓名、id号、专业、成绩 CollegeStu cs(name,id,subject,score); cs.Person:Display(); /输出姓名,id cs.Display(); /输出专业、成绩 return 0;1219: 继承与派生4Description已知Base为基类,派生出Derived类,两个类的定义及main的代码如下(不允许改动),请完成Base类和Derived类的构造函数和析构函数,能够根据输入获取相应的输出。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、出有序数组SortArray类,SortArray类中实现有序数组的插入。已知Array类的定义如下(不允许增加成员函数):class Arraypublic: Array(); /构造函数,初始化为空数组(length置为0) int Length(); /获取数组的实际长度 double Get(int pos); /获取data中下标为pos的元素的值 void Insert(int pos, double x); /在下标pos处插入x void Display(); /输出线性表 private: double dataMaxSize; /存储元素(MaxSize为常量) int l
21、ength; /数组的实际长度; SortArray类定义如下(不允许增加成员函数):class SortArray:private Arraypublic: SortArray(); int Length(); /获取数组的实际长度 double Get(int pos); /获取data中下标为pos的元素的值 void Display(); /输出线性表 void Insert(double x); /递增有序数组中插入x,使序列仍有序;请实现Array类和SortArray类的成员函数, main中输入若干个实数,以0结束,利用SortArray类中的Insert函数将它们插入data
22、中,得到有序序列,再利用Display函数输出有序序列。代码如下(不允许修改):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(); /构造函数,初始化为空数组(length置为0)int Length(); /获取顺序表实际长度double Get(int pos); /获取下标为pos的元素的值void Insert(int pos, doub
24、le x); /在下标pos处插入xvoid Display();/输出线性表private:double dataMaxSize; /存储元素int length; /数组的实际长度;Array:Array()length=0;int Array:Length()return length;double Array:Get(int pos)if (pos<0 | pos>length-1)/下标不合法throw "Illegal position"return datapos;void Array:Insert(int pos, double x) /在下标p
25、os处插入x int i; if (length>=MaxSize) /表满不能插入 throw "Overflow" if (pos<0 |pos>length)/下标不合法 throw "Illegal position" for (i=length-1;i>=pos;i-)/将下标大于等于pos的元素后移 datai+1=datai; datapos=x; /在下标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); /递增有序数
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类的定义如下(不允许增加成员函数):class Arraypublic: Ar
29、ray(int size); /构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0) int Length(); /获取顺序表实际长度 double Get(int pos); /获取下标为pos的元素的值 void Insert(int pos, double x); /在下标pos处插入x void Display(); /输出线性表 private: double *data; /存储元素 int MaxSize; int length; /数组的实际长度; SortArray类定义如下(不允许增加其它成员函数):class SortArray:
30、private Arraypublic: SortArray(int size); int Length(); /获取顺序表实际长度 double Get(int pos); /获取下标为pos的元素的值 void Display(); /输出线性表 void Insert(double x); /递增有序数组中插入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;请实现Array类和SortArray类的成员函数。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); /构造函数,初始化数据成员(为data分配内存,MaxSize置为size,length置为0) int Length(); /获取顺序表实际长度 double Get(int pos); /获取下标为pos的元素的值 void Insert(int pos, double x); /在下标pos处插入x void Display(); /输出线性表 private: double *data; /存储元素 int MaxSize; int leng
33、th; /数组的实际长度;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)/下标不合法throw "Illegal position"return datapos;void Array:Insert(int pos, double x) /在下标pos处插入x int i; if (length>=MaxSize
34、) /表满不能插入 throw "Overflow" if (pos<0 |pos>length)/下标不合法 throw "Illegal position" for (i=length-1;i>=pos;i-)/将下标大于等于pos的元素后移 datai+1=datai; datapos=x; /在下标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); /获取下标为pos的元素的值 void Display(); /输出线性表 void Insert(double x); /递增有序数组中插入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中的代码(不允许改动)如下,请实现各个类的成员函数,完成相应的输出: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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025广东东莞市谢岗镇政府第一食堂招聘厨师长、副厨2人考前自测高频考点模拟试题完整参考答案详解
- 2025福建福州市罗源县卫健系统事业单位招聘控制数卫技人员12人模拟试卷及答案详解(各地真题)
- 2025广东佛山市季华中学招聘编制教师2人考前自测高频考点模拟试题附答案详解
- 2025广东深圳市罗山科技园开发运营服务有限公司高校应届毕业生招聘拟聘模拟试卷及答案详解(各地真题)
- 2025年福建省福清市市场监督管理局招聘20人模拟试卷及答案详解(有一套)
- 2025年滇西科技师范学院公开招聘博士人员(8人)考前自测高频考点模拟试题参考答案详解
- 2025年温州永嘉县金溪镇中心卫生院招聘季节工4人模拟试卷及参考答案详解1套
- 2025第二季度重庆医科大学附属大学城医院临床医技科室人员招聘考前自测高频考点模拟试题及答案详解(网校专用)
- 2025北京市怀柔区卫生健康委员会所属事业单位第二批招聘医务人员4人考前自测高频考点模拟试题附答案详解(考试直接用)
- 2025安徽中国电信股份有限公司湾沚分公司公开招聘笔试题库历年考点版附带答案详解
- 局生态环保培训课件
- 虚拟现实技术在宠物行为干预中的临床应用-洞察阐释
- 2025至2030中国石油化工设备行业发展分析及发展趋势分析与未来投资战略咨询研究报告
- 育龄妇女生殖健康知识
- 思想道德与法治2023年版电子版教材-1
- 冻伤的处理与急救措施
- 装修公司草签合同协议
- 粮食代烘干合同协议
- 拓印基础知识课件
- 江苏凤凰科学技术小学劳动四年级上册教学计划及教学设计
- 2025年高考数学大题突破+限时集训(新高考专用)大题06概率与统计(七大题型)(学生版+解析)
评论
0/150
提交评论