程序设计教程()课后习题答案第7章 操作符重载
第7章 操作符重载1、 为什么要对操作符进行重载?是否所有的操作符都可以重载?答:通过对C+操作符进行重载,我们可以实现用C+的操作符按照通常的习惯来对某些类(特别是一些数学类)的对象进行操作,从而使得程序更容易理解。除此之外,操作符重载机制也提高了C+语言的灵活性和可扩充性,它使得C+操作符除了能对基本数据类型和构造数据类型进行操作外,也能用它们来对类的对象进行操作。 不是所有的操作符都可以重载,因为“.”, “.*”, “:”, “?:”,sizeof这五个操作符不能重载。2、 操作符重载的形式有哪两种形式?这两种形式有什么区别?答:一种就是作为成员函数重载操作符;另一种就是作为全局(友元)函数重载操作符。当操作符作为类的非静态成员函数来重载时,由于成员函数已经有一个隐藏的参数this,因此对于双目操作符重载函数只需要提供一个参数,对于单目操作符重载函数则不需提供参数。当操作符作为全局函数来重载时,操作符重载函数的参数类型至少有一个为类、结构、枚举或它们的引用类型。而且如果要访问参数类的私有成员,还需要把该函数说明成相应类的友元。对于双目操作符重载函数需要两个参数,对于单目操作符重载函数则需要给出一个参数。操作符=、 ( )、 以及->不能作为全局函数来重载。另外,作为类成员函数来重载时,操作符的第一个操作数必须是类的对象,全局函数重载则否。3、 定义一个时间类Time,通过操作符重载实现:时间的比较(=、!=、>、>=、<、<=)、时间增加/减少若干秒(+=、-=)、时间增加/减少一秒(+、-)以及两个时间相差的秒数(-)。解:class Timeprivate :int hour, minute, second;public :Time()hour=minute=second=0;Time(int h)hour=h;minute=second=0;Time(int h, int m)hour=h;minute=m;second=0;Time(int h, int m, int s)hour=h;minute=m;second=s;Time(const Time &t)hour=t.hour;minute=t.minute;second=t.second;bool operator =(Time &t)if (hour=t.hour && minute=t.minute && second=t.second)return true;return false;bool operator !=(Time &t)return !(*this = t);bool operator > (Time &t)if (hour>t.hour)return true;else if (hour=t.hour && minute>t.minute)return true;else if (hour=t.hour && minute=t.minute && second>t.second)return true;elsereturn false;bool operator >=(Time &t)return *this > t | *this = t;bool operator <(Time &t) return !(*this >= t);bool operator <=(Time &t)return !(*this > t);Time &operator +=(int s)second+=s;while (second>=60)second-=60;minute+;while (minute>=60)minute-=60;hour+;while (hour>=24)hour-=24;return *this;Time &operator -=(int s)second-=s;while (second<0)second+=60;minute-;while (minute<0)minute+=60;hour-;while (hour<0)hour+=24;return *this;Time &operator +() /对Time t,操作为:+t*this += 1;return *this;Time operator +(int) /对Time t,操作为:t+Time t=*this;*this += 1;return t;Time &operator -() *this -= 1;return *this;Time operator -(int) Time t=*this;*this -= 1;return t;int operator -(Time &t)/把时间直接换算成秒数计算int sec1=hour*3600+minute*60+second;int sec2=t.hour*3600+t.minute*60+t.second;return sec2-sec1;4、 利用操作符重载给出一个完整的复数类的定义。解:class Complexprivate :double real, imag;public :Complex()real = imag = 0;Complex(double r)real = r;imag = 0;Complex(double r, double i)real = r;imag = i;Complex(const Complex &c)real=c.real;imag=c.imag; double modulus() const return real*real+imag*imag; Complex operator -() constComplex temp;temp .real = -real;temp.imag = -imag;return temp; friend bool operator =(const Complex &c1,const Complex &c2); friend bool operator !=(const Complex &c1,const Complex &c2); friend bool operator > (const Complex &c1,const Complex &c2); friend bool operator >=(const Complex &c1,const Complex &c2); friend bool operator < (const Complex &c1,const Complex &c2); friend bool operator <=(const Complex &c1,const Complex &c2);friend Complex operator+(const Complex &c1,const Complex &c2);friend Complex operator-(const Complex &c1,const Complex &c2);friend Complex operator*(const Complex &c1,const Complex &c2);friend Complex operator/(const Complex &c1,const Complex &c2);bool operator =(const Complex &c1,const Complex &c2)return (c1.real = c2.real) && (c1.imag = c2.imag);bool operator !=(const Complex &c1,const Complex &c2)return !(c1 = c2);bool operator >(const Complex &c1,const Complex &c2)return c1.modulus() > c2.modulus();bool operator >=(const Complex &c1,const Complex &c2)return c1.modulus() >= c2.modulus();bool operator <(const Complex &c1,const Complex &c2)return c1.modulus() < c2.modulus();bool operator <=(const Complex &c1,const Complex &c2)return c1.modulus() <= c2.modulus();Complex operator+(const Complex &c1,const Complex &c2)Complex temp;temp.real=c1.real+c2.real;temp.imag=c1.imag+c2.imag;return temp;Complex operator-(const Complex &c1,const Complex &c2)Complex temp;temp.real=c1.real-c2.real;temp.imag=c1.imag-c2.imag;return temp;Complex operator*(const Complex &c1,const Complex &c2)Complex temp;temp.real=c1.real*c2.real-c1.imag*c2.imag;temp.imag=c1.real*c2.imag+c1.imag*c2.real;return temp;Complex operator/(const Complex &c1,const Complex &c2)double d=c2.modulus(); if (d != 0) Complex temp; temp.real=(c1.real*c2.real+c1.imag*c2