《2022年实验三多态与虚函数 .pdf》由会员分享,可在线阅读,更多相关《2022年实验三多态与虚函数 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C+ 与数据结构实验指导书实验三多态与虚函数一、实验目的1、理解多态的概念;2、理解函数的静态联编和动态联编;3、掌握虚函数的使用方法;4、掌握抽象类的概念及使用方法。二、实验内容虚函数是在类中被声明为virtual的成员函数, 当编译器看到通过指针或引用调用此类函数时,对其执行动态联编,即通过指针(或引用) 指向的类的类型信息来决定该函数是哪个类的。 通常此类指针或引用都声明为基类的,它可以指向基类或派生类的对象。多态指同一个方法根据其所属的不同对象可以有不同的行为。虚函数是 C+中用于实现多态(polymorphism)的机制。 核心理念就是通过基类访问派生类定义的函数1、录入下面程序,并
2、分析结果:#include #include using namespace std; class Base public: Base() coutBase-ctorendl; Base() coutBase-dtorendl; virtual void f(int)coutBase:f(int)endl; virtual void f(double)coutBase:f(double)endl; virtual void g(int i=10)coutBase:g()iendl; ; class Derived : public Base 名师资料总结 - - -精品资料欢迎下载 - - -
3、 - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - public: Derived() coutDerived-ctor endl; Derived()coutDerived-dtorendl; void f(complex) coutDerived:f(complex)endl; void g(int i=20) coutDerived:g()iendl; ; int main() coutsizeof(Base)endl; coutsizeof(Derived)f(1.0); b
4、.g(); d.g(); pb-g(); delete pb; return 0; 2、录入下面程序,分析运行结果:#include using namespace std; class Base public: Base():data(count) coutBase-ctorendl; +count; Base() coutBase-dtorendl; -count; static int count; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - -
5、 - - - - - - int data; ; int Base:count; class Derived : public Base public: Derived():data(count),data1(data) coutDerived-ctorendl; +count; Derived() coutDerived-dtorendl; -count; static int count; int data1; int data; ; int Derived:count=10; int main() coutsizeof(Base)endl; coutsizeof(Derived)endl
6、; Base* pb = new Derived3; coutpb2.dataendl; cout(static_cast(pb)+2)-data1endl; delete pb; coutBase:countendl; coutDerived:countendl; return 0; 3、录入下面程序,分析编译错误信息。#include #include #include using namespace std; class Abstract public: Abstract() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - -
7、 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - cout f(); return 0; 4、基类 shape 类是一个表示形状的抽象类,area( ) 为求图形面积的函数。请从shape类派生三角形类(triangle) 、圆类( circles ) 、并给出具体的求面积函数。#include #include using namespace std; class Shape public: virtual float area( )=0 ; ; class Triangle : public Shape private: float a
8、,b; public: 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - Triangle(float l,float h)a=l;b=h;cout底:l高:hendl; virtual float area( ) return 0.5*a*b; ; class Triangle1 : public Shape private: float x,y,z; public: Triangle1(float a,float b,floa
9、t c)x=a;y=b;z=c; virtual float area( ) return 0.25*sqrt(x+y+z)*(x+y-z)*(x+z-y)*(y+z-x); ; class Circle : public Shape private: float r; public: Circle(float rr)r=rr;cout半径 :rendl; virtual float area( ) return r*r*3.14159; ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
10、- 第 5 页,共 7 页 - - - - - - - - - void main() float l;float h; coutlh; Triangle tri(l,h); cout 三角形面积是tri.area()endl; float a,b,c; coutabc; Triangle1 tri1(a,b,c); cout 三角形面积是tri1.area()endl; float rr; cout rr; Circle cir(rr); cout 圆面积是: cir.area() abstractMethod(); delete pBase; 名师资料总结 - - -精品资料欢迎下载 -
11、- - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - return 0; #include using namespace std; class Base public: virtual void abstractMethod() = 0; ; class Derived:public Base public: virtual void abstractMethod() coutBase:abstractMethod is calledabstractMehtod(); void abstractMehtod() coutDerived:abstractMethod is calledabstractMethod(); delete pBase; return 0; 分析运行结果。三、实验要求1、 写出程序,并调试程序,要给出测试数据和实验结果。2、 整理上机步骤,总结经验和体会。3、 完成实验报告和上交程序。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -
限制150内