继承与派生参考代码.doc_第1页
继承与派生参考代码.doc_第2页
继承与派生参考代码.doc_第3页
继承与派生参考代码.doc_第4页
继承与派生参考代码.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1197: 继承与派生1Description请以点类Point为基类派生出一个圆类Circle。圆类Circle的数据成员为r(私有属性,存储圆的半径,圆心的点坐标通过继承点类Point加以实现),成员函数有构造函数Circle、计算圆的面积函数Area、计算圆的周长函数Perimeter和输出函数Display,其中构造函数实现基类和圆类的数据成员的初始化,Display函数实现圆心坐标(利用基类Point的Display实现)、圆的半径、圆的面积(利用Area函数实现)和圆的周长(利用Perimeter函数实现)的输出。请编写圆类的定义及成员函数实现,并在主函数中定义圆类对象,验证各个函数的正确性。说明:圆周率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; cinxyr; /圆心的点坐标及圆的半径 Circle C(x,y,r); C.Display(); /输出圆心点坐标,圆的半径,圆的面积,圆的周长 return 0;InputOutputSample Input1.5 2.6 1.8Sample OutputCenter:Point(1.5,2.6)Radius:1.8Area:10.1736Perimeter:11.304*#includeusing namespace std;class Point public: Point(double xx,double yy) /constructor x=xx; y=yy; void Display()/display point coutCenter:Point(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();coutRadius:rendl;coutArea:Area()endl;coutPerimeter:Perimeter()xyr; /圆心的点坐标及圆的半径 Circle C(x,y,r); C.Display(); /输出圆心点坐标,圆的半径,圆的面积,圆的周长 return 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() char name81,subject81; int id; double score; cinnameidsubjectscore; CollegeStu cs(name,id,subject,score); cs.Display(); return 0;InputOutputSample InputZhangsan 2 Computer 89.5Sample OutputName:ZhangsanID:2Subject:ComputerC+ Score:89.5*#include#includeusing 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()coutName:nameendl; coutID:idendl;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()delete subject;void Display()Person:Display();coutSubject:subjectendl; coutC+ Score:scorenameidsubjectscore; Collegestu cs(name,id,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)。main的代码如下:(不允许改动)int main() char name81,subject81; int id; double score; cinnameidsubjectscore; /输入学生的姓名、id号、专业、成绩 CollegeStu cs(name,id,subject,score); cs.Person:Display(); /输出姓名,id cs.Display(); /输出专业、成绩 return 0;InputOutputSample InputLixu 5 Software 87.5Sample OutputName:LixuID:5Subject:SoftwareC+ Score:87.5*#include#includeusing 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()coutName:nameendl; coutID:idendl;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()delete subject;void Display()coutSubject:subjectendl; coutC+ Score:scorenameidsubjectscore; /输入学生的姓名、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: Base(int); Base();class Derived:public Baseprivate: int d;public: Derived(int,int); Derived();int main() int a,b; cinab; Derived dr(a,b); return 0;InputOutputSample Input1 3Sample OutputBase 1 says helloDerived 3 says hiDerived 3 says byeBase 1 says goodbye*#includeusing namespace std;class Baseprivate: int b;public: Base(int c) b=c; coutBase b says helloendl; Base() coutBase b says goodbyeendl; ;class Derived:public Baseprivate: int d;public:Derived(int c,int b):Base(c)d=b;coutDerived d says hiendl; Derived() coutDerived d says byeab; Derived dr(a,b); return 0;1220: 继承与派生5Description由Array类派生出有序数组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 length; /数组的实际长度; 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中,得到有序序列,再利用Display函数输出有序序列。代码如下(不允许修改):int main() SortArray sa; double num; while(1) cinnum; if(fabs(num)=1e-6) break; try sa.Insert(num); / catch(char* message) cout messageendl; /如失败提示失败信息 sa.Display(); return 0;InputOutputSample Input2.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 #include using namespace std;const int MaxSize=100;/顺序表的最大长度class Arraypublic:Array(); /构造函数,初始化为空数组(length置为0)int Length(); /获取顺序表实际长度double Get(int pos); /获取下标为pos的元素的值void Insert(int pos, double x); /在下标pos处插入xvoid Display();/输出线性表private:double dataMaxSize; /存储元素int length; /数组的实际长度;Array:Array()length=0;int Array:Length()return length;double Array:Get(int pos)if (poslength-1)/下标不合法throw Illegal position;return datapos;void Array:Insert(int pos, double x) /在下标pos处插入x int i; if (length=MaxSize) /表满不能插入 throw Overflow; if (poslength)/下标不合法 throw Illegal position; for (i=length-1;i=pos;i-)/将下标大于等于pos的元素后移 datai+1=datai; datapos=x; /在下标pos处插入元素x length+; /线性表长度增1void Array:Display()/输出线性表 int i;coutThe length:lengthendl;coutThe elements:; for (i=0;ilength;i+)coutdatai ; cout=MaxSize) throwOverflow;for(i=0;ix)break;Array:Insert(i,x); int main()SortArray sa;double num;while(1)cinnum;if(fabs(num)=1e-6) break;trysa.Insert(num); /catch(char* message)cout messagesize; SortArray sa(size); double num; while(1) cinnum; if(fabs(num)=1e-6) break; try sa.Insert(num); catch(char* wrong) cout wrongendl; /如失败提示失败信息 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 #include 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 length; /数组的实际长度;Array:Array(int size)MaxSize=size;data=new doubleMaxSize;length=0;int Array:Length()return length;double Array:Get(int pos)if (poslength-1)/下标不合法throw Illegal position;return datapos;void Array:Insert(int pos, double x) /在下标pos处插入x int i; if (length=MaxSize) /表满不能插入 throw Overflow; if (poslength)/下标不合法 throw Illegal position; for (i=length-1;i=pos;i-)/将下标大于等于pos的元素后移 datai+1=datai; datapos=x; /在下标pos处插入元素x length+; /线性表长度增1void Array:Display()/输出线性表 int i;coutThe length:lengthendl;coutThe elements:; for (i=0;ilength;i+)coutdatai ; cout=MaxSize) throwOverflow;for(i=0;ix)break;Array:Insert(i,x); int main() int size; cinsize; SortArray sa(size); double num; while(1) cinnum; if(fabs(num)=1e-6) break; try sa.Insert(num);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论