虚函数与多态性实验

上传人:工**** 文档编号:478373671 上传时间:2023-02-09 格式:DOC 页数:10 大小:151KB
返回 下载 相关 举报
虚函数与多态性实验_第1页
第1页 / 共10页
虚函数与多态性实验_第2页
第2页 / 共10页
虚函数与多态性实验_第3页
第3页 / 共10页
虚函数与多态性实验_第4页
第4页 / 共10页
虚函数与多态性实验_第5页
第5页 / 共10页
点击查看更多>>
资源描述

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

1、word一实验目的与要求1进一步熟悉类的设计、运用继承与派生机制设计派生类,合理设置数据成员和成员函数。2掌握通过继承、虚函数、基类的指针或引用实现动态多态性的方法。3理解并掌握具有纯虚函数的抽象类的作用,在各派生类中重新定义各纯虚函数的方法,以与此时实现的动态多态性。二实验内容在自己的文件夹下建立一个名为exp5的工程,在该工程中做如下操作:定义一个抽象类容器类,其中定义了假如干纯虚函数,实现求外表积、体积、输出等功能。由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。

2、1在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用一样的函数能执行不同的函数代码,从而实现动态多态性。2定义一个顶层函数void TopPrint(Container &r);使得主函数中调用该函数时,根据实在参数所有的类自动调用对应类的输出函数。3主函数中定义一个Container类对象,观察编译时的错误信息,从而得出什么结论三实验程序与运行结果#include using namespace std;class Basepublic:virtual void f() cout 调用Base:f() endl; ;class Derived: public Basep

3、ublic:void f() cout 调用Derived:f() f();/ 调用函数f()system(PAUSE); return 0; 2.#include using namespace std;/class Basepublic:virtual void Show() const cout 调用Base:Show() endl; / 虚函数;class Derived: public Basepublic:void Show() const cout 调用Derived:Show() endl; ;void Refers(const Base &obj)/ 基类引用obj.Show

4、();/ 调用函数Show()int main(void)Base obj1;Derived obj2;/ 定义对象Refers(obj1);/ 调用函数Refers()Refers(obj2);system(PAUSE); return 0; 3.#include using namespace std;/class Baseprivate:int m;public:Base(int a): m(a) virtual void Show() const cout m endl; / 虚函数;class Derived: public Baseprivate:int n;public:Deri

5、ved(int a, int b): Base(a), n(a) / 构造函数void Show() const cout n Show();p = &obj2;p-Show();p-Base:Show();system(PAUSE); return 0; 4.#include using namespace std;class Apublic:virtual void Show() const cout 基类A endl; ;class B: public Apublic:void Show() const cout 派生类B Show();system(PAUSE);return 0;5.

6、#include using namespace std;const double PI = 3.1415926;class Shapepublic:virtual void Show() const = 0;static double sum;class Circle: public Shapeprivate:double radius;public:Circle(double r): radius(r) sum += PI * radius * radius; void Show() const cout 圆形: endl;cout 半径: radius endl;cout 面积: PI

7、* radius * radius endl;class Rectangle: public Shapeprivate:double height;double width;public:Rectangle(double h, double w): height(h), width(w) sum += height * width; void Show() const cout 矩形: endl;cout 高: height endl;cout 宽: width endl;cout 面积: height * width endl;double Shape:sum = 0;int main(vo

8、id)char flag = Y;Shape *p;while (toupper(flag) = Y)cout select;switch (select)case 1:double r;cout r;p = new Circle(r);p-Show();delete p;break;case 2:double h, w;cout h;cout w;p = new Rectangle(h, w);p-Show();/ 显示相关信息delete p;/ 释放存储空间break;default:/ 其它情况, 表示选择有误cout 选择有误! endl;break;cout endl flag;c

9、out 总面积: Shape:sum endl;system(PAUSE); return 0; 6.#include using namespace std;const double PI = 3.1415926;const int NUM = 10;class Shapepublic:virtual void ShowArea() const = 0;static double sum;class Circle: public Shapeprivate:double radius;public:Circle(double r): radius(r) sum += PI * radius *

10、 radius; void ShowArea() const cout 圆面积: PI * radius * radius endl; ;class Rectangle: public Shapeprivate:double height;double width;public:Rectangle(double h, double w): height(h), width(w) sum += height * width; void ShowArea() const cout 矩形面积: height * width endl; ;class Square: public Shapepriva

11、te:double length;public:Square(double a): length(a) sum += length * length; void ShowArea() const cout 正方形面积: length * length endl; ;double Shape:sum = 0;int main(void)Shape *shapeNUM;int count = 0;while (count NUM)cout select;if (select = 4) break;switch (select)case 1:double r;cout r;shapecount = new Circle(r);shapecount-ShowArea();count+;break;case 2:double h, w;cout h;cout w;shapecount = new Rectangle(h, w);shapecount-S

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

当前位置:首页 > 建筑/环境 > 施工组织

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