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; cinxyr; /圆心的点坐标及圆的半径 Circle C(x,y,r); C.Display(); /输出圆心点坐标,圆的半径,圆的面积,圆的长 return 0;InputOutputS
3、ample 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:publ
4、ic 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()
5、; /输出圆心点坐标,圆的半径,圆的面积,圆的长 return 0;1217: 继承与派生2DescriptionPerson类派生大学生CollegeStu类(1)。设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写成员函数:构造函数Person、Display函数(显示数据成员信息)和析构函数;由Person类派生出大学生类CollegeStu,其属性有专业subject(指针类型),C+程序设计课程成绩score(double型),编写构造函数(实现数据初始化)、输出函数Display(包括name,id,subject,score)。
6、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;c
7、lass 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 scor
8、e;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+ Sc
9、ore: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
10、型),编写构造函数(实现数据初始化)、输出函数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 Softwar
11、e 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() cout
12、Name: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=score
13、1; 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的代码如下(不允改动),请完成
14、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
15、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() coutDeriv
16、ed 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 v
17、oid 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,使序列仍有序;请实现
18、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;InputOutputSa
19、mple 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
20、Insert(int pos, double x); /在下标pos处插入x void 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)
21、/在下标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; try sa.Insert(num); / catch(char* message) cout messagesize; SortArray sa(size); double num;