虚函数与多态性实验(共10页).doc
精选优质文档-倾情为你奉上一实验目的及要求1进一步熟悉类的设计、运用继承与派生机制设计派生类,合理设置数据成员和成员函数。2掌握通过继承、虚函数、基类的指针或引用实现动态多态性的方法。3理解并掌握具有纯虚函数的抽象类的作用,在各派生类中重新定义各纯虚函数的方法,以及此时实现的动态多态性。二实验内容在自己的文件夹下建立一个名为exp5的工程,在该工程中做如下操作:定义一个抽象类容器类,其中定义了若干纯虚函数,实现求表面积、体积、输出等功能。由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。(1)在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用相同的函数能执行不同的函数代码,从而实现动态多态性。(2)定义一个顶层函数void TopPrint(Container &r);使得主函数中调用该函数时,根据实在参数所有的类自动调用对应类的输出函数。(3)主函数中定义一个Container类对象,观察编译时的错误信息,从而得出什么结论三实验程序及运行结果#include <iostream>using namespace std;class Basepublic:virtual void f() cout << "调用Base:f()" << endl; ;class Derived: public Basepublic:void f() cout << "调用Derived:f()" << endl; / 虚函数;int main(void)Derived obj;/ 定义派生类对象Base *p = &obj;/ 基类指针p->f();/ 调用函数f()system("PAUSE"); return 0; 2.#include <iostream>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();/ 调用函数Show()int main(void)Base obj1;Derived obj2;/ 定义对象Refers(obj1);/ 调用函数Refers()Refers(obj2);system("PAUSE"); return 0; 3.#include <iostream>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:Derived(int a, int b): Base(a), n(a) / 构造函数void Show() const cout << n << ","/Base:Show();/ 调用基类的Show();int main(void)Base obj1(168);Derived obj2(158, 98);Base *p;p = &obj1;p->Show();p = &obj2;p->Show();p->Base:Show();system("PAUSE"); return 0; 4.#include <iostream>using namespace std;class Apublic:virtual void Show() const cout << "基类A" << endl; ;class B: public Apublic:void Show() const cout << "派生类B" << endl; ;int main(void)B obj;A *p = &obj;p->Show();system("PAUSE");return 0;5.#include <iostream>using namespace std;const double PI = 3.;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 * 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(void)char flag = 'Y''Shape *p;while (toupper(flag) = 'Y')cout << "请选择输入类别(1.圆形 2.矩形)"int select;cin >> select;switch (select)case 1:double r;cout << "输入半径:"cin >> r;p = new Circle(r);p->Show();delete p;break;case 2:double h, w;cout << "输入高:"cin >> h;cout << "输入宽:"cin >> w;p = new Rectangle(h, w);p->Show();/ 显示相关信息delete p;/ 释放存储空间break;default:/ 其它情况, 表示选择有误cout << "选择有误!"<< endl;break;cout << endl << "是否继续录入信息?(Y/N)"cin >> flag;cout << "总面积:" << Shape:sum << endl;system("PAUSE"); return 0; 6.#include <iostream>using namespace std;const double PI = 3.;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 * 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 Shapeprivate: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 << "请选择(1.圆形 2.矩形 3.正方形 4.退出):"int select;cin >> select;if (select = 4) break;switch (select)case 1:double r;cout << "输入半径:"cin >> r;shapecount = new Circle(r);shapecount->ShowArea();count+;break;case 2:double h, w;cout << "输入高:"cin >> h;cout << "输入宽:"cin >> w;shapecount = new Rectangle(h, w);shapecount->ShowArea();count+;break;case 3:double a;cout << "输入边长:"cin >> a;shapecount = new Square(a);shapecount->ShowArea();count+;break;default:cout << "选择有误!"<< endl;break;cout << "总面积:" << Shape:sum << endl;for (int i = 0; i < count; i+) delete shapei;system("PAUSE"); return 0; 五 实验总结通过本次试验我更深刻的理解了某些语句如何使用及结构体的优点也能更加熟练的编写简单的程序了我深知实践要比书本更加重要今后还要多练习在实践中学习。专心-专注-专业