实 验 报 告 | |||
实验名称 | 多态性实验 | ||
实验类型 | 综合 | 实验学时 | 2 |
(1)熟悉类的设计、运用继承与派生机制设计派生类,合理设置数据成员和成员函数。 (2)掌握双目运算符、单目运算符的重载方法,对常用算术运算符能在自定义类中通过友元函数、成员函数进行重载,以实现静态多态性。 (3)掌握通过继承、虚函数、基类的指针或引用实现动态多态性的方法。 (4)理解并掌握有纯虚函数的抽象类的作用,在各派生类中重新定义各纯虚函数的方法,以及此时实现的动态多态性。 | |||
二、实验环境(实验设备) 硬件: 微型计算机 软件: Windows 操作系统、Microsoft Visual Studio 2010 | |||
三、实验原理及内容 实验题目1:定义点类Point,有两个double 类型的数据成员x 和y,分别表示横坐标和纵 坐标,要求完成如下内容。 (1)定义坐标默认值为原点(0.0,0.0)的构造函数。 (2)以成员函数形式重载:前置“++”运算符和双目运算符“−”。 (3)用友元函数形式重载:双目运算符“+”(两种版本,详见实验指导部分)、插 入运算符。 (4)先根据main()主函数代码和运行结果,补充类的定义和相关函数的定义,写出完 整程序。 (5)程序正确后,删除main()函数体,根据运行结果,自己重新完成main()函数。 main()主函数代码如下。(中文五号宋体,英文五号Consolas字体,单倍行距) int main() { Point pt1(10.5,20.8),pt2(-5.3,18.4),pt3; cout<<"original pt1,pt2,pt3 are:\n"; cout<<pt1<<pt2<<pt3; pt3=pt1+100.8; cout<<"after pt3=pt1+100.8, pt3 is:"<<pt3; pt3=pt1+pt2; cout<<"after pt3=pt1+pt2, pt3 is:"<<pt3; pt3=++pt1; ++pt2; cout<<"after ++ pt1,pt2,pt3 are:\n"; cout<<pt1<<pt2<<pt3; pt3=pt1-pt2; cout<<"after pt3=pt1-pt2, pt3 is:"<<pt3; return 0 ; } 程序运行结果如下。 original pt1,pt2,pt3 are: (10.5,20.8) (-5.3,18.4) (0,0) after pt3=pt1+100.8, pt3 is:(111.3,121.6) after pt3=pt1+pt2, pt3 is:(5.2,39.2) after ++ pt1,pt2,pt3 are: (11.5,21.8) (-4.3,19.4) (11.5,21.8) after pt3=pt1-pt2, pt3 is:(15.8,2.4) 实验解答:
Point(double a=0.0,double b=0.0) { x=a;y=b; }
Point operator ++(); Point operator -(Point &a); Point Point::operator ++() { ++x; ++y; return *this; } Point Point::operator - (Point &a) { Point temp; temp.x=x-a.x; temp.y=y-a.y; return temp; }
friend Point operator +(Point &a,Point &b); friend Point operator +(Point &a,double b); Point operator +(Point &a,Point &b) { Point c; c.x=a.x+b.x; c.y=a.y+b.y; return c; } Point operator +(Point &a,double b) { Point c; c.x=a.x+b; c.y=a.y+b; return c; }
friend ostream & operator <<(ostream &out,const Point &com) { out<<"("<<com.x<<","<<com.y<<")"<<endl; return out; }
int main() { Point pt1(10.5,20.8),pt2(-5.3,18.4),pt3; cout<<"original pt1,pt2,pt3 are:\n"; cout<<pt1<<pt2<<pt3; pt3=pt1+100.8; cout<<"after pt3=pt1+100.8, pt3 is:"<<pt3; pt3=pt1+pt2; cout<<"after pt3=pt1+pt2, pt3 is:"<<pt3; pt3=++pt1; ++pt2; cout<<"after ++ pt1,pt2,pt3 are:\n"; cout<<pt1<<pt2<<pt3; pt3=pt1-pt2; cout<<"after pt3=pt1-pt2, pt3 is:"<<pt3; return 0 ; } 实验题目2: 定义一个抽象类容器类,其中定义了若干纯虚函数,实现求表面积、体积、输出等功能。由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。 ① 在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用相同的函数能执行不同的函数代码,从而实现动态多态性。 ② 定义一个顶层函数void TopPrint(Container &r);使得主函数中调用该函数时,根据实在参数所有的类自动调用对应类的输出函数。 ③ 主函数中定义一个Container类对象,观察编译时的错误信息,从而得出什么结论? 实验解答: (1)基类Container的定义见实验教材。 (2)各个派生类的定义,根据提示进行填写完整代码: ① //正方体类,从Container类公有继承,定义构造函数,重新定义基类的3个纯虚函数 class Cube:public Container { public: Cube(double a):Container(a) {} double area() { return 6*radius*radius; } double volume() { return radius*radius; } void print() { cout<<"the radius is:"<<radius<<endl; cout<<"the area of Cube is:"<<area()<<endl; cout<<"the volume of Cube is:"<<volume()<<endl; } }; ② //球类,从Container类公有继承,定义构造函数,重新定义基类的3个纯虚函数
class Sphere:public Container { public: Sphere(double a):Container(a) {} double area() { return 4*PI*radius*radius; } double volume() { return 4.0/3*PI*radius*radius; } void print() { cout<<"the radius is:"<<radius<<endl; cout<<"the area of Sphere is:"<<area()<<endl; cout<<"the volume of Sphere is:"<<volume()<<endl; } }; ③ //圆柱体类,从Container类公有继承,需要增加的成员变量,定义构造函数,重新定义基类的三个纯虚函数 class Cylinder:public Container { private: double h; public: Cylinder(double a,double b):Container(a),h(b) {} double area() { return 2*PI*radius*radius+2*PI*radius*h; } double volume() { return h*PI*radius*radius; } void print() { cout<<"the radius is:"<<radius<<","<<"the hight is:"<<h<<endl; cout<<"the area of Cylinder is:"<<area()<<endl; cout<<"the volume of Cylinder is:"<<volume()<<endl; } }; (3)正确定义各派生类对象,记录程序的运行结果是: 正方体对象:_the radius is:1 the area of Cube is:6 the volume of Cube is:1__ 球体对象:____the radius is:1 the area of Sphere is:12.56 the volume of Sphere is:4.18667____ 圆柱体对象:_the radius is:1,the hight is:1 the area of Cylinder is:12.56 the volume of Cylinder is:3.14______ 运行结果: 6 1 the radius is:1 the area of Cube is:6 the volume of Cube is:1 12.56 4.18667 the radius is:1 the area of Sphere is:12.56 the volume of Sphere is:4.18667 12.56 3.14 the radius is:1,the hight is:1 the area of Cylinder is:12.56 the volume of Cylinder is:3.14 (4)主函数中定义一个Container类对象,编译器的报错信息: [Error] cannot declare variable 'd' to be of abstract type 'Container' [Note] because the following virtual functions are pure within 'Container': 试说明原因: 纯虚数函数仅仅为多态机制提供一个界面而没有任何实体定义 四、实验小结(包括问题和解决方法、心得体会、意见与建议等) (中文五号宋体,英文五号Consolas字体,单倍行距) (一)实验中遇到的主要问题及解决方法 1.在题目(2)中在主函数中定义Container类的对象,会产生报错信息,解释原因。 纯虚函数没有实体定义 2. 通过题目(2),你觉得纯虚函数与抽象类在编程中有什么价值和意义? 可以减少重新定义函数,增加代码的重复性,为多态机制提供一个界面 3.在题目(1)中通过代码验证,请总结友元函数与成员函数在实现运算符重载时的区别。 函数声明时有部分不同 调用方式不同 形参数量不同 4.其它问题及解决方法: (二)实验心得 学了只是就要及时练习 (三)意见与建议(没有可省略) 多敲代码 |
南京邮电大学C++实验报告3
于 2024-05-28 09:33:14 首次发布