c++类的继承编程练习.doc

上传人:F****n 文档编号:98082086 上传时间:2019-09-08 格式:DOC 页数:7 大小:96.50KB
返回 下载 相关 举报
c++类的继承编程练习.doc_第1页
第1页 / 共7页
c++类的继承编程练习.doc_第2页
第2页 / 共7页
c++类的继承编程练习.doc_第3页
第3页 / 共7页
c++类的继承编程练习.doc_第4页
第4页 / 共7页
c++类的继承编程练习.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《c++类的继承编程练习.doc》由会员分享,可在线阅读,更多相关《c++类的继承编程练习.doc(7页珍藏版)》请在金锄头文库上搜索。

1、实验十六 继承1、实验目的1)掌握继承的实现方法;2)继承中常见问题的处理方法。2、实验内容1分析下面的程序,指出程序运行的结果#includeclass CBasepublic:void fn1();void CBase:fn1()cout调用基类类的函数fn1()n;class CDerived:public CBasepublic:void fn1();void CDerived:fn1()coutfn1();pd.fn1();运行结果:调用派生类的函数fn1()调用基类类的函数fn1()调用基类类的函数fn1()2.2编写并调试程序:1)分析下面程序,写出该程序的功能和运行结果#inc

2、ludeusing namespace std;class Aprivate:int a;public:A()a=0;A(int i)a=i;void Print()couta,;class B:public Aprivate:int b1,b2;public:B()b1=0;b2=0;B(int i)b1=1;b2=0;B(int i,int j,int k):A(i),b1(j),b2(k)void Print()A:Print();coutb1,b2endl;int main()B ob1,ob2(1),ob3(3,6,9);ob1.Print();ob2.Print();ob3.Pri

3、nt();return 0;运行结果:0,0,00,1,03,6,9程序功能:输出空间一点的坐标。2)定义一个图形类,其中有保护类型的成员数据:高度和宽度,一个公有的构造函数。由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类中都包含一个函数area(),分别用来计算矩形和等腰三角形的面积。#include using namespace std;class figureprotected:double height,width;public:figure(double=0,double=0);figure:figure(double h,double w)height=h;widt

4、h=w;class triangle:public figurepublic:double area();triangle(double=0,double=0);triangle:triangle(double h,double w):figure(h,w)height=h;width=w;double triangle:area()return 0.5*height*width;class rectangle:public figurepublic:double area();rectangle(double=0,double=0);rectangle:rectangle(double h,

5、double w):figure(h,w)height=h;width=w;double rectangle:area()return height*width;int main()triangle tri(2,3);rectangle rec(2,3);coutThe area of triangle is:tri.area()endl;coutThe area of rectangle is:rec.area()endl;return 0;3)编写一个程序计算出圆和圆柱体的表面积和体积。要求:(1) 定义一个点(point)类,包含数据成员x,y(坐标点),以它为基类,派生出一个circl

6、e类(圆类),增加数据成员r(半径),再以circle作为直接基类,派生出一个cylinder(圆柱体)类,再增加数据成员h(高)。设计类中数据成员的访问属性。(2) 定义基类的派生类圆、圆柱都含有求表面积和体积的成员函数和输出函数。(3) 定义主函数,求圆、圆柱的面积和体积。 #include using namespace std;const double PI=3.;class pointprotected:double X,Y;public:point(double x=0,double y=0)X=x;Y=y;void getXY()coutX;coutY;void show()co

7、utThe coordinate of the point is:(X,Y).endl;class circle:protected pointprotected:double radius;public:circle(double x=0,double y=0,double r=0):point(x,y)X=x;Y=y;radius=r;void getXYradius()circle:getXY();coutradius;double area()return PI*radius*radius;class cylinder:protected circleprotected:double

8、height;public:cylinder(double x=0,double y=0,double r=0,double h=0):circle(x,y,r)X=x;Y=y;radius=r;height=h;void getdata()circle:getXYradius();coutheight;double area()return PI*radius*radius*2+2*PI*radius*height;double V()return PI*radius*radius*height;int main()circle ci;cylinder cy;ci.getXYradius()

9、;cy.getdata();coutThe area of the circle is:ci.area()endl;coutThe area of the cylinder is:cy.area()endl;coutThe volume of the cylinder is:cy.V()endl;实验总结:1.个人感觉面向对象比面向过程更加直观。2.就是因为面向对象的思路是用程序语言直接描述客观世界,它程序的长度会更长,但是一个大程序会被分割成多个小部分,每一部分分开编程,然后再调试各个部分的连接情况,当程序比较大的时候,工作量其实相对更小。在能力与知识结构方面,要求学生应具有扎实的专业和日语语言基础,熟练掌握日语听、说、读、写、译的基本技能;了解日本社会及日本文化等方面的基本知识,熟悉日本国情,具有一定的日本人文知识及运用这些知识与日本人进行交流的能力。

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

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

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