文档详情

C++类的继承与派生实验报告.doc

发布:2015-08-12约2.88千字共10页下载文档
文本预览下载声明
Guangxi University of Science and Technology 实验报告 实验课程: C++程序设计教程 实验内容: 类的继承与派生 院 (系): 专 业: 班 级: 学生姓名: 学 号: 指导教师: 2013年 月日 (二)实验题目二: 声明一个学生类,有成员函数:学号、姓名、性别、年龄,要求有如下成员函数:构造 函数,输出所有成员的函数。声明一个课程类,有成员数据:课程编号、课程名称、学时数,要求有如下成员函数:构造函数,输出所有成员的函数。将学生类和课程类作为基类,通过公有继承,派生选课类,派生类新增成员数据有:成绩;新成员函数有:构造函数,输出所有成员的函数。main()完成派生类对象的定义和相关函数的测试。 2.程序源代码: #includeiostream using namespace std; #includestring class 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 subject { public: /*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(); } 实验结果 (三)实验题目三:设计一个汽车类Vehicle,包含数据成员车轮和重量,由它派生出类Car 和 类Truck,前者包含载客数,后者包含载重量。编写程序实现。 程序源代码: #includeiostream using namespace std; class vehicle { public: void get(int we,int wh) { weight=we; wheel=wh; } void display() { coutthe weight isweightendl; coutthe wheel iswheelendl; } protected: int weight; int wheel; }
显示全部
相似文档