文章作者 100test 发表时间 2007:03:10 17:11:46
来源 100Test.Com百考试题网
1•. 重载为类的成员函数:
格式: < 类名 > operator < 运算符 >(< 参数表 >)
例如:利用重载运算符实现复数类对象的算术四则运算。
#include
class complex
{
public:
complex(){ real=imag=0.}
complex(double r,double I)
{
real=r.imag=I.
}
complex operator (complex &.c).
complex operator -(complex &.c).
complex operator *(complex &.c).
complex operator /(complex &.c).
friend void print(complex &.c).
private:
double real,imag.
}.
inline complex complex::operator (complex &.c)
{
return complex(real c.real,imag c.imag).
}
inline complex complex::operator –(complex &.c)
{
return complex(real-c.real,imag-c.imag).
}
inline complex complex::operator *(complex &.c)
{
return complex(real*c.real-imag*c.imag,real*c.imag imag*c.real).
}
inline complex complex::operator /(complex &.c)
{
return complex((real*c.real imag*c.imag)/(c.real*c.real c.imag*c.imag),
(imag*c.real-real*c.imag)/(c.real*c.real c.imag*c.imag)).
}
void print(complex &.c)
{
if(c.imag<0)
cout< else cout< } void main() { complex c1(2.0,3.0),c2(4.0,-2.0),c3. c3=c1 c2. cout<<”\nc1 c2=”. print(c3). c3=c1-c2. cout<<”\nc1-c2=”. print(c3). c3=c1*c2. cout<<”\nc1*c2=”. print(c3). c3=c1/c2. cout<<”\nc1/c2=”. print(c3). c3=(c1 c2)*(c1-c2)*c2/c1. cout<<”\n(c1 c2)*(c1-c2)*c2/c 1” . print(c3). cout< } 说明:①程序中的表达式: c1 c2 ,编译程序将解释 c1.operator (c2) ②当重载成为成员函数时,双目运算符仅有一个参数。对单目运算符,重载为成员函数时,总是隐含了一个 参数,该参数是 this 指针。 This 指针指向调用该成员函数对象的指针。 2、重载为友员函数: 当重载友员函数时,将没有隐含的参数 this 指针。这样,对双目运算符,友员函数有2个参数,对单目运算符,友员函数有一个参数。 格式: friend < 类型说明符 >operaotr < 运算符 >(< 参数表 >) {……..} 例如:重写上例: #include class complex { public: complex (){real=imag=0.} complex(double r,double I) { real=r.imag=I. } friend complex operator (complex &.c1,complex &.c2). friend complex operator -(complex &.c1,complex &.c2). friend complex operator *(complex &.c1,complex &.c2). friend complex operator /(complex &.c1,complex &.c2). friend void print(complex &.c). private: double real,imag. }. complex operator (complex &.c1,complex &.c2) { return complex(c1real c2.real,c1.imag c2.imag). } complex operator –(complex &.c1,complex &.c2) { return complex(c1.real-c2.real,c1.imag-c2.imag). } complex operator *(complex &.c1,complex &.c2) { return complex(c1real*c2.real-c1.imag*c2.imag,c1.real*c2.imag c1.imag*c2.real). } complex operator /(complex &.c1,complex &.c2) { return complex((c1.real*c2.real c1.imag*c2.imag)/(c1.real*c2.real c1.imag*c2.imag), (c1.imag*c2.real-c1.real*c2.imag)/(c1.real*c2.real c1.imag*c2.imag)). } void print(complex &.c) { if(c.imag<0) cout< else cout< } void main() { complex c1(2.0,3.0),c2(4.0,-2.0),c3. c3=c1 c2. cout<<”\nc1 c2=”. print(c3). c3=c1-c2. cout<<”\nc1-c2=”. print(c3). c3=c1*c2. cout<<”\nc1*c2=”. print(c3). c3=c1/c2. cout<<”\nc1/c2=”. print(c3). c3=(c1 c2)*(c1-c2)*c2/c1. cout<<”\n(c1 c2)*(c1-c2)*c2/c 1” . print(c3). cout< } 说明: c1 c2 编译程序结实为 operator (c1,c2), 将调用 complex operator (complex &.c1,complex &.c2) 求值。 同理, c1-c2 理解为 operator –(c1,c2), 调用 complex operator –(complex &.c1,complex &.c2) 实现上述表达式的求值。 3 、两种重载形式的比较:
相关文章
06年9月计算机等级考试三级C语言上机试题
二级C 多态性:运算符重载中的几个问题
二级C 多态性:运算符重载中的两种形式
05年4月计算机二级C语言考试试题及答案
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛