1、程序设计教程机械工业出版社课后习题答案第7章操作符重载第 7 章 操作符重载1、 为什么要对操作符进行重载?是否所有的操作符都可以重载?答:通过对 C+ 操作符进行重载,我们可以实现用 C+ 的操作符按照通常的习惯来对某些类(特别是一些数学类)的对象进行操作,从而使得程序更容易理解。除此之外,操作符重载机制也提高了 C+ 语言的灵活性和可扩充性,它使得 C+ 操作符除了能对基本数据类型和构造数据类型进行操作外,也能用它们来对类的对象进行操作。不是所有的操作符都可以重载,因为“ . ”, “.* ”, “ :”, “ ?:”, sizeof 这五个操作符不能重载。2、 操作符重载的形式有哪两种形
2、式?这两种形式有什么区别?答:一种就是作为成员函数重载操作符;另一种就是作为全局(友元)函数重载操作符。当操作符作为类的非静态成员函数来重载时,由于成员函数已经有一个隐藏的参数this,因此对于双目操作符重载函数只需要提供一个参数,对于单目操作符重载函数则不需提供参数。当操作符作为全局函数来重载时,操作符重载函数的参数类型至少有一个为类、结构、枚举或它们的引用类型。 而且如果要访问参数类的私有成员, 还需要把该函数说明成相应类的友元。 对于双目操作符重载函数需要两个参数, 对于单目操作符重载函数则需要给出一个参数。操作符 =、 ( ) 、 以及 -不能作为全局函数来重载。另外,作为类成员函数来
3、重载时,操作符的第一个操作数必须是类的对象,全局函数重载则否。3、 定义一个时间类 Time ,通过操作符重载实现:时间的比较( =、 != 、 = 、 (Time &t) if (hourt.hour) return true;else if (hour=t.hour & minutet.minute)return true;else if (hour=t.hour & minute=t.minute & secondt.second)return true;elsereturn false;bool operator =(Time &t) return *this t | *this =
4、t;bool operator = t);bool operator 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 (second0) second+=60; minute-;while (minute0) minute+=60; hour-;while (hour
5、(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) return c1.modulus() c2.modulus();bool operator =(const Complex &c1,const Complex &c2
6、) 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.r
7、eal;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.
8、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.imag)/d; temp.imag=(c1.imag*c2.real-c1.real*c2.imag)/d; return temp;else cout Error in operation / of Complex endl;ex
9、it(-1);5、 定义一个多项式类 Polynomial ,其实例为多项式: a0+a1x+a2 x2+.+an xn,该类具有如下的接口:class Polynomial .public:Polynomial();Polynomial(double coefs, int exps, int size);/ 系数数组、指数数组和项数Polynomial(const Polynomial&); Polynomial();Polynomial& operator=(const Polynomial&);int degree() const; / 最高幂指数double evaluate(doub
10、le x) const; / 计算多项式的值bool operator=(const Polynomial&) const;bool operator!=(const Polynomial&) const;Polynomial operator+(const Polynomial&) const;Polynomial operator-(const Polynomial&) const;Polynomial operator*(const Polynomial&) const;Polynomial& operator+=(const Polynomial&);Polynomial& opera
11、tor-=(const Polynomial&);Polynomial& operator*=(const Polynomial&);解:class Polynomial double *pcoefs;int *pexps;int num_of_items;int add(const Polynomial &p, double *coefs, int *exps) const;int subtract(const Polynomial &p, double *coefs, int *exps) const;public :Polynomial();Polynomial(double coefs
12、, int exps, int size);Polynomial(const Polynomial &p);Polynomial();Polynomial &operator=(const Polynomial &p);int degree() const;double evaluate(double x) const;bool operator=(const Polynomial &p) const;bool operator!=(const Polynomial &p) const;Polynomial operator+(const Polynomial &p) const;Polyno
13、mial operator-(const Polynomial &p) const;Polynomial operator*(const Polynomial &p) const;Polynomial &operator+=(const Polynomial &p);Polynomial &operator-=(const Polynomial &p);Polynomial &operator*=(const Polynomial &p);int Polynomial:add(const Polynomial &p, double *coefs, int *exps) const int co
14、unt=0,i=0,j=0;while (inum_of_items & jp.num_of_items) if (pexpsi = p.pexpsj) if (pcoefsi != -p.pcoefsj) coefscount = pcoefsi + p.pcoefsj; expscount = pexpsi;count+;i+; j+;else if (pexpsi p.pexpsj) coefscount = pcoefsi;expscount = pexpsi;count+; i+;else coefscount = p.pcoefsj; expscount = p.pexpsj;co
15、unt+; j+;if (inum_of_items)while (inum_of_items) coefscount = pcoefsi;expscount = pexpsi;count+; i+;elsewhile (jp.num_of_items) coefscount = p.pcoefsj;expscount = p.pexpsj;count+; j+;return count;int Polynomial:subtract(constPolynomial&p, double*coefs,int*exps)const int count=0,i=0,j=0;while (inum_o
16、f_items & jp.num_of_items) if (pexpsi = p.pexpsj) if (pcoefsi != p.pcoefsj) coefscount = pcoefsi - p.pcoefsj; expscount = pexpsi;count+;i+; j+;else if (pexpsi p.pexpsj) coefscount = pcoefsi; expscount = pexpsi;count+; i+;else coefscount = -p.pcoefsj; expscount = p.pexpsj; count+; j+;if (inum_of_item
17、s)while (inum_of_items) coefscount = pcoefsi;expscount = pexpsi; count+; i+;elsewhile (jp.num_of_items) coefscount = -p.pcoefsj;expscount = p.pexpsj;count+; j+;return count;Polynomial:Polynomial() pcoefs=NULL;pexps=NULL;num_of_items=0;Polynomial:Polynomial(double coefs, int exps, int size) num_of_it
18、ems=size;pcoefs=new doublenum_of_items;pexps=new intnum_of_items;int i;for (i=0; i1; i-) bool exchange=false; for (int j=1; ji; j+) if (pexpsj pexpsj-1) / 交换 pexpsj 和 pexpsj-1int temp1=pexpsj;pexpsj = pexpsj-1;pexpsj-1 = temp1;/ 交换 pcoefsj 和 pcoefsj-1double temp2 = pcoefsj;pcoefsj = pcoefsj-1;pcoefs
19、j-1 = temp2;exchange = true;if (!exchange) break;Polynomial:Polynomial(const Polynomial &p) num_of_items=p.num_of_items;pcoefs=new doublenum_of_items; pexps=new intnum_of_items;for (int i=0; inum_of_items; i+) pcoefsi=p.pcoefsi; pexpsi=p.pexpsi;Polynomial:Polynomial() delete pcoefs; delete pexps;pco
20、efs=NULL;pexps=NULL;num_of_items=0;Polynomial& Polynomial:operator=(const Polynomial &p) delete pcoefs;delete pexps;num_of_items=p.num_of_items;pcoefs=new doublenum_of_items;pexps=new intnum_of_items; for (int i=0; inum_of_items; i+) pcoefsi=p.pcoefsi;pexpsi=p.pexpsi;return *this;int Polynomial:degr
21、ee() const if (num_of_items = 0) return 0;elsereturn pexpsnum_of_items-1;double Polynomial:evaluate(double x) const double sum=0;for (int i=0; inum_of_items; i+) double temp=pcoefsi;for (int j=0; jpexpsi; j+)temp *= x;sum += temp;return sum;bool Polynomial:operator=(const Polynomial &p) const if (num_of_items != p.num_of_items) return false; for (int i=0; inum_of_items; i+)if (pcoefsi!=p.pcoefsi | pexpsi!=p.pexpsi) return false;return tr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1