淮海工学院计算机工程学院
实验报告书
课程名: 《 C++程序设计(2) 》
题 目: 多态性与虚函数
班 级: 网络111
评语: 成绩: 指导教师: 批阅时间: 年 月 日
《 C++程序设计(2) 》实验报告 - 1 -
1、实验内容或题目
(1) 分析调试教材p402页例12.2,将基类成员函数改为 虚函数, 理解虚函数的作用; (2) P416习题1(参考P258“类的声明和成员函数定义的分离”介绍)、编程实现; (3) 习题4,编程实现。
2、实验目的与要求
(1) 了解多态性的概念;
(2) 了解虚函数的作用及使用方法; (3) 了解静态关联和动态关联的概念和方法; (4) 了解纯虚函数和抽象类的概念和方法。
3、实验步骤与源程序
⑴ 实验步骤
第一步: 定义Circle类;结构函数;
第二步:定义虚函数;定义Rectangle类;结构函数; 第三步:输出s的面积;
第四步:建立Circle类对象circle;输出circle的面积; 第五步:建立Rectangle类对象rectangle; 第六步:输出rectangle的面积;
第七部:建立Triangle类对象 ;输出triangle的面积. ⑵ 源代码(只写编程题的代码) 1):#include Student (int,string,float); virtual void display(); protected: }; int num; string name; float score; 《 C++程序设计(2) 》实验报告 - 2 - Student::Student(int n,string nam,float s) {num=n;name=nam;score=s;} void Student::display() {cout<<\"num:\"< void Graduate::display() {cout<<\"num:\"< 2): #include Point(float=0,float=0); void setPoint(float,float); float getX() const {return x;} float pay; 《 C++程序设计(2) 》实验报告 - 3 - float getY() const {return y;} friend ostream & operator<<(ostream &,const Point &); protected: float x,y; }; //point.cpp文件 //POINT.CPP Point::Point(float a,float b) {x=a;y=b;} void Point::setPoint(float a,float b) {x=a;y=b;} ostream & operator<<(ostream &output,const Point &p) {output<<\"[\"< class Circle:public Point {public: Circle(float x=0,float y=0,float r=0); void setRadius(float); float getRadius() const; float area () const; friend ostream &operator<<(ostream &,const Circle &); protected: float radius; }; //circle.cpp文件 //CIRCLE.CPP //#include Circle::Circle(float a,float b,float r):Point(a,b),radius(r){} 《 C++程序设计(2) 》实验报告 - 4 - void Circle::setRadius(float r) {radius=r;} float Circle::getRadius() const {return radius;} float Circle::area() const {return 3.14159*radius*radius;} ostream &operator<<(ostream &output,const Circle &c) {output<<\"Center=[\"< class Cylinder:public Circle {public: Cylinder (float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; float area() const; float volume() const; friend ostream& operator<<(ostream&,const Cylinder&); protected: float height; }; //cylinder.cpp文件 //CYLINDER.CPP Cylinder::Cylinder(float a,float b,float r,float h) :Circle(a,b,r),height(h){} 《 C++程序设计(2) 》实验报告 - 5 - void Cylinder::setHeight(float h){height=h;} float Cylinder::getHeight() const {return height;} float Cylinder::area() const { return 2*Circle::area()+2*3.14159*radius*height;} float Cylinder::volume() const {return Circle::area()*height;} ostream &operator<<(ostream &output,const Cylinder& cy) {output<<\"Center=[\"< {Cylinder cy1(3.5,6.4,5.2,10); cout<<\"\\noriginal cylinder:\\nx=\"< 《 C++程序设计(2) 》实验报告 - 6 - using namespace std; class Shape {public: virtual double area() const =0; }; class Circle:public Shape {public: Circle(double r):radius(r){} virtual double area() const {return 3.14159*radius*radius;}; protected: double radius; }; class Rectangle:public Shape {public: Rectangle(double w,double h):width(w),height(h){} virtual double area() const {return width*height;} protected: double width,height; }; class Triangle:public Shape {public: Triangle(double w,double h):width(w),height(h){} virtual double area() const {return 0.5*width*height;} protected: double width,height; }; void printArea(const Shape &s) {cout< 《 C++程序设计(2) 》实验报告 - 7 - cout<<\"area of circle =\"; printArea(circle); Rectangle rectangle(4.5,8.4); cout<<\"area of rectangle =\"; printArea(rectangle); Triangle triangle(4.5,8.4); cout<<\"area of triangle =\"; printArea(triangle); return 0; } 4、测试数据与实验结果(可以抓图粘贴,提交所有题目的结果。) 1 ) : 《 C++程序设计(2) 》实验报告 - 8 - 5、结果分析与实验体会 1:用#include命令时,应当用双撇号把文件名括起来,不要用尖括号; 2:虚函数实现的动态多样性就是:同一类族中不同类的对象,对同一函数的调用做出不同的响应 因篇幅问题不能全部显示,请点此查看更多更全内容