天津理工大学C++实验三(共19页)

上传人:文库****9 文档编号:211360962 上传时间:2021-11-16 格式:DOC 页数:19 大小:540KB
返回 下载 相关 举报
天津理工大学C++实验三(共19页)_第1页
第1页 / 共19页
天津理工大学C++实验三(共19页)_第2页
第2页 / 共19页
天津理工大学C++实验三(共19页)_第3页
第3页 / 共19页
天津理工大学C++实验三(共19页)_第4页
第4页 / 共19页
天津理工大学C++实验三(共19页)_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《天津理工大学C++实验三(共19页)》由会员分享,可在线阅读,更多相关《天津理工大学C++实验三(共19页)(19页珍藏版)》请在金锄头文库上搜索。

1、精选优质文档-倾情为你奉上天津理工大学计算机科学与技术学院实验报告 至 学年 第 学期课程名称C+程序设计学号学生姓名年级专业教学班号实验地点实验时间 年 月 日 第 节 至 第 节主讲教师辅导教师实验( 三 )实验名称派生与继承软件环境C+visual硬件环境无实验目的(1) 理解继承的含义,掌握派生类的定义方法和实现;(2) 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;(3) 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;实验内容(应包括实验题目、实验要求、实验任务等)1.#incl

2、ude #define PI 3.14159class Point / 定义“点”类int x, y;public:Point(int a=0, int b=0)x=a; y=b;void ShowPoint( )coutPoint:(x,y)n;int Getx( ) return x; int Gety( )return y;void Setxy(int a, int b)x=a; y=b;class Circle: public Point / 定义“圆”类,公有继承int r; / “圆”的半径 public:Circle(int x, int y, int ra) : Point(x

3、, y) / B r = ra; void Setr(int ra)r = ra; double Area( ) /求圆的面积return PI*r*r;void Move(int x_offset, int y_offset) /将圆心坐标平移int x1=Getx( ); /存取基类的私有成员int y1=Gety( ); / Dx1 += x_offset; y1 += y_offset;Setxy(x1, y1); / Evoid ShowCircle( )ShowPoint( ); / Fcout Radius: rt;coutArea: Area( )endl; /G;void m

4、ain( )Circle c(1, 1, 1);c.ShowCircle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5); /重新置圆心坐标 c.Setr(2); /重新置半径值 c.ShowCircle( );实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)(1) 记录程序的运行结果Point(1, 1)Radius: 1Area: 3.14159Point(2, 3)Radius: 1Area: 3.14159Point(4, 5)Radius: 2Area: 12.5664(2) 测试能否将move函数体改写为x=x+x_of

5、fset;y=y+y_offset;不可以,因为x和y是Point类的私有(private)成员。2.#include #define PI 3.14159class Point protected: /A int x, y;public:Point(int a=0, int b=0) x=a; y=b; void ShowPoint( ) coutPoint:(x,y)n; int Getx( ) return x; int Gety( ) return y; void Setxy(int a, int b) x=a; y=b; ;class Radius protected: int r;

6、public:Radius(int ra=0) r = ra; void Setr(int ra) r = ra; int Getr( ) return r; ;class Circle : public Point, public Radius public:Circle(int x, int y, int ra) : Point(x, y), Radius(ra) /D double Area( ) return PI*r*r; /直接访问基类的保护成员 void Move(int x_offset, int y_offset) x += x_offset; y += y_offset;

7、void ShowCircle( ) ShowPoint( );coutRadius: rt;coutArea: Area( )endl;void main( )Circle c(1, 1, 1);c.ShowCircle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5);c.Setr(2);c.ShowCircle( );(1) 记录程序的运行结果Point:(1,1)Radius: 1 Area: 3.14159Point:(2,3)Radius: 1 Area: 3.14159Point:(4,5)Radius: 2 Area: 12.5664(

8、2) 为什么可以直接使用x,y,x和y是Point类的保护(protected)成员(3) 如果x,y在基类中是私有的行吗?不行#include class Base1protected: int data1;public:Base1(int a=0)data1 = a; coutBase Constructor1n;Base1( )coutBase Destructor1n; ;class Base2protected: int data2;public:Base2(int a=0)data2 = a; coutBase Constructor2n;Base2( )coutBase Dest

9、ructor2n; ;class Derived: public Base1, public Base2 int d;public:Derived(int x, int y, int z):Base1(x), Base2(y) d=z; coutDerived Constructorn;Derived( ) coutDerived Destructorn; void Show( ) coutdata1,data2,dendl; ;void main( ) Derived c(1, 2, 3);c.Show( );(1) 记录程序的运行结果Base Constructor1Base Constr

10、uctor2Derived Constructor1,2,3Derived DestructorBase Destructor2Base Destructor1#include class Base1protected: int data1; public:Base1(int a=8)data1 = a; coutdata1, Base Constructor1n;Base1( )coutdata1, Base Destructor1n;class Base2protected: int data2;public:Base2(int a=9)data2 = a; coutdata2, Base

11、 Constructor2n;Base2( )coutdata2, Base Destructor2n;class Derived:public Base1, public Base2 int d;Base1 b1, b2; public:Derived(int x, int y, int z) : Base1(x), Base2(y), b1(x+y), b2(x+z) d=z; coutDerived Constructorn;Derived( ) coutDerived Destructorn;void Show( )coutdata1,data2,dendl;void main( )Derived c(1, 2, 3);c.Show( );(1) 记录程序的运行结果1, Base Constructor12, Base Constructor23, Base Constructor14, Base Constructor1Derived Constructor1,2,3Derived Destructor4, Base Destructor13, Base Destructor12, Base Destructor21, Base Destructor15.#include#includes

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号