欢迎来到天天文库
浏览记录
ID:56798512
大小:539.00 KB
页数:19页
时间:2020-07-12
《天津理工大学C++实验三.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、天津理工大学计算机科学与技术学院实验报告至学年第学期课程名称C++程序设计学号学生姓名年级专业教学班号实验地点实验时间年月日第节至第节主讲教师辅导教师实验(三)实验名称派生与继承软件环境C++visual硬件环境无实验目的(1)理解继承的含义,掌握派生类的定义方法和实现;(2)理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;(3)理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;实验内容(应包括实验题目、实验要求、实
2、验任务等)1.#include#definePI3.14159classPoint//定义“点”类{intx,y;public:Point(inta=0,intb=0){x=a;y=b;}voidShowPoint(){cout<<"Point:("<3、有继承{intr;//“圆”的半径public:Circle(intx,inty,intra):Point(x,y)//B{r=ra;}voidSetr(intra){r=ra;}doubleArea()//求圆的面积{returnPI*r*r;}voidMove(intx_offset,inty_offset)//将圆心坐标平移{intx1=Getx();//存取基类的私有成员inty1=Gety();//Dx1+=x_offset;y1+=y_offset;Setxy(x1,y1);//E}voidShowCi4、rcle(){ShowPoint();//Fcout<<"Radius:"<5、Radius:1Area:3.14159Point(2,3)Radius:1Area:3.14159Point(4,5)Radius:2Area:12.5664(2)测试能否将move函数体改写为x=x+x_offset;y=y+y_offset;不可以,因为x和y是Point类的私有(private)成员。2.#include#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;6、}voidShowPoint(){cout<<"Point:("<7、blic:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<8、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:2A
3、有继承{intr;//“圆”的半径public:Circle(intx,inty,intra):Point(x,y)//B{r=ra;}voidSetr(intra){r=ra;}doubleArea()//求圆的面积{returnPI*r*r;}voidMove(intx_offset,inty_offset)//将圆心坐标平移{intx1=Getx();//存取基类的私有成员inty1=Gety();//Dx1+=x_offset;y1+=y_offset;Setxy(x1,y1);//E}voidShowCi
4、rcle(){ShowPoint();//Fcout<<"Radius:"<5、Radius:1Area:3.14159Point(2,3)Radius:1Area:3.14159Point(4,5)Radius:2Area:12.5664(2)测试能否将move函数体改写为x=x+x_offset;y=y+y_offset;不可以,因为x和y是Point类的私有(private)成员。2.#include#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;6、}voidShowPoint(){cout<<"Point:("<7、blic:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<8、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:2A
5、Radius:1Area:3.14159Point(2,3)Radius:1Area:3.14159Point(4,5)Radius:2Area:12.5664(2)测试能否将move函数体改写为x=x+x_offset;y=y+y_offset;不可以,因为x和y是Point类的私有(private)成员。2.#include#definePI3.14159classPoint{protected://Aintx,y;public:Point(inta=0,intb=0){x=a;y=b;
6、}voidShowPoint(){cout<<"Point:("<7、blic:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<8、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:2A
7、blic:Circle(intx,inty,intra):Point(x,y),Radius(ra)//D{}doubleArea(){returnPI*r*r;}//直接访问基类的保护成员voidMove(intx_offset,inty_offset){x+=x_offset;y+=y_offset;}voidShowCircle(){ShowPoint();cout<<"Radius:"<8、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:2A
8、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:2A
此文档下载收益归作者所有