c++运算符重载习题

上传人:小** 文档编号:89184387 上传时间:2019-05-20 格式:DOC 页数:14 大小:65.96KB
返回 下载 相关 举报
c++运算符重载习题_第1页
第1页 / 共14页
c++运算符重载习题_第2页
第2页 / 共14页
c++运算符重载习题_第3页
第3页 / 共14页
c++运算符重载习题_第4页
第4页 / 共14页
c++运算符重载习题_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《c++运算符重载习题》由会员分享,可在线阅读,更多相关《c++运算符重载习题(14页珍藏版)》请在金锄头文库上搜索。

1、Task8-1/*1.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和*/#includeusing namespace std;class Complexpublic: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; void display(); double real; double imag;void Complex:display() cout(real,imagi);Complex operator +(Co

2、mplex &c1,Complex &c2) Complex p; p.real=c1.real+c2.real; p.imag=c1.imag+c2.imag; return p;int main() Complex c1(3,5),c2(2,5),c3; c1.display(); cout+; c2.display(); cout=; c3=c1+c2; c3.display();Task8-2/*2.定义一个复数类Complex,重载运算符“+”、”-“、”*”、”/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数,编程,分别求两个复数之和差积商。*/#

3、includeusing namespace std;class Complexpublic:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex &c2);Complex operator-(Complex &c2);Complex operator*(Complex &c2);Complex operator/(Complex &c2);void display();private: double real; double imag;Complex Complex:o

4、perator +(Complex &c2)Complex c;c.real=real+c2.real;c.imag=imag+c2.imag;return c;Complex Complex:operator -(Complex &c2)Complex c;c.real=real-c2.real;c.imag=imag-c2.imag;return c;Complex Complex:operator *(Complex &c2)Complex c;c.real=real*c2.real;c.imag=imag*c2.imag;return c;Complex Complex:operato

5、r /(Complex &c2)Complex c;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return c;void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3;c3=c1+c2;coutc1+c2=;c3.display();c3=c1-c2;cou

6、tc1-c2=;c3.display();c3=c1*c2;coutc1*c2=;c3.display();c3=c1/c2;coutc1/c2=;c3.display();return 0;Task8-3/*3.有两个矩阵a和b,均为n行m列(m、n的值自己给出),求两个矩阵之和、差、积、商, 重载运算符“+”、”-“、”*”、”/”,使之能用于矩阵向加减乘除,如c=a+b、 c=a*b。*/#include #define n 2#define m 3using namespace std;class Matrix /定义Matrix类 public: Matrix(); /默认构造函数

7、friend Matrix operator+(Matrix &,Matrix &); /重载运算符“+” friend Matrix operator-(Matrix &,Matrix &); friend Matrix operator*(Matrix &,Matrix &); friend Matrix operator/(Matrix &,Matrix &); void input(); /输入数据函数 void display(); /输出数据函数 private: int matnm; ;Matrix:Matrix() /定义构造函数for(int i=0;in;i+) for(i

8、nt j=0;jm;j+) matij=0;Matrix operator+(Matrix &a,Matrix &b) /定义重载运算符“+”函数Matrix c; for(int i=0;in;i+) for(int j=0;jm;j+) c.matij=a.matij+b.matij; return c;Matrix operator-(Matrix &a,Matrix &b) /定义重载运算符“+”函数Matrix c; for(int i=0;in;i+) for(int j=0;jm;j+) c.matij=a.matij-b.matij; return c;Matrix opera

9、tor*(Matrix &a,Matrix &b) /定义重载运算符“+”函数Matrix c; for(int i=0;in;i+) for(int j=0;jm;j+) c.matij=a.matij*b.matij; return c;Matrix operator/(Matrix &a,Matrix &b) /定义重载运算符“+”函数Matrix c; for(int i=0;in;i+) for(int j=0;jm;j+) c.matij=a.matij/b.matij; return c;void Matrix:input() /定义输入数据函数coutinput value o

10、f matrix:endl;int i,j;for(i=0;in;i+) for(j=0;jmatij; void Matrix:display() /定义输出数据函数for (int i=0;in;i+) for(int j=0;jm;j+) coutmatij ; coutendl;int main() Matrix a,b,c; a.input(); b.input(); coutendlMatrix a:endl; a.display(); coutendlMatrix b:endl; b.display(); c=a+b; /用重载运算符“+”实现两个矩阵相加 coutendlMat

11、rix c = Matrix a + Matrix b :endl; c.display(); c=a-b; /用重载运算符“+”实现两个矩阵相加 coutendlMatrix c = Matrix a - Matrix b :endl; c.display(); c=a*b; /用重载运算符“+”实现两个矩阵相加 coutendlMatrix c = Matrix a * Matrix b :endl; c.display(); c=a/b; /用重载运算符“+”实现两个矩阵相加 coutendlMatrix c = Matrix a / Matrix b :和, 使之能用于该矩阵的输入和输出。*/#include #define n 2#define m 3using namespace std;class Matrix /定义Matrix类 public: Matrix(); /默认构造函数 friend Matrix operator+(Matrix &,Mat

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

当前位置:首页 > 商业/管理/HR > 管理学资料

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