c试验多态性试验报告

上传人:cl****1 文档编号:500439977 上传时间:2023-08-17 格式:DOC 页数:9 大小:79.50KB
返回 下载 相关 举报
c试验多态性试验报告_第1页
第1页 / 共9页
c试验多态性试验报告_第2页
第2页 / 共9页
c试验多态性试验报告_第3页
第3页 / 共9页
c试验多态性试验报告_第4页
第4页 / 共9页
c试验多态性试验报告_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《c试验多态性试验报告》由会员分享,可在线阅读,更多相关《c试验多态性试验报告(9页珍藏版)》请在金锄头文库上搜索。

1、贵州大学实验报告学院:电子信息学院专业:通信工程班级:姓名学号实验组5实验时间指导教师成绩实验项目名称多态性实 验 目 的通过让学生进行实验,使其对于动态多态性有一个较为深入的了解和熟悉。最终可以熟练使用。实 验 要 求1. 编与4个重载函数Double (x),返回值为输入参数的两倍;参数类型分别为int、 long、float、double,返回值类型与参数类型一样。2. 请编与一个抽象类 Shape,在此基础上派生出类Rectangle和Circle,一者都有计算对象面积的函数GetArea ()和计算周长函数GetPerim ()。3. 对类Point重载+ (自增)、-(自减)运算符

2、。实 验 原 理Visual C+勺编译环境下,独立完成实验要求的内容,独立完成编写、编译以及运行 的过程实 验 仪 器安装了 Visual C+的PC机器实 验 步 骤按照实验要求的内容逐一完成实验的要求。顺序是编写、编译、运行。实 验 内 容1. 编与4个重载函数Double (x),返回值为输入参数的两倍;参数类型分别为int、long、float、double,返回值类型与参数类型一样。2. 请编与一个抽象类 Shape,在此基础上派生出类Rectangle和Circle ,一者都有计算对象面积的函数 GetArea ()和计算周长函数 GetPerim ()。3. 对类Point重载

3、+ (自增)、-(自减)运算符。实 验 数 据1、代码如下:#in cludeusing n amespace std; int Double(i nt x); long Double(lo ng x); float Double(float x); double Double(double x); int mai n() int myl nt = 6500; coutDouble(m yin t)e ndl; long myLong = 65000; coutDouble(myL on g)e ndl; float myFloat =;coutDouble(myFloat)e ndl; do

4、uble myDouble =;coutDouble(myDouble)e ndl; int Double(i nt x) return 2*x;long Double(long x) return 2*x;float Double(float x) return 2*x; double Double(double x) return 2*x; 运行结果:2、代码:#in clude#defi ne PI ;using n amespace std;class Shape /抽象类的定义public:virtual double GetArea() = 0; /virtual double G

5、etPerim() = 0; /;class Recta ngle : public Shape /纯虚函数纯虚函数矩形类,公有继承public: Recta ngle(double aa, double bb) /带参数的构造函数a=aa;b=bb;cout长a宽bendl;virtual double GetArea()return a * b;virtual double GetPerim()return 2*( a + b );private:double a;double b;;class Circle : public Shape /圆类,公有继承public: Circle(do

6、uble rr) /带参数的 构造函数r=rr;cout 半径re ndl;virtual double GetArea()return r * r * PI;virtual double GetPerim()return 2 * r * PI;private:double r;void mai n()double len gth, width;cout len gth width;Recta ngle rect(le ngth, width);cout 面积是: () en dl周长是:()e ndl;double rr;cout rr;Circle cir(rr);cout 面积是:()

7、en dl周长是:()e ndl;运行结果:HC:XPrcgfam F le ,liero4oft V CLai udiMyPreicti1 s .Dbdq23.exe78_5398d continue31-4159 y key t3、代码如下:#in cludeclass Pointpublic:Poin t(i nt xx,i nt yy):x(xx),y(yy) void display()c on st;Point &operator+();Point operator+(i nt);Point &operator-();Point operator-(i nt);private:in

8、t x,y;;void Poin t:display()c onstcout当前 Point(x,y)endl;Point &Poin t:operator+()x+;y+;cout执行 x+,y+ 操作!endl;return *this;Point Poin t:operator+(i nt)cout执行 +x,+y 操作!endl;retur n Poin t(+x,+y);Point & Poi nt:operator-()x-;y-;cout执行 x-,y- 操作!endl;return *this;Point Poin t:operator-(i nt)cout执行-x,-y 操作

9、!endl; return Poi nt(-x,-y);int mai n()int x,y;cout xy;Point poi nt1(x,y);();poi nt1+;();+poi nt1;();poi nt1-;();-poi nt1;();return 0;运行结果:C:Pragrarn FilesVlicrosott Visual EtuiU o WyPrajects.,5a.Debug a.ex?njpu.t x(ty:4 目前Po int(4,5 艮址呗f操作甲 芳理Point (5垄? 无企操作色 当前 PointK厂号操作学int f操作to cont nue当刖 Point Press any v实 验 总 结学习使用虚函数实现动态多态性。而虚函数就是在基类中被关键字virtual说明,并在派生类中重新定义的函数,且在派生类中重工业新定义时,函数原型,包括返回 类型、函数名、参数个数与参数类型的顺序,都必须与基类中的完全相冋。此外,构 造函数不能是虚函数,但析构函数可以是虚函数。函数的重载方法有一参数个数相冋,但是类型不冋;二参数个数不冋;三coust(常量)。指导教 师意见签名:年月日注:各学院可根据教学需要对以上栏木进行增减。表格内容可根据内容扩充。

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

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

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