河北工业大学-C++实验报告实验四

上传人:daj****de2 文档编号:57466727 上传时间:2022-02-24 格式:DOC 页数:16 大小:99KB
收藏 版权申诉 举报 下载
河北工业大学-C++实验报告实验四_第1页
第1页 / 共16页
河北工业大学-C++实验报告实验四_第2页
第2页 / 共16页
河北工业大学-C++实验报告实验四_第3页
第3页 / 共16页
资源描述:

《河北工业大学-C++实验报告实验四》由会员分享,可在线阅读,更多相关《河北工业大学-C++实验报告实验四(16页珍藏版)》请在装配图网上搜索。

1、1、编写一个程序,要求:(1)生明一个类Complex(复数类),定义类Complex的两个对象cl和c2.对象 c1 通过构造函数直接指定复数的实部和虚部(类私有数据成员为 double 类型:real和imag)为及,对象c2通过构造函数直接指定复数的实部和虚部为 及;(2)定义友元运算符重载函数,它以cl、c2对象为参数,调用该函数时能 返回两个复数对象相加操作;( 3)定义成员函数 print ,调用该函数时,以格式“ real+imag i ”输出当 前对象的实部和虚部, 例如: 对象的实部和虚部分别是和, 则调用 print 函数输 出格式为: + i ;( 4)编写主程序,计算出

2、复数对象 c1 和 c2 相加结果,并将其结果输出。#includeusing namespace std;class Complexpublic:Complex(double r=,double i=;friend Complex operator+ (Complex& a,Complex& b);void printf();private:double real;double imag;Complex:Complex(double r,double i)real=r;imag=i;Complex operator+ (Complex& a,Complex& b)Complex temp;=

3、+;=+;return temp;void Complex:printf()cout0)cout+;if(imag!=O)coutimagie ndl;void mai n()Complex c1,c2,c3;c3=c1+c2;();* CAUSESAH Pvc5ada d a5Debugsc.7+10.21fgss 孔ny k&y to cantinue2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“ +”实现两个时间的相加。要求将小时范围限制在 大于等于0,分钟范围限制在059分,秒钟范围限制在059秒。提示:时间类Time的参考框架如下:c

4、lass Timepublic:Time(i nt h=0,i nt m=0,i nt s=0);e ndl;void In put(i nt & h,i nt & m,i nt &s)cout输入时间:;cinh ;cinm ;cins ;while(m59|s59)cout*时间输入错误!请重新输 !*n;cout输入时间:;cinh ;cinm ;cins ;int mai n()int h1,m1,s1,h2,m2,s2;In put(h1,m1,s1);In put(h2,m2,s2);Time A(h1,m1,s1),B(h2,m2,s2);A=A+B;();return 0;5

5、E R H P vc s.3 da sd asDebu c输入时间江2332術入时间=712438:36:1&.Press adv key to continue3、用友元运算符函数或成员运算符函数,重载运算符“+”、“- ”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。#in clude#defi ne hang 2#defi ne lie 2class Matrixprivate:int Row;int Colu mn;int MATRIXha nglie;public:Matrix(i nt r,int c)Row=r;Colu mn=c;Matrix() void Type

6、Matrix();void Prin t() con st;Matrix & operator = (const Matrix & rhs);Matrix operator + (const Matrix & rhs);Matrix operator - (const Matrix& rhs);void Matrix:TypeMatrix()std:cout 请输入矩阵 :std:endl;for(int i=0;ihang;i+)for(int j=0;jMATRIXij;void Matrix:Print() conststd:cout 矩阵的结果为 :std:endl;for(int q

7、=0;qhang;q+)for(int s=0;slie;s+)std:coutMATRIXqst; if(s=lie-1)std:coutstd:endl;Matrix& Matrix:operator = (const Matrix& rhs)if(this!=&rhs)for(int g=0;ghang;g+)for(int h=0;hMATRIXgh=gh;return *this;Matrix Matrix:operator + (const Matrix& rhs) int i,j;for(i=0;ihang;i+)for(j=0;jMATRIXij+ij;return *this

8、 ;Matrix Matrix:operator - (const Matrix& rhs)int i,j;for(i=0;ihang;i+)for(j=0;jMATRIXijij;return *this ;int main()Matrix a,b,c,d;();();c=a+b;();d=a-b;();请输人矩阵?1323请输人矩阵:2L3館晖的结臬为:34备阵的结h为:1 32 3Ppess any Icev to continue4、编写一个程序,用于进行集合的和、并和交运算。例如输入整数集合9,5,4,3,6,7和2,4,6,9,计算出他们进行集合的并、差和交运算后的结果【提示】(1

9、)可以用一下表达式实现整数集合的基本运算:s1+s2两个整数集合的并运算s1-s2两个整数集合的差运算s1*s2两个整数集合的交运算(2)参考以下Set类的框架,用于完成集合基本运算所需的各项功能。class Setpublic:Set();void in put(i nt d);向集合中添加一个元素int len gth();返回集合中的元素个数int getd(int i);返回集合中位置i的元素void display。;/显示集合的所有元素Set operato叶(Set s1);成员运算符重载函数,实现集合的并运算Set operator-(Set s1);成员运算符重载函数,实现集

10、合的差运算Set operator*(Set s1);成员运算符重载函数,实现集合的交运算Set operator=(Set s1);/成员运算符重载函数,实现集合的赋值运protected:int len;/ 统计结合中元素的个数; int sMAX;/ 存放集合中的元素;#includeusing namespace std;const int MAX = 50;class setpublic:set();void input(int d);int length();int getd(int i);void disp();set operator+(set s1);set operator

11、-(set s1);set operator*(set s1);set operator=(set s1);protected:int len;int sMAX;set:set()len = 0;/s =0;cout * 建立一个集合 *n;void set:input(int d)len = d;cout 输入集合元素 d 个:;for(int i = 0; i si ;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; i

12、 len; i+) cout si ;/cout endl;并运算 set set:operator+(set s1) /-/strcat(s,;for (int i = 0; i len; i+) for(int j = 0; j ; j+) /if(si = j) /for (;j ; j+)j = j+1;在 中选出不相同的选出相同的元素删掉得到与 s 不同的元素将 中不相同的加在 s 后面for (int j = 0;j ; j+ ) /slen = j;len+;slen+ = 0;return *this;差运算set set:operator-(set s1) /int t;fo

13、r (int i = 0; i ; i+)for(int j = 0; j len; j+) ifi = sj ) /选出 s 与中相同的元素并且删除掉t = j;for (;t len; t+)st = st+1;-len;return *this;set set:operator*(set s1) / 交运算 int mMAX;int l = 0;for (int i = 0; i ; i+) for(int j = 0; j len; j+) /选出相同的元素ifi = sj)ml = sj;l+; for (i = 0; i l; i+) si = mi; sl = 0; len =

14、l;return *this;set set:operator=(set s1)for (int i = 0; i (); i+) si = i;len = ;return *this;int main()int n;set C;set A;cout n;(n);set B;cout n;(n);cout endl;cout 两集合的差集( A - B )C = A - B;();cout endl;/*cout 两集合的交集( A * B )C = A * B;();cout endl;*/*cout 两集合的并集( A + B )C = A + B;();cout endl;*/retur

15、n 0;说明分别分开运行6、写一个程序,定义抽象类class Container为: ;为: ;为: ;Container :protected:double radius;public:Container(double r);/ 抽象类 Container 的构造函数virtual double surface_area()=0;/ 纯虚函数 surface_areavirtual double volume()=0;/ 纯虚函数 volume;【要求】建立3个继承Con tai ner的派生类:Sphere (球体)、Cyli nder (圆柱体)、Cube (正方体),让每一个派生类都包

16、含虚函数 surface_area() 和 volume() ,分别用 来球体、圆柱体和正方体的表面积和体积。要求写出主程序,应用C+勺多态性, 分别计算边长为的正方体、 半径为的球体, 以及半径为和高为的圆柱体的表面积 和体积。#include#includeusing namespace std;class containerprotected:double radius;public:container(double radius1);virtual double surface_area()=0;virtual double volume()=0;container:container

17、(double radius1)radius=radius1;/ 派生类 cube、 sphere 与 cylinder class cube : public containerpublic: 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

18、 sphere : public container public:sphere(double radius1):container( radius1 ) virtual double surface_area(); virtual double volume();double sphere : surface_area()return 4*radius*radius;double sphere : volume()return *radius*radius*radius;class cylinder : public containerprotected:double high;public

19、:cylinder(double high1,double radius1):container(radius1)high=high1;virtual double surface_area();virtual double volume();double cylinder : surface_area()return 2*radius*radius+2*high*radius;double cylinder : volume()return *radius*radius*high; int main()container *p;cube cc; sphere ss; cylinder cy,;p=&cc;cout 正方体的表面积 :surface_area()endl; cout 正方体的体积 :volume()endl;p=&ss;cout 球体的表面积 :surface_area()endl;cout 球体的体积 :volume()endl;p=&cy;cout 圆柱体的表面积 :surface_area()endl;cout 圆柱体的体积 :volume()endl;return 0;IB26pess x-ey to continuecUSERSXHPvc,50djh-Debugadjhc

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!