多态性和虚函数实验报告.docx
《多态性和虚函数实验报告.docx》由会员分享,可在线阅读,更多相关《多态性和虚函数实验报告.docx(13页珍藏版)》请在三一办公上搜索。
1、多态性和虚函数 实验报告淮海工学院计算机科学系 实验报告书 课程名: C+程序设计(二) 题 目: 多态性和虚函数 班 级: 学 号: 姓 名: 评语: 成绩: 指导教师: 批阅时间: 年 月 日 C+程序设计实验报告 1、实验内容或题目 声明二维坐标类作为基类派生圆的 类,把派生类圆作为基类,派生圆柱体类。其中,基类二维坐标类有成员数据:x、y坐标值;有成员函数:构造函数实现对基类成员数据的初始化、输出的成员函数,要求输出坐标位置。派生类圆类有新增成员数据:半径;有成员函数:构造函数实现对成员数据的初始化、计算圆面积的成员函数、输出半径的成员函数。派生圆柱体类新增数据有高;新增成员函数有:构
2、造函数、计算圆柱体体积的函数和输出所有成员的函数。请完成程序代码的编写、调试。 教材393页78题。 教材416页1、4、5题。 2、实验目的与要求 理解继承与派生的概念 掌握通过继承派生出一个新的类的方法 了解多态性的概念 了解虚函数的作用与使用方法 3、实验步骤与源程序 实验步骤 先定义一个基类point,及其成员函数,然后以public的继承方式定义子类circle,再定义一个派生类cylinder,最后在main主函数中定义类对象,调用函数实现其功能。 先定义一个基类A及其重载的构造函数,然后以Public派生出子类B,再定义其构造函数,最后在main主函数中定义类对象,调用成员函数实
3、现其功能。 源代码 1.#include class Point public: Point(float=0,float=0); void setPoint(float,float); float getX const return x; C+程序设计实验报告 float getY const return y; friend ostream & operator(ostream &,const Point &); protected: ; Point:Point(float a,float b) x=a;y=b; void Point:setPoint(float a,float b) x=
4、a;y=b; ostream & operator(ostream &output,const Point &p) class Circle:public Point public: Circle(float x=0,float y=0,float r=0); void setRadius(float); float getRadius const; float area const; friend ostream &operator(ostream &,const Circle &); coutp.x,p.yendl; return output; float x,y; protected:
5、 ; Circle:Circle(float a,float b,float r):Point(a,b),radius(r) float radius; C+程序设计实验报告 void Circle:setRadius(float r) radius=r; float Circle:getRadius const return radius; float Circle:area const return 3.14159*radius*radius; ostream &operator(ostream &output,const Circle &c) coutCenter=c.x,c.y, ar
6、ea=c.areaendl; class Cylinder:public Circle public: Cylinder (float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight const; float area const; float volume const; friend ostream& operator(ostream&,const Cylinder&); return output; r=c.radius, protected: ; Cylinder:Cylinder(fl
7、oat a,float b,float r,float h):Circle(a,b,r),height(h) float height; C+程序设计实验报告 void Cylinder:setHeight(float h)height=h; float Cylinder:getHeight const return height; float Cylinder:area const return 2*Circle:area+2*3.14159*radius*height; float Cylinder:volume const return Circle:area*height; ostre
8、am &operator(ostream &output,const Cylinder& cy) coutCenter=cy.x,cy.y, r=cy.radius, h=cy.height narea=cy.area, volume=cy.volumeendl; int main Cylinder cy1(3.5,6.4,5.2,10); coutnoriginal cylinder:nx=cy1.getX, y=cy1.getY, r= return output; cy1.getRadius, h=cy1.getHeightnarea=cy1.area , volume=cy1.volu
9、meendl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); coutnnew cylinder:ncy1; Point &pRef=cy1; coutnpRef as a point:pRef; Circle &cRef=cy1; C+程序设计实验报告 coutncRef as a Circle:cRef; return 0; 2(1) #include using namespace std; class A public: Aa=0;b=0; A(int i)a=i;b=0; A(int i,int j)a=i;b=j
10、; void displaycouta=a b=b; private: int a; int b; ; class B : public A public: Bc=0; B(int i):A(i)c=0; B(int i,int j):A(i,j)c=0; B(int i,int j,int k):A(i,j)c=k; void display1 display; cout c=cendl; private: C+程序设计实验报告 int c; ; int main B b1; B b2(1); B b3(1,3); B b4(1,3,5); b1.display1; b2.display1;
11、 b3.display1; b4.display1; (2) #include using namespace std; class A public: Acoutconstructing A endl; return 0; Acoutdestructing A endl; ; class B : public A public: Bcoutconstructing B endl; Bcoutdestructing B endl; ; C+程序设计实验报告 class C : public B public: Ccoutconstructing C endl; Ccoutdestructing
12、 C endl; ; int main C c1; return 0; 3.(1)/Point.h class Point public: Point(float=0,float=0); void setPoint(float,float); float getX const return x; float getY const return y; friend ostream & operator(ostream &,const Point &); protected: /Point.cpp Point:Point(float a,float b) x=a;y=b; void Point:s
13、etPoint(float a,float b) x=a;y=b; ostream & operator(ostream &output,const Point &p) float x,y; C+程序设计实验报告 outputp.x,p.yendl; return output; /Circle.h #include point.h class Circle:public Point public: Circle(float x=0,float y=0,float r=0); void setRadius(float); float getRadius const; float area co
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多态性和虚函数 实验报告 多态性 函数 实验 报告
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-3391497.html