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

上传人:最**** 文档编号:114693342 上传时间:2019-11-12 格式:DOC 页数:15 大小:82KB
返回 下载 相关 举报
河北工业大学-C++实验报告实验四讲解_第1页
第1页 / 共15页
河北工业大学-C++实验报告实验四讲解_第2页
第2页 / 共15页
河北工业大学-C++实验报告实验四讲解_第3页
第3页 / 共15页
河北工业大学-C++实验报告实验四讲解_第4页
第4页 / 共15页
河北工业大学-C++实验报告实验四讲解_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《河北工业大学-C++实验报告实验四讲解》由会员分享,可在线阅读,更多相关《河北工业大学-C++实验报告实验四讲解(15页珍藏版)》请在金锄头文库上搜索。

1、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)编写主程序,

2、计算出复数对象c1和c2相加结果,并将其结果输出。#includeusing namespace std;class Complex public: Complex(double r=0.0,double i=0.0); 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)C

3、omplex temp;temp.real=a.real+b.real;temp.imag=a.imag+b.imag;return temp;void Complex:printf()cout0)cout+;if(imag!=0)coutimagiendl;void main()Complex c1(2.5,3.7),c2(4.2,6.5),c3;c3=c1+c2;c3.printf();2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在059分,秒钟范围限制在059秒。提示:

4、时间类Time的参考框架如下:class Time public: Time(int h=0,int m=0,int s=0);/构造函数 Time operator+(Time &);/运算符重载函数,实现两个时间的相加 void disptime();/显示时间函数 private: int hours,minutes,seconds;#includeusing namespace std;class Timepublic:Time(int h=0,int m=0,int s=0);/构造函数 Time operator+(Time &);/运算符重载函数,实现两个时间的相加 void d

5、isptime();/显示时间函数 private:int hours;intminutes;int seconds;Time:Time(int h,int m,int s)hours=h;minutes=m;seconds=s;Time Time:operator+(Time& t)int h,m,s;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;minu

6、tes=m;seconds=s;return *this;void Time:disptime()couthours:minutes:seconds.endl;void Input(int &h,int &m,int &s)couth ;cinm ;cins ;while(m59|s59) cout*时间输入错误!请重新输 !*n;couth ;cinm ;cins ;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();r

7、eturn 0;3、用友元运算符函数或成员运算符函数,重载运算符“+”、“-”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。#include#define hang 2#define lie 2class Matrixprivate:int Row;int Column;int MATRIXhanglie;public:Matrix(int r,int c)Row=r;Column=c;Matrix() void TypeMatrix();void Print() const;Matrix& operator = (const Matrix& rhs);Matrix operato

8、r + (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=0;qhang;q+)for(int s=0;slie;s+)std:coutMATRIXqst;if(s=lie-1)std:coutstd:endl;Matrix& Matr

9、ix:operator = (const Matrix& rhs)if(this!=&rhs)for(int g=0;ghang;g+)for(int h=0;hMATRIXgh=rhs.MATRIXgh;return *this;Matrix Matrix:operator + (const Matrix& rhs)int i,j;for(i=0;ihang;i+)for(j=0;jMATRIXij+rhs.MATRIXij;return *this ;Matrix Matrix:operator - (const Matrix& rhs)int i,j;for(i=0;ihang;i+)f

10、or(j=0;jMATRIXij-rhs.MATRIXij;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类的框架,用于完成集

11、合基本运算所需的各项功能。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);/成员运

12、算符重载函数,实现集合的赋值运算 protected: int len;/统计结合中元素的个数; int sMAX;/存放集合中的元素 ;#includeusing 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 sMAX;set:set()len = 0;/s =0;cout *建立一个集合*n;void set:input(int

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

当前位置:首页 > 高等教育 > 大学课件

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