C程序设计实践指导书7答案.docx

上传人:b****5 文档编号:4523372 上传时间:2022-12-01 格式:DOCX 页数:10 大小:251.14KB
下载 相关 举报
C程序设计实践指导书7答案.docx_第1页
第1页 / 共10页
C程序设计实践指导书7答案.docx_第2页
第2页 / 共10页
C程序设计实践指导书7答案.docx_第3页
第3页 / 共10页
C程序设计实践指导书7答案.docx_第4页
第4页 / 共10页
C程序设计实践指导书7答案.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

C程序设计实践指导书7答案.docx

《C程序设计实践指导书7答案.docx》由会员分享,可在线阅读,更多相关《C程序设计实践指导书7答案.docx(10页珍藏版)》请在冰豆网上搜索。

C程序设计实践指导书7答案.docx

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

usingnamespacestd;

classStudent{

public:

Student(inti,stringn,chars,inta){

ID=i;

name=n;

sex=s;

age=a;

}

intgetID(){

returnID;

}

voidshow(){

cout<<"ID:

"<

cout<<"name:

"<

cout<<"sex:

"<

cout<<"age:

"<

}

private:

intID;

stringname;

charsex;

intage;

};

classCourse{

public:

Course(into,char*,floatch){

cnum=cno;

cname=cn;

chour=ch;

}

voidshow(){

cout<<"Coursenumber:

"<

cout<<"Coursename:

"<

cout<<"Coursehours:

"<

}

private:

intum;

stringame;

floatchour;

};

 

classSelCourse:

publicStudent,publicCourse{

public:

SelCourse(inti,stringn,chars,inta,into,char*,floatch,floatg):

Student(i,n,s,a),Course(cno,cn,ch){

score=g;

}

voidshow(){

Student:

:

show();

Course:

:

show();

cout<<"Score:

"<

}

private:

floatscore;

};

voidmain(){

Students1(0001,"林维",'S',21);

s1.show();

cout<

Coursec1(1001,"高级语言程序设计",64);

c1.show();

cout<

SelCoursesc1(9901,"张帅",'M',22,1002,"面向对象程序设计C++",56,89);

sc1.show();

}

2、

#include

#include

usingnamespacestd;

classPoint{

public:

Point(intxx=0,intyy=0){

x=xx;

y=yy;

}

intgetX(){returnx;}

intgetY(){returny;}

voidshow(){cout<<"("<

protected:

intx,y;

};

classCircle:

virtualpublicPoint{

public:

Circle(intxx=0,intyy=0,floatr=1):

Point(xx,yy){

radius=r;

}

intgetR(){returnradius;}

voidshow(){

cout<<"圆心坐标:

";

Point:

:

show();

cout<<"圆半径:

"<

}

protected:

floatradius;

};

 

classcylinder:

publicCircle{

public:

cylinder(intxx=0,intyy=0,floatr=1,floath=2):

Point(xx,yy),Circle(r){

height=h;

}

intgetH(){returnheight;}

voidshow(){

Circle:

:

show();

cout<<"圆柱体高度:

"<

}

private:

floatheight;

};

intmain(){

Pointp1(1,2);

p1.show();

cout<

Circlec1(2,2,3);

c1.show();

cout<

cylindercy1;

cy1.show();

system("pause");

return0;

}

不使用虚基类。

如果circle类继承point,cylinder继承circle,并且在cylinder类中Point(xx,yy),Circle(r)这样在构造函数中赋值就会报错“错误1errorC2614:

“cylinder”:

非法的成员初始化:

“Point”不是基或成员”。

修改办法一,将point设置为虚基类,修改办法二,在cylinder构造函数中通过Circle(xx,yy,r)传值给point。

#include

#include

usingnamespacestd;

classPoint{

public:

Point(intxx=0,intyy=0){

x=xx;

y=yy;

}

intgetX(){returnx;}

intgetY(){returny;}

voidshow(){cout<<"("<

protected:

intx,y;

};

classCircle:

publicPoint{

public:

Circle(intxx=0,intyy=0,floatr=1):

Point(xx,yy){

radius=r;

}

intgetR(){returnradius;}

voidshow(){

cout<<"圆心坐标:

";

Point:

:

show();

cout<<"圆半径:

"<

}

protected:

floatradius;

};

 

classcylinder:

publicCircle{

public:

cylinder(intxx=0,intyy=0,floatr=1,floath=2):

Circle(xx,yy,r){

height=h;

}

intgetH(){returnheight;}

voidshow(){

Circle:

:

show();

cout<<"圆柱体高度:

"<

}

private:

floatheight;

};

intmain(){

Pointp1(1,2);

p1.show();

cout<

Circlec1(2,2,3);

c1.show();

cout<

cylindercy1(5,6,7,8);

cy1.show();

system("pause");

return0;

}

 

四、思考题

1、继承与派生的过程。

五、结果分析

六、指导教师评阅成绩

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高中教育

copyright@ 2008-2022 冰豆网网站版权所有

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