欢迎来到冰豆网! | 帮助中心 分享价值,成长自我!
冰豆网
全部分类
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • 党团工作>
  • ImageVerifierCode 换一换
    首页 冰豆网 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    继承与派生参考代码.docx

    • 资源ID:25607619       资源大小:18.76KB        全文页数:31页
    • 资源格式: DOCX        下载积分:10金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    继承与派生参考代码.docx

    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;


    注意事项

    本文(继承与派生参考代码.docx)为本站会员主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2022 冰点文档网站版权所有

    经营许可证编号:鄂ICP备2022015515号-1

    收起
    展开