欢迎来到天天文库
浏览记录
ID:41986299
大小:140.74 KB
页数:12页
时间:2019-09-05
《2.4运算符重载(第二章C面向对象程序设计)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、34、运算符重载的含义是什么?是否所有的运算符都可以重载?答:运算符重载是对已有的运算符赋予多重含义,同一个运算符作用于不用类型的数据导致不同行为。不是。在C++语言中除了(点运算符(.)、作用域运算符(::)、条件运算符(?:)、成员指针运算符(*)、编译预处理命令的开始符号(#))五个运算符不能被重载外,其他运算符均可重载。35、运算符重载有哪两种形式?这两种形式有何区别?答:运算符重载有成员函数和友元函数两种形式。区别:运算符作为成员函数重载时的参数比作为友元函数重载时少一个。例如使用成员函数重载双目运算符时只用一个
2、参数,而使用友元函数重载时要用两个参数。36、转换函数的作用是什么?答:1、支持混合类型表达式2、转换减少所需操作符的数目37、分析下列程序的结果。#include〈iostreani・h>classCMatrix{public:cMatrix(intr,intc){row=r;col=c;element=newdouble[row^col];}double&operator()(intx,inty){returnelement[c()l*(xT)+yT];}double&opertitorO(intx,inty)cons
3、t{returnelement[col*(xT)+yT];}^CMatrix(){delete[]element;}private:double*element;introw,col;};voidmain()CMatrixm(5,8);for(inti二;i<5;i++)m(i,l)=i+5;for(i=0;i<5;i++)cout<4、包括测试各种运算符的程序部分)。捉示:两复数相乘的计•算公式为:(a+bi)*(c+di)=(ac-bd)+(ab+bc)T,而两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i<>#ineludeusingnamespacestd;classcomplex{public:comp1ex(inti二0,intj=0):real(i),image(j){}complex(constcomplex&a):real(a.real),im5、age(a・image){}complex&operatx)r=(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«"(”«real«<6、&);friendcomplexoperator-(consIcomplex&,constcomplex&);friendcomplexoperator*(consIcomplex&,constcomplex&);friendcomplexoperator/(constcomplex&,constcomplex&);private:intreal;intimage;};complexoperator+(constcomplexconstcomplex&b){complexr(a):r.real+=b.real;rimag7、e+二b・image;returnr;complexoperator-(constcomplexconstcomplex&b)complexr;r.real=r.real一b・real:r.image=r.image一b.image;complexoperator*(constcomplex&cl,constcomplex&c2)complext;t.real=cl.real*c2・real一cl.image*c2・image;t.image=cl・image*c2・real+cl・real*c2・image;return8、t;complexoperator/(constcomplex&cl,constcomplex&c2)complext;t.real=(cl・real*c2・real+cl.image*c2・image)(c2・real*c2・real+c2・image*c2.image);t.image=(c2・im
4、包括测试各种运算符的程序部分)。捉示:两复数相乘的计•算公式为:(a+bi)*(c+di)=(ac-bd)+(ab+bc)T,而两复数相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i<>#ineludeusingnamespacestd;classcomplex{public:comp1ex(inti二0,intj=0):real(i),image(j){}complex(constcomplex&a):real(a.real),im
5、age(a・image){}complex&operatx)r=(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«"(”«real«<6、&);friendcomplexoperator-(consIcomplex&,constcomplex&);friendcomplexoperator*(consIcomplex&,constcomplex&);friendcomplexoperator/(constcomplex&,constcomplex&);private:intreal;intimage;};complexoperator+(constcomplexconstcomplex&b){complexr(a):r.real+=b.real;rimag7、e+二b・image;returnr;complexoperator-(constcomplexconstcomplex&b)complexr;r.real=r.real一b・real:r.image=r.image一b.image;complexoperator*(constcomplex&cl,constcomplex&c2)complext;t.real=cl.real*c2・real一cl.image*c2・image;t.image=cl・image*c2・real+cl・real*c2・image;return8、t;complexoperator/(constcomplex&cl,constcomplex&c2)complext;t.real=(cl・real*c2・real+cl.image*c2・image)(c2・real*c2・real+c2・image*c2.image);t.image=(c2・im
6、&);friendcomplexoperator-(consIcomplex&,constcomplex&);friendcomplexoperator*(consIcomplex&,constcomplex&);friendcomplexoperator/(constcomplex&,constcomplex&);private:intreal;intimage;};complexoperator+(constcomplexconstcomplex&b){complexr(a):r.real+=b.real;rimag
7、e+二b・image;returnr;complexoperator-(constcomplexconstcomplex&b)complexr;r.real=r.real一b・real:r.image=r.image一b.image;complexoperator*(constcomplex&cl,constcomplex&c2)complext;t.real=cl.real*c2・real一cl.image*c2・image;t.image=cl・image*c2・real+cl・real*c2・image;return
8、t;complexoperator/(constcomplex&cl,constcomplex&c2)complext;t.real=(cl・real*c2・real+cl.image*c2・image)(c2・real*c2・real+c2・image*c2.image);t.image=(c2・im
此文档下载收益归作者所有