您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 2.4-运算符重载-(第二章-C++面向对象程序设计)
34、运算符重载的含义是什么?是否所有的运算符都可以重载?答:运算符重载是对已有的运算符赋予多重含义,同一个运算符作用于不用类型的数据导致不同行为。不是。在C++语言中除了(点运算符(.)、作用域运算符(::)、条件运算符(?:)、成员指针运算符(*)、编译预处理命令的开始符号(#))五个运算符不能被重载外,其他运算符均可重载。35、运算符重载有哪两种形式?这两种形式有何区别?答:运算符重载有成员函数和友元函数两种形式。区别:运算符作为成员函数重载时的参数比作为友元函数重载时少一个。例如使用成员函数重载双目运算符时只用一个参数,而使用友元函数重载时要用两个参数。36、转换函数的作用是什么?答:1、支持混合类型表达式2、转换减少所需操作符的数目37、分析下列程序的结果。#includeiostream.hclassCMatrix{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{returnelement[col*(x-1)+y-1];}~CMatrix(){delete[]element;}private:double*element;introw,col;};voidmain(){CMatrixm(5,8);for(inti=;i5;i++)m(i,1)=i+5;for(i=0;i5;i++)coutm(i,1)”,”;coutendl;}运行结果:5,6,7,8,9,38、定义一个复数类,通过重载运算符:=、+=、+、-、*、/,直接实现两个复数之间的各种运算。编写一个完整的程序(包括测试各种运算符的程序部分)。提示:两复数相乘的计算公式为:(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。#includeiostreamusingnamespacestd;classcomplex{public:complex(inti=0,intj=0):real(i),image(j){}complex(constcomplex&a):real(a.real),image(a.image){}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(real,image)endl;}friendcomplexoperator+(constcomplex&,constcomplex&);friendcomplexoperator-(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.image;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;returnt;}complexoperator/(constcomplex&c1,constcomplex&c2){complext;t.real=(c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.real+c2.image*c2.image);t.image=(c2.image*c2.real-c1.real*c2.image)/(c2.real*c2.real+c2.image*c2.image);returnt;}intmain(){complexa(32,6);complexb(23,63);complexc;c=a+b;c.display();a+=b;a.display();c=a-b;c.display();c=a*b;c.display();c=a/b;c.display();return0;}39、定义一个学生类,数据成员包括:姓名、学号、C++、英语和数学成绩。重载运算符“”和“”,实现学生对象的直接输入输出。增加转换函数,实现姓名的转换。设计一个完整的程序,验证成员函数和重载运算符的正确性。#includeiostreamusingnamespacestd;classstudent{public:student(char*n=):number(201),cpp(0.0),eng(0.0),math(0.0){}voidset(char*nm,char*num,float_cpp,floatma,float_eng){name=nm;mumber=num;cpp=_cpp;math=ma;eng=_eng;}friendostream&operator(ostream&out,conststudent&a);friendistream&operator(ostream&in,student&b);private:charname[20];charnumber[20];floatcpp;floatmath;floateng;};ostream&operator(ostream&out,conststudent&a){outa.namea.numbera.cppa.matha.eng;returnout;}istream&operator(ostream&in,student&b){ina.namea.numbera.cppa.matha.eng;}intmain(){studenta;a.set(linda,201,34,64,64);couta;studentb;cinb;coutb;}40、定义一个平面直角坐标系上的一个点的类CPoint,重载“++”和“--”运算符,并区分这两种运算符的前置和后置运算,构造一个完整的程序。#includeiostreamusingnamespacestd;classCPoint{public:CPoint(inti=0,intj=0):x(i),y(j){}CPoint&operator++();//前置CPointoperator++(int);//后置CPoint&operator--();//前置CPointoperator--(int);//后置voiddisplay();private:intx;inty;};CPoint&CPoint::operator++(){++x;++y;return*this;}CPoint&CPoint::operator--(){--x;--y;return*this;}CPointCPoint::operator++(int){CPointret(*this);++x;++y;returnret;}CPointCPoint::operator--(int){CPointret(*this);--x;--y;returnret;}voidCPoint::display(){coutxyendl;}intmain(){CPointdot1(2,5);++dot1;dot1.display();CPointdot2(1,2);--dot2;dot2.display();CPointdot3(2,2);dot3++;dot3.display();return0;}41、在上题的基础上,为其定义友元函数实现重载运算符“+”。#includeiostreamusingnamespacestd;classCPoint{public:CPoint(inti=0,intj=0):x(i),y(j){}CPoint&operator++();//前置CPointoperator++(int);//后置CPoint&operator--();//前置CPointoperator--(int);//后置voiddisplay();friendCPointoperator+(constCPoint&a,constCPoint&b);private:intx;inty;};CPoint&CPoint::operator++(){++x;++y;return*this;}CPoint&CPoint::operator--(){--x;--y;return*this;}CPointCPoint::operator++(int){CPointret(*this);++x;++y;returnret;}CPointCPoint::operator--(int){CPointret(*this);--x;--y;returnret;}voidCPoint::display(){coutxyendl;}CPointoperator+(constCPoint&a,constCPoint&b){CPointt(a);t.x+=b.x;t.y+=b.y;returnt;}intmain(){CPointdot1(2,5);++dot1;dot1.display();CPointdot2(1,2);--dot2;dot2.display();CPointdot3(2,2);dot3++;dot3.display();CPointdot4;dot4=dot1+dot2;dot4.display();return0;}
本文标题:2.4-运算符重载-(第二章-C++面向对象程序设计)
链接地址:https://www.777doc.com/doc-5935485 .html