已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 编写一个程序 要求 1 生明一个类 Complex 复数类 定义类 Complex 的两个对象 c1 和 c2 对象 c1 通过构造函数直接指定复数的实部和虚部 类私有数据成员为 double 类型 real 和 imag 为 2 5 及 3 7 对象 c2 通过构造函数直接指定复数 的实部和虚部为 4 2 及 6 5 2 定义友元运算符重载函数 它以 c1 c2 对象为参数 调用该函数时 能返回两个复数对象相加操作 3 定义成员函数 print 调用该函数时 以格式 real imag i 输出当前 对象的实部和虚部 例如 对象的实部和虚部分别是 4 2 和 6 5 则调用 print 函数输出格式为 4 2 6 5 i 4 编写主程序 计算出复数对象 c1 和 c2 相加结果 并将其结果输出 include using namespace std class Complex public Complex double r 0 0 double i 0 0 friend Complex operator Complex void printf private double real double imag Complex Complex double r double i real r imag i Complex operator Complex temp real a real b real temp imag a imag b imag return temp void Complex printf cout 0 cout if imag 0 cout imag i endl void main Complex c1 2 5 3 7 c2 4 2 6 5 c3 c3 c1 c2 c3 printf 2 编写一个程序 其中设计一个时间类 Time 用来保存时 分 秒等私 有数据成员 通过重载操作符 实现两个时间的相加 要求将小时范围限制 在大于等于 0 分钟范围限制在 0 59 分 秒钟范围限制在 0 59 秒 提示 时间类 Time 的参考框架如下 class Time public Time int h 0 int m 0 int s 0 构造函数 Time operator Time 运算符重载函数 实现两个时间的相加 void disptime 显示时间函数 private int hours minutes seconds include using namespace std class Time public Time int h 0 int m 0 int s 0 构造函数 Time operator Time 运算符重载函数 实现两个时间的相加 void disptime 显示时间函数 private int hours intminutes int seconds Time Time int h int m int s hours h minutes m seconds s Time Time operator Time s t seconds seconds 60 m minutes t minutes t seconds seconds 60 60 h hours t hours minutes t minutes t seconds seconds 60 60 hours h minutes m seconds s return this void Time disptime cout hours minutes seconds endl void Input int cin m cin s while m59 s59 cout 时间输入错误 请重新输 n cout h cin m cin 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 return 0 3 用友元运算符函数或成员运算符函数 重载运算符 实 现对实验二中实现的矩阵类的对象的加 减 乘法 include define hang 2 define lie 2 class Matrix private int Row int Column int MATRIX hang lie public Matrix int r int c Row r Column c Matrix void TypeMatrix void Print const Matrix Matrix operator const Matrix Matrix operator const Matrix void Matrix TypeMatrix std cout 请输入矩阵 std endl for int i 0 i hang i for int j 0 j MATRIX i j void Matrix Print const std cout 矩阵的结果为 std endl for int q 0 q hang q for int s 0 s lie s std cout MATRIX q s t if s lie 1 std cout std endl Matrixg hang g for int h 0 hMATRIX g h rhs MATRIX g h return this Matrix Matrix operator const Matrix for i 0 i hang i for j 0 jMATRIX i j rhs MATRIX i j return this Matrix Matrix operator const Matrix for i 0 i hang i for j 0 jMATRIX i j rhs MATRIX i j return this int main Matrix a b c d a TypeMatrix b TypeMatrix c a b c Print d a b d Print 4 编写一个程序 用于进行集合的和 并和交运算 例如输入整数集合 9 5 4 3 6 7 和 2 4 6 9 计算出他们进行集合的并 差和交运算后的结果 提示 1 可以用一下表达式实现整数集合的基本运算 s1 s2 两个整数集合的并运算 s1 s2 两个整数集合的差运算 s1 s2 两个整数集合的交运算 2 参考以下 Set 类的框架 用于完成集合基本运算所需的各项功能 class Set public Set void input int d 向集合中添加一个元素 int length 返回集合中的元素个数 int getd int i 返回集合中位置 i 的元素 void display 显示集合的所有元素 Set operator Set s1 成员运算符重载函数 实现集合的并运算 Set operator Set s1 成员运算符重载函数 实现集合的差运算 Set operator Set s1 成员运算符重载函数 实现集合的交运算 Set operator Set s1 成员运算符重载函数 实现集合的赋值运算 protected int len 统计结合中元素的个数 int s MAX 存放集合中的元素 include using namespace std const int MAX 50 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 s MAX set set len 0 s 0 cout 建立一个集合 n void set input int d len d cout 输入集合元素 d 个 for int i 0 i s i int set length int n 0 while s n 0 n return n int set getd int i return 0 void set disp for int i 0 i len i cout s i cout endl set set operator set s1 并运算 strcat s s1 s for int i 0 i len i for int j 0 j s1 len j 在 s1 s 中选出不相同的 if s i s1 s j 选出相同的元素删掉得到 s1 s 与 s 不同的元素 for j s1 len j s1 s j s1 s j 1 s1 len for int j 0 j s1 len j 将 s1 s 中不相同的加在 s 后面 s len s1 s j len s len s1 len 0 return this set set operator set s1 差运算 int t for int i 0 i s1 len i for int j 0 j len j if s1 s i s j 选出 s 与 s1 s 中相同的元素并且删除掉 t j for t len t s t s t 1 len return this set set operator set s1 交运算 int m MAX int l 0 for int i 0 i s1 len i for int j 0 j len j 选出相同的元素 if s1 s i s j m l s j l for i 0 i l i s i m i s l 0 len l return this set set operator set s1 for int i 0 i s1 length i s i s1 s i len s1 len return this int main int n set C set A cout n A input n set B cout n B input n cout endl cout 两集合的差集 A B 为 C A B C disp cout endl cout 两集合的交集 A B 为 C A B C disp cout endl cout 两集合的并集 A B 为 C A B C disp cout endl return 0 说明分别分开运行 6 写一个程序 定义抽象类 Container class Container protected double radius public Container double r 抽象类 Container 的构造函数 virtual double surface area 0 纯虚函数 surface area virtual double volume 0 纯虚函数 volume 要求 建立 3 个继承 Container 的派生类 Sphere 球体 Cylinder 圆柱体 Cube 正方体 让每一个派生类都包含虚函数 surface area 和 volume 分别 用来球体 圆柱体和正方体的表面积和体积 要求写出主程序 应用 C 的多 态性 分别计算边长为 6 0 的正方体 半径为 5 0 的球体 以及半径为 5 0 和高 为 6 0 的圆柱体的表面积和体积 include include using namespace std class container protected double radius public container double radius1 virtual double surface area 0 virtual double volume 0 container container double radius1 radius radius1 派生类 cube sphere 与 cylinder class cube public container public cube double radius1 container radius1 virtual double surface area virtual double volume double cube surface area return 6 radius radius double cube volume return radius radius radius class sphere public container public sphere double radius1 container radius1 virtual double surface area virtual double volume double sphere surface area return 4 3 14 radius radius double
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 物业绿化铺砖合同范本
- 茶叶地摊进货合同范本
- 直播托管服务合同范本
- 绿道工程劳务合同范本
- 进口代理销售合同范本
- 物业管理合同中止协议
- 货物运输赊帐合同范本
- 酒店合作模式合同范本
- 人教鄂教版 (2017)三年级上册5 盐和糖的溶解第二课时教学设计
- 2025年特岗音乐创编题库及答案
- 广东省深圳市某中学2025届高三年级下册3月一模 历史试题(含解析)
- 肄业合作合同协议
- 中国糖尿病肾脏病防治指南(2021年版)
- 2025年广东省深圳中考英语词汇复习易错词(原词版默写版)
- 书记员试题及答案
- 人身意外险市场分析与策略
- 企业风险管理及内部控制制度框架课件
- “双减”背景下初中历史作业设计研究
- 2025年春新人教版数学一年级下册课件 欢乐购物街 第1课时 认识人民币
- 2025年度敬老院养老服务机构服务质量评估合同规范3篇
- 2025年贵州省公路工程集团招聘笔试参考题库含答案解析
评论
0/150
提交评论