当前位置:文档之家› C++程序的设计实践指导书7(答案)

C++程序的设计实践指导书7(答案)

C++程序设计实践上机指导书 (第七次) 专业 班级 学号

工程学院信息学院 实践成绩评价说明 1) 上机前充分准备实践材料,对上机容有程序草稿。(10分) 2) 独立完成实践任务,对实践过程非常清晰。(30分) 3) 认真理解知识点,能够与理论知识相结合。(10分) 4) 在机房遵守上机守则,接受实践指导教师的监督与管理。(20分) 5) 认真填写实践指导书,写出实践小结。(10分) 6) 在实践中具备一定的创新思想,能够主动与指导教师探讨。(5分) 7) 加大实践工作量,主动完成实践指导书中的选做题目。(5分) 8) 掌握程序调试的方法,认真完成程序调试工作,使程序能够运行(10分)。 上机七 继承与派生(二) 一、目的 1.理解继承与派生、单继承与多继承的概念; 2.理解基类与派生类的定义及使用方法,派生类对象及初始化方法; 3.理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。

二、要求: 1. 在上课之前,每一个同学必须将题目、程序编写完毕,做好充分的准备。 2. 所有环节均由每位同学独立完成,严禁抄袭他人结果。

三、步骤和容 1由学生类、课程类作为基类,共同派生选课类。 声明一个学生类,有成员数据:学号、、性别、年龄,要求有如下成员函数:构造函数、输出所有成员的函数。 声明一个课程类,有成员数据:课程编号(cnum)、课程名称(cname)、学时数(chour),要求有如下成员函数:构造函数、输出所有成员的函数。 将学生类和课程类作为基类,通过公有继承,共同派生选课类,派生类新增成员数据有:成绩(score);新增成员函数有:构造函数、输出所有成员的函数。 main()完成派生类对象的定义和有关成员函数的测试。 2、由二维坐标点类作为基类派生出圆类;再由圆类作为基类派生出圆柱体类。(提示:点类Point的数据成员为点坐标x、y,函数成员有构造函数和显示点坐标的函数show;Circle类新增数据成员为圆的半径radius,其成员函数show除了显示圆心的坐标外还能显示半径大小;Cylinder类新增数据成员为圆柱体高度height,其成员函数除了显示基类的所有数据成员外,还得显示圆柱体的高度) 1 #include #include using namespace std;

class Student{ public: Student(int i,string n,char s,int a){ ID=i; name=n; sex=s; age=a; }

int getID(){ return ID; } void show(){ cout<<"ID: "

class Course{ public: Course(into,char *, float ch){ cnum=cno; cname=cn; chour=ch; }

void show(){ cout<<"Course number: "

class SelCourse:public Student,public Course{ public: SelCourse(int i,string n,char s,int a,into,char*,float ch,float g):Student(i,n,s,a),Course(cno,cn,ch){ score=g; }

void show(){ Student::show(); Course::show(); cout<<"Score: "<} private: float score; };

void main(){ Student s1(0001,"林维",'S',21); s1.show(); cout<

Course c1(1001,"高级语言程序设计",64); c1.show(); cout<

SelCourse sc1(9901,"帅",'M',22,1002,"面向对象程序设计C++",56,89); sc1.show(); }

2、 #include #include using namespace std;

class Point { public: Point(int xx=0, int yy=0){ x=xx; y=yy; } int getX() { return x; } int getY() { return y; } void show() {cout<<"("

class Circle:virtual public Point{ public: Circle(int xx=0,int yy=0,float r=1):Point(xx,yy){ radius=r; } int getR() {return radius;} void show(){

cout<<"圆心坐标:"; Point::show(); cout<<"圆半径:"<} protected: float radius; };

class cylinder:public Circle{ public: cylinder(int xx=0,int yy=0,float r=1,float h=2):Point(xx,yy),Circle(r){ height=h; } int getH() {return height;} void show(){ Circle::show();

cout<<"圆柱体高度:"<} private: float height; }; int main(){ Point p1(1,2); p1.show(); cout<

Circle c1(2,2,3); c1.show(); cout<

cylinder cy1; cy1.show(); system("pause"); return 0; }

不使用虚基类。如果circle类继承point,cylinder继承circle,并且在cylinder类中Point(xx,yy),Circle(r)这样在构造函数中赋值就会报错“错误 1 error C2614: “cylinder”: 非法的成员初始化:“Point”不是基或成员 ”。修改办法一,将point设置为虚基类,修改办法二,在cylinder构造函数过Circle(xx,yy,r)传值给point。 #include #include using namespace std;

class Point { public: Point(int xx=0, int yy=0){ x=xx; y=yy; } int getX() { return x; } int getY() { return y; } void show() {cout<<"("

class Circle:public Point{ public: Circle(int xx=0,int yy=0,float r=1):Point(xx,yy){ radius=r; } int getR() {return radius;} void show(){ cout<<"圆心坐标:"; Point::show(); cout<<"圆半径:"<} protected: float radius; };

class cylinder:public Circle{ public: cylinder(int xx=0,int yy=0,float r=1,float h=2):Circle(xx,yy,r){ height=h; } int getH() {return height;} void show(){ Circle::show(); cout<<"圆柱体高度:"<} private: float height; };

int main(){ Point p1(1,2); p1.show(); cout<

Circle c1(2,2,3); c1.show(); cout<

cylinder cy1(5,6,7,8); cy1.show(); system("pause"); return 0; }

相关主题