c++类的继承编程练习.doc_第1页
c++类的继承编程练习.doc_第2页
c++类的继承编程练习.doc_第3页
c++类的继承编程练习.doc_第4页
c++类的继承编程练习.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验十六 继承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)分析下面程序,写出该程序的功能和运行结果#includeusing 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.Print();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;width=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,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(坐标点),以它为基类,派生出一个circle类(圆类),增加数据成员r(半径),再以circle作为直接基类,派生出一个cylinder(圆柱体)类,再增加数据成员h(高)。设计类中数据成员的访问属性。(2) 定义基类的派生类圆、圆柱都含有求表面积和体积的成员函数和输出函数。(3) 定义主函数,求圆、圆柱的面积和体积。 #include using namespace std;const double PI=3.141592653;class pointprotected:double X,Y;public:point(double x=0,double y=0)X=x;Y=y;void getXY()coutX;coutY;void show()coutThe 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 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();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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论