C++类的继承与派生 实验报告.doc_第1页
C++类的继承与派生 实验报告.doc_第2页
C++类的继承与派生 实验报告.doc_第3页
C++类的继承与派生 实验报告.doc_第4页
C++类的继承与派生 实验报告.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

广西科技大学实验报告 面向对象程序设计实验 Guangxi University of Science and Technology实验报告实验课程: C+程序设计教程 实验内容: 类的继承与派生 院 (系): 专 业: 班 级: 学生姓名: 学 号: 指导教师: 2013年 11 月 11日 9一. 实验目的: 1、掌握利用单继承和多继承的方式定义派生类的方法。 2、深刻理解在各种继承方式下构造函数和析构函数的执行顺序。 3、理解和掌握公有继承、私有继承和保护继承对基类成员的访问机制。 4、理解虚基类的目的和作用。二. 实验内容()实验题目一: 声明一个圆类作为基类,含成员数据半径R;有成员函数:构造函数实现对基类成员数 据的初始化、计算圆面积的成员函数、输出的成员函数,要求输出圆半径R。把圆类作为基 类,通过公有继承,派生圆柱体类,派生类新增成员数据有高(H);新增成员函数有构造函 数、计算圆柱体体积的函数、输出所有成员的函数。main()完成派生类对象的定义和相关函 数的测试。 2. 程序源代码:#includeusing namespace std;class roundpublic:round(float m)rad=m; float space()sp=3.14*rad*rad;return sp;void display() coutthe round of informationendl;cout该圆半径: radendl;cout该圆面积: space()endl;protected:float rad;float sp;class cylinder: public roundpublic:cylinder(float m,float n):round(m)high=n;void gethigh(float n)high=n;float room()bulk=3.14*rad*rad*high;return bulk;void play()coutthe cylinder of information:endl;cout该圆柱高: highendl;cout该圆柱体积: room()endl;protected:float high;float bulk;int main()float m,n; coutm;coutn;cylinder ss(m,n);ss.display();ss.play();3.实验结果:(二)实验题目二: 声明一个学生类,有成员函数:学号、姓名、性别、年龄,要求有如下成员函数:构造 函数,输出所有成员的函数。声明一个课程类,有成员数据:课程编号、课程名称、学时数,要求有如下成员函数:构造函数,输出所有成员的函数。将学生类和课程类作为基类,通过公有继承,派生选课类,派生类新增成员数据有:成绩;新成员函数有:构造函数,输出所有成员的函数。main()完成派生类对象的定义和相关函数的测试。 2.程序源代码:#includeusing namespace std;#includeclass student /student的类public:student() /student默认构造函数 void set1(char *n,char* id,char *s,int y)name=n;ID=id;sex=s;year=y;void get1()coutname: nameendl;coutID:IDendl;coutsex: sexendl;coutthe age is: yearendl;protected:string name;string ID;string sex;int year;class subject /subject类public:subject()void set2(int si,char *sn,int st)subid=si;subname=sn;subtime=st;void get2()coutthe course name: subnameendl;coutthe course ID:subidendl;coutthe courses time is: subtimeendl;protected:int subid;string subname;int subtime;class ssubject:public student,public subjectpublic:/*ssubject():student(),subject()*/void set3(int sc)score=sc;void get3()coutthe courses score isscoreendl;protected:int score;;void main()student s; s.set1(liuguanhua,20120040157,nan,20);s.get1(); subject ss; ss.set2(1,shuxue,48);coutendl; ss.get2();coutendl;ssubject sss;sss.set1(fuzizhou,201200403152,nan,21); sss.set2(1,shuxue,48); sss.set3(80); sss.get1(); sss.get2();sss.get3();3. 实验结果(三)实验题目三:设计一个汽车类Vehicle,包含数据成员车轮和重量,由它派生出类Car 和 类Truck,前者包含载客数,后者包含载重量。编写程序实现。 1 程序源代码:#includeusing namespace std;class vehiclepublic:void get(int we,int wh)weight=we;wheel=wh;void display()coutthe weight isweightendl;coutthe wheel iswheelendl;protected:int weight;int wheel; ;class car :public vehiclepublic:void get1(int m)busload=m;void play1()coutthe busload isbusloadendl;protected:int busload;class truck:public vehiclepublic:void get2(int m)deadweight=m; void play2()coutthe deadweight isdeadweightendl;protected:int deadweight;void main()vehicle s;car ss;truck sss;s.get(3,5);ss.get1(4);sss.get2(10);s.display();s

温馨提示

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

评论

0/150

提交评论