欢迎来到天天文库
浏览记录
ID:58670798
大小:33.00 KB
页数:10页
时间:2020-10-15
《运算符重载(第二章C面向对象程序设计).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、34、运算符重载的含义是什么?是否所有的运算符都可以重载?答:运算符重载是对已有的运算符赋予多重含义,同一个运算符作用于不用类型的数据导致不同行为。不是。在C++语言中除了(点运算符(.)、作用域运算符(::)、条件运算符(?:)、成员指针运算符(*)、编译预处理命令的开始符号(#))五个运算符不能被重载外,其他运算符均可重载。35、运算符重载有哪两种形式?这两种形式有何区别?答:运算符重载有成员函数和友元函数两种形式。区别:运算符作为成员函数重载时的参数比作为友元函数重载时少一个。例如使用成员函数重载双目运算符时只用一个参数,而
2、使用友元函数重载时要用两个参数。36、转换函数的作用是什么?答:1、支持混合类型表达式2、转换减少所需操作符的数目37、分析下列程序的结果。#includeclassCMatrix{public:cMatrix(intr,intc){row=r;col=c;element=newdouble[row*col];}double&operator()(intx,inty){returnelement[col*(x-1)+y-1];}double&operator()(intx,inty)const{return
3、element[col*(x-1)+y-1];}~CMatrix(){delete[]element;}private:double*element;introw,col;};voidmain(){CMatrixm(5,8);for(inti=;i<5;i++)m(i,1)=i+5;for(i=0;i<5;i++)cout<4、算符的程序部分)。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ab+bc)I,而两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。#includeusingnamespacestd;classcomplex{public:complex(inti=0,intj=0):real(i),image(j){}complex(constcomplex&a):real(a.real),image(a.image){5、}complex&operator=(constcomplex&a){real=a.real;image=a.image;return*this;}complex&operator+=(constcomplex&a){real=real+a.real;image=image+a.image;return*this;}voiddisplay(){cout<<"("<6、endcomplexoperator-(constcomplex&,constcomplex&);friendcomplexoperator*(constcomplex&,constcomplex&);friendcomplexoperator/(constcomplex&,constcomplex&);private:intreal;intimage;};complexoperator+(constcomplex&a,constcomplex&b){complexr(a);r.real+=b.real;r.image+=b.im7、age;returnr;}complexoperator-(constcomplex&a,constcomplex&b){complexr;r.real=r.real-b.real;r.image=r.image-b.image;returnr;}complexoperator*(constcomplex&c1,constcomplex&c2){complext;t.real=c1.real*c2.real-c1.image*c2.image;t.image=c1.image*c2.real+c1.real*c2.image;re8、turnt;}complexoperator/(constcomplex&c1,constcomplex&c2){complext;t.real=(c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.rea
4、算符的程序部分)。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ab+bc)I,而两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。#includeusingnamespacestd;classcomplex{public:complex(inti=0,intj=0):real(i),image(j){}complex(constcomplex&a):real(a.real),image(a.image){
5、}complex&operator=(constcomplex&a){real=a.real;image=a.image;return*this;}complex&operator+=(constcomplex&a){real=real+a.real;image=image+a.image;return*this;}voiddisplay(){cout<<"("<6、endcomplexoperator-(constcomplex&,constcomplex&);friendcomplexoperator*(constcomplex&,constcomplex&);friendcomplexoperator/(constcomplex&,constcomplex&);private:intreal;intimage;};complexoperator+(constcomplex&a,constcomplex&b){complexr(a);r.real+=b.real;r.image+=b.im7、age;returnr;}complexoperator-(constcomplex&a,constcomplex&b){complexr;r.real=r.real-b.real;r.image=r.image-b.image;returnr;}complexoperator*(constcomplex&c1,constcomplex&c2){complext;t.real=c1.real*c2.real-c1.image*c2.image;t.image=c1.image*c2.real+c1.real*c2.image;re8、turnt;}complexoperator/(constcomplex&c1,constcomplex&c2){complext;t.real=(c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.rea
6、endcomplexoperator-(constcomplex&,constcomplex&);friendcomplexoperator*(constcomplex&,constcomplex&);friendcomplexoperator/(constcomplex&,constcomplex&);private:intreal;intimage;};complexoperator+(constcomplex&a,constcomplex&b){complexr(a);r.real+=b.real;r.image+=b.im
7、age;returnr;}complexoperator-(constcomplex&a,constcomplex&b){complexr;r.real=r.real-b.real;r.image=r.image-b.image;returnr;}complexoperator*(constcomplex&c1,constcomplex&c2){complext;t.real=c1.real*c2.real-c1.image*c2.image;t.image=c1.image*c2.real+c1.real*c2.image;re
8、turnt;}complexoperator/(constcomplex&c1,constcomplex&c2){complext;t.real=(c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.rea
此文档下载收益归作者所有