厦里c++实验4

上传人:飞*** 文档编号:47750491 上传时间:2018-07-04 格式:PDF 页数:11 大小:144.75KB
返回 下载 相关 举报
厦里c++实验4_第1页
第1页 / 共11页
厦里c++实验4_第2页
第2页 / 共11页
厦里c++实验4_第3页
第3页 / 共11页
厦里c++实验4_第4页
第4页 / 共11页
厦里c++实验4_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《厦里c++实验4》由会员分享,可在线阅读,更多相关《厦里c++实验4(11页珍藏版)》请在金锄头文库上搜索。

1、C+ 面向对象程序设计实验报告实验序号: 4 实验项目名称:多态性学号姓名Debug 专业实验地点指导教师实验时间一、实验目的及要求(1)了解多态性的概念;(2)掌握运算符重载的基本方法;(3)掌握虚函数的定义和使用方法;(4)掌握纯虚函数和抽象类的概念和用法二、实验设备(环境)及要求Micorsoft Visual C+ 6.0 三、实验内容与步骤(题目、算法和结果描述)1、分析并调试下列程序,写出程序的输出结果,并解释输出结果。#include using namespace std; class B public: virtual void f1(double x) coutf1(1.2

2、3); pd-f1(1.23); pb-f2(1.23); pb-f3(1.23); pd-f3(3.14); return 0; 【运行结果截图】 :【运行结果分析】 :本题是虚函数的运用,在派生类D 中定义与基类B 一样的成员函数f1 为虚函数,并通过指针或引用来访问基类和派生类中的同名函数。2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。【要求】: 将小时范围限制在大于等于0,分钟范围限制在059 分,秒钟范围限制在059秒。【提示】:时间类 Time 的参考框架如下:class Time public: Time(

3、int h=0,int m=0,int s=0); Time operator +(Time void disptime(string); private: int hourse; int minuters; int seconds; ; 【运行结果截图】 :3、给出下面的抽象基类container。class container protected: double radius; public: container(double radius); virtual double surface_area()=0; virtual double volume()=0; ; 建立 3 个继承 co

4、ntainer 的派生类cube、sphere 与 cylinder ,让每一个派生类都包含虚函数surface_area()和 volume() ,分别用来计算正方体、球体和圆柱体的表面积及体积。【要求】:写出主程序,用于C+的多态性,分别计算边长为6.0 的正方体、半径为5.0的球体,以及半径为5.0 和高为 6.0 的圆柱体的表面积和体积。【运行结果截图】 :4、 编写程序, 用于进行集合的并、 差和交运算。 例如输入整数集合9 5 4 3 6 7 和2 4 6 9 ,计算出它们进行集合的并、差和交运算后的结果。1)可以用以下表达式实现整数集合的运算:s1+s2 两个整数集合的并运算s1

5、-s2 两个整数集合的差运算s1*s2 两个整数集合的交运算【 Set类的框架】:class set public: set(); void input(int d); int length(); int getd(int i); void disp(); set operator+(set s1); set operator-(set s1); set operator*(set s1); set operator=(set s1); protected: int len; int sMAX; ; 【运行结果截图】 :四、分析与讨论(记录实验过程中出现的主要问题和心得体会)五、教师评语签名:

6、日期:成绩附:程序源代码1、2、#include #include using namespace std; class Time public: Time(int h=0,int m=0,int s=0); Time operator+(Time void disptime(string); private: int hourse; int minutes; int seconds; ; Time:Time(int h,int m,int s) hourse = h; minutes = m; seconds = s; Time Time:operator+(Time s = (t.seco

7、nds+seconds)%60; m = (minutes + t.minutes + (t.seconds+seconds)/60)%60; h = hourse + t.hourse + (minutes + t.minutes + (t.seconds+seconds)/60)/60; hourse = h; minutes = m; seconds = s; return *this; /返回当前对象的值 void Time:disptime(string str) cout h; cin m; cin s; while ( m 59 | s 59) cout h; cin m; ci

8、n s; int main() int h1,m1,s1,h2,m2,s2; Input(h1,m1,s1); Input(h2,m2,s2); Time A(h1,m1,s1), B(h2,m2,s2); A = A + B; A.disptime(“n 时间相加后的结果为:“); return 0; 3、#include #define pi 3.14 using namespace std; class container protected: double radius; public: container(double r)radius=r; virtual double surfa

9、ce_area()=0; /定义虚函数计算表面积virtual double volume()=0; /定义虚函数计算体积; class cube:public container public: cube(double r):container(r); double surface_area() coutsurface_area(); p-volume(); cylinder t(5.0); p= p-surface_area(); p-volume(); sphere f(5.0,6.0); p= p-surface_area(); p-volume(); return 0; 4 #inc

10、lude using namespace std; const int MAX=100; class set public: set(); void input(int d); int length(); int getd(int i); void disp(); set operator+(set s1); set operator-(set s1); set operator*(set s1); set operator=(set s1); protected: int len; int sMAX; ; set:set() len=0; void set:input(int d) len=

11、d; coutsi; int set:length() int n=0; while(sn!=0) n+; return n; int set:getd(int i) return 0; void set:disp() for (int i=0;in; A.input(n); set D=A; set B; coutn; B.input(n); coutendl; cout“两集合的并集(A+B) 为:“; C=A+B; C.disp(); coutendl; A=D; cout“两集合的差集(A-B) 为:“; C=A-B; C.disp(); coutendl; A=D; cout“两集合的交集(A*B) 为:“; C=A*B; C.disp(); coutendl; return 0; 5、6、

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

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