天津理工大学C实验三.doc
《天津理工大学C实验三.doc》由会员分享,可在线阅读,更多相关《天津理工大学C实验三.doc(19页珍藏版)》请在课桌文档上搜索。
1、#理工大学计算机科学与技术学院实验报告至 学年 第 学期课程名称C+程序设计学号学生#年级专业教学班号实验地点实验时间 年 月 日 第 节 至 第 节主讲教师辅导教师实验 三 实验名称派生与继承软件环境C+visual硬件环境无实验目的(1) 理解继承的含义,掌握派生类的定义方法和实现;(2) 理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;(3) 理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;实验内容应包括实验题目、实验要求、实验任务等1.#include #define PI 3.141
2、59class Point / 定义点类int x, y;public:Pointx=a; y=b;void ShowPointcoutPoint:x,yn;int Getx return x; int Getyreturn y;void Setxyx=a; y=b;class Circle: public Point / 定义圆类,公有继承int r; / 圆的半径 public:Circle : Point / B r = ra; void Setrr = ra; double Area /求圆的面积return PI*r*r;void Move /将圆心坐标平移int x1=Getx;
3、/存取基类的私有成员int y1=Gety; / Dx1 += x_offset; y1 += y_offset;Setxy; / Evoid ShowCircleShowPoint; / Fcout Radius: rt;coutArea: Areaendl; /G;void mainCircle c;c.ShowCircle;c.Move;c.ShowCircle;c.Setxy; /重新置圆心坐标 c.Setr; /重新置半径值 c.ShowCircle;实验过程与实验结果可包括实验实施的步骤、算法描述、流程、结论等(1) 记录程序的运行结果PointRadius: 1Area: 3.1
4、4159PointRadius: 1Area: 3.14159PointRadius: 2Area: 12.5664(2) 测试能否将move函数体改写为x=x+x_offset;y=y+y_offset;不可以,因为x和y是Point类的私有private成员.2.#include #define PI 3.14159class Point protected: /A int x, y;public:Point x=a; y=b; void ShowPoint coutPoint:x,yn; int Getx return x; int Gety return y; void Setxy x
5、=a; y=b; ;class Radius protected: int r;public:Radius r = ra; void Setr r = ra; int Getr return r; ;class Circle : public Point, public Radius public:Circle : Point, Radius /D double Area return PI*r*r; /直接访问基类的保护成员 void Move x += x_offset; y += y_offset; void ShowCircle ShowPoint;coutRadius: rt;cou
6、tArea: Areaendl;void mainCircle c;c.ShowCircle;c.Move;c.ShowCircle;c.Setxy;c.Setr;c.ShowCircle;(1) 记录程序的运行结果Point:Radius: 1 Area: 3.14159Point:Radius: 1 Area: 3.14159Point:Radius: 2 Area: 12.5664(2) 为什么可以直接使用x,y,x和y是Point类的保护protected成员(3) 如果x,y在基类中是私有的行吗?不行#include class Base1protected: int data1;p
7、ublic:Base1data1 = a; coutBase Constructor1n;Base1coutBase Destructor1n; ;class Base2protected: int data2;public:Base2data2 = a; coutBase Constructor2n;Base2coutBase Destructor2n; ;class Derived: public Base1, public Base2 int d;public:Derived:Base1, Base2 d=z; coutDerived Constructorn;Derived coutD
8、erived Destructorn; void Show coutdata1,data2,dendl; ;void main Derived c;c.Show;(1) 记录程序的运行结果Base Constructor1Base Constructor2Derived Constructor1,2,3Derived DestructorBase Destructor2Base Destructor1#include class Base1protected: int data1; public:Base1data1 = a; coutdata1, Base Constructor1n;Bas
9、e1coutdata1, Base Destructor1n;class Base2protected: int data2;public:Base2data2 = a; coutdata2, Base Constructor2n;Base2coutdata2, Base Destructor2n;class Derived:public Base1, public Base2 int d;Base1 b1, b2; public:Derived : Base1, Base2, b1, b2 d=z; coutDerived Constructorn;Derived coutDerived D
10、estructorn;void Showcoutdata1,data2,dendl;void mainDerived c;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#includeusi
11、ng namespace std;class baseprotected: float price; /单价 string place; /产地 int count; /库存量 public: base price=p; count=c; place=pl; void in_something count+=add_t; void out_something ifcountcount=0; /变0保护 小于0的按0计算 else count-=del_t; double total_price return price*count; int r_count return count; ;cla
12、ss shirt:public baseprotected: string material;public: shirt:base material=m; void print coutn衬衣t产地:placet材料:material n库存量:countt单价:pricet总价格total_priceendl; ;class wardrobe:public baseprotected: string wood; string color;public: wardrobe:base wood=w; color=co; void print coutn立柜t产地:placet颜色:colort木
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 天津 理工大学 实验

链接地址:https://www.desk33.com/p-7109.html