云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 实验四 类的多态性与虚函数

实验四 类的多态性与虚函数

  • 62 次阅读
  • 3 次下载
  • 2025/5/2 18:39:39

实验四类的多态性与虚函数

(实验课时:2 实验性质:设计)

实验名称: 类的多态性与虚函数 实验目的: (1)了解多态性的概念;

(2)了解虚函数的作用和使用方法;

(3)了解纯虚函数和抽象类的概念和用法。

实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB);

(2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual

C++6.0。

实验内容: 事先编好程序,上机调试和运行程序,分析结果。

(1)编程:定义抽象基类Shape(图形)。

Shape类有一个公共的纯虚函数Area,用于派生类求图形的面积。 (2)由Shape派生出3个派生类:Circle(圆形)、Rectangle(矩形)、Triangle(三

角形),用一个函数PrintArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。

在主程序中测试这些类的PrintArea函数。

(3)在类Circle基础上再派生一个圆柱体Cylinder,重载Area函数求圆柱体的

体积。然后,编写主程序采用静态多态性和动态多态性分别测试Area函数。

(4)在类Circle中定义一个析构函数,让它输出一行信息“This is Cicle

Destruction。”,在类Cylinder中定义一个析构函数,让它输出一行信息“This is Cylinder Destruction。”。

在主程序中用指向基类Circle类的指针动态生成Cylinder类对象,然后用delete

语句释放该对象,测试看看运行结果如何。再修改Circle的析构函数为虚函数,测试看看运行结果如何。

(5)运行上述程序,并分析结果。

实验要求: (1)掌握多态性的概念;

(2)掌握虚函数的作用和使用方法;

(3)掌握纯虚函数和抽象类的概念和用法; (4)程序格式规范,程序运行正确;

(5)认真书写实验报告,如实填写各项实验内容。

实验步骤: (1)启动Visual C++6.0开发环境;

(2)创建一个项目;

(3)建立C++源程序文件; (4)编辑C++源程序文件内容;

(5)建立并运行可执行程序,并分析结果; (6)关闭工作空间。

思考题:虚函数的作用是什么?、

1

#include using namespace std;

const double PI=3.14159; class Shape {

public: Area(int x,int y=0); virtual ~Shape(); virtual void PrintArea()=0; protected: int m_nX ,m_nY; };

Shape::Area(int x,int y) {

m_nX=x; m_nY=y; }

Shape::~Shape() { cout<<\析构函数\}

//矩形

class Rectangle:public Shape {

public: Rectangle(int rx,int ry); ~Rectangle(); void PrintArea(); };

Rectangle::Rectangle(int rx,int ry) { Area(rx,ry); }

Rectangle::~Rectangle(){ cout<<\析构函数\}

void Rectangle::PrintArea(){ cout<<\矩形的面积是:\}

//三角形

class Triangle:public Shape {

2

public: Triangle(int d,int h); ~Triangle(); void PrintArea(); };

Triangle::Triangle(int d,int h) { Area(d,h); }

Triangle::~Triangle(){ cout<<\析构函数\}

void Triangle::PrintArea(){ cout<<\三角形的面积是:\}

//圆形

class Circle:public Shape{ public: Circle(int r); ~Circle(); void PrintArea(); };

Circle::Circle(int r){ Area(r); }

Circle::~Circle(){ cout<<\}

void Circle::PrintArea(){ cout<<\圆形的面积是:\}

//圆柱

class Cylinder:public Shape {

private: int rad; int height; public: Area(int r,int h); //重载shape类里函数Area(); Cylinder(int r,int h); ~Cylinder(); void PrintArea(); };

Cylinder::Area(int r,int h){

3

rad=r; height=h; }

Cylinder::Cylinder(int r,int h){ Area(r,h); }

Cylinder::~Cylinder(){ cout<<\}

void Cylinder::PrintArea(){ cout<<\圆柱的体积是:\}

int main() { Shape *ptr[4]; ptr[0]=new Rectangle(10,5); ptr[0]->PrintArea(); delete ptr[0]; ptr[1]=new Circle(2); ptr[1]->PrintArea(); delete ptr[1]; ptr[2]=new Triangle(4,4); ptr[2]->PrintArea(); delete ptr[2]; ptr[3]=new Cylinder(2,6); ptr[3]->PrintArea(); delete ptr[3]; return 0; }

4

搜索更多关于: 实验四 类的多态性与虚函数 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

实验四类的多态性与虚函数 (实验课时:2 实验性质:设计) 实验名称: 类的多态性与虚函数 实验目的: (1)了解多态性的概念; (2)了解虚函数的作用和使用方法; (3)了解纯虚函数和抽象类的概念和用法。 实验设备: (1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB); (2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual C++6.0。 实验内容: 事先编好程序,上机调试和运行程序,分析结果。 (1)编程:定义抽象基类Shape(图形)。 Shape类有一个公共的纯虚函数Area,用于派生类求图形的面积。 (2)由Shape派生出

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com