多态性和虚函数 实验报告

上传人:zw****58 文档编号:41295595 上传时间:2018-05-29 格式:DOC 页数:17 大小:110.50KB
返回 下载 相关 举报
多态性和虚函数    实验报告_第1页
第1页 / 共17页
多态性和虚函数    实验报告_第2页
第2页 / 共17页
多态性和虚函数    实验报告_第3页
第3页 / 共17页
多态性和虚函数    实验报告_第4页
第4页 / 共17页
多态性和虚函数    实验报告_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《多态性和虚函数 实验报告》由会员分享,可在线阅读,更多相关《多态性和虚函数 实验报告(17页珍藏版)》请在金锄头文库上搜索。

1、淮海工学院计算机科学系实验报告书课 程 名: C+程序设计(二) 题 目: 多态性和虚函数 班 级: 学 号: 姓 名: 评语:成绩: 指导教师: 批阅时间: 年 月 日C+程序设计实验报告1、实验内容或题目(1)声明二维坐标类作为基类派生圆的 类,把派生类圆作为基类,派生圆柱体类。其中,基类二维坐标类有成员数据:x、y 坐标值;有成员函数:构造函数实现对基类成员数据的初始化、输出的成员函数,要求输出坐标位置。派生类圆类有新增成员数据:半径(R) ;有成员函数:构造函数实现对成员数据的初始化、计算圆面积的成员函数、输出半径的成员函数。派生圆柱体类新增数据有高(H) ;新增成员函数有:构造函数、

2、计算圆柱体体积的函数和输出所有成员的函数。请完成程序代码的编写、调试。(2)教材 393 页 78 题。(3)教材 416 页 1、4、5 题。2、实验目的与要求(1)理解继承与派生的概念(2)掌握通过继承派生出一个新的类的方法(3)了解多态性的概念(4)了解虚函数的作用与使用方法3、实验步骤与源程序 实验步骤先定义一个基类 point,及其成员函数,然后以 public 的继承方式定义子类 circle,再定义一个派生类 cylinder,最后在 main 主函数中定义类对象,调用函数实现其功能。先定义一个基类 A 及其重载的构造函数,然后以 Public 派生出子类 B,再定义其构造函数,

3、最后在 main 主函数中定义类对象,调用成员函数实现其功能。 源代码1.#include class Pointpublic:Point(float=0,float=0);void setPoint(float,float);float getX() const return x;C+程序设计实验报告float getY() const return y;friend ostream class Apublic:A()a=0;b=0;A(int i)a=i;b=0;A(int i,int j)a=i;b=j;void display()coutusing namespace std;clas

4、s Apublic:A()cout #include “cylinder.h“#include “point.cpp“#include “circle.cpp“#include “cylinder.cpp“int main()Cylinder cy1(3.5,6.4,5.2,10);coutusing namespace std;class Shapepublic:virtual double area() const =0; C+程序设计实验报告;class Circle:public Shapepublic:Circle(double r):radius(r) virtual double

5、 area() const return 3.14159*radius*radius; protected:double radius; ;class Rectangle:public Shapepublic:Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected:double width,height; ;class Triangle:public Shapepublic:Triangle(double w,double h):widt

6、h(w),height(h) virtual double area() const return 0.5*width*height; protected:double width,height; ;void printArea(const Shape class Shapepublic:virtual double area() const =0; ;class Circle:public Shapepublic:Circle(double r):radius(r) virtual double area() const return 3.14159*radius*radius; prote

7、cted:double radius; C+程序设计实验报告;class Square:public Shapepublic:Square(double s):side(s) virtual double area() const return side*side; protected:double side;class Rectangle:public Shapepublic:Rectangle(double w,double h):width(w),height(h) virtual double area() const return width*height; protected:do

8、uble width,height; ;class Trapezoid:public Shapepublic:Trapezoid(double t,double b,double h):top(t),bottom(t),height(h)virtual double area() const return 0.5*(top+bottom)*height;protected:double top,bottom,height; ;class Triangle:public ShapeC+程序设计实验报告public:Triangle(double w,double h):width(w),heig

9、ht(h) virtual double area() const return 0.5*width*height;protected:double width,height; ;int main()Circle circle(12.6); Square square(3.5); Rectangle rectangle(4.5,8.4); Trapezoid trapezoid(2.0,4.5,3.2); Triangle triangle(4.5,8.4); Shape *pt5= double areas=0.0; for(int i=0;iarea();cout“totol of all

10、 areas=“areasendl; return 0;4、测试数据与实验结果(可以抓图粘贴)C+程序设计实验报告C+程序设计实验报告5、结果分析与实验体会继承时,子类对基类的访问属性,基类的私有成员无论以何种方式继承在子类中都是不可访问的,唯有调用基类中的成员函数方可访问其私有变量。另外,派生类构造函数的总参数列表中的参数,应当包括基类构造函数和子对象的参数列表中的参数。而多层派生时,只需写出其直接基类的构造函数即可。如果在基类中定义了没有参数的构造函数,那么在定义派生类构造函数时可以不写基类构造函数,在调用派生类构造函数时,系统会自动首先调用基类的默认构造函数;如果在基类或子对象类型的声明中定义了参数的构造函数,那么就必须显式地定义派生类构造函数,并在派生类构造函数中写出基类或子对象类型的构造函数及其参数表。

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

当前位置:首页 > 高等教育 > 教育学

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