| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1073 人关注过本帖
标题:请教大家,着急,谢谢了
只看楼主 加入收藏
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
以下是引用wumingsx在2006-3-16 23:13:00的发言:

看看这个程序
#include<iostream>
using namespace std;

class Complex{
double real; //实数部分
double imag; //虚数部分
public:
Complex(double real=0,double imag=0){}
Complex operator+(Complex &s);
Complex operator+(int &s);
friend Complex operator+(int &s,Complex &s);
void disp();
};

Complex Complex::operator+(Complex &s)
/*{
Complex a;
a.real=real+s.real;
a.imag=imag+s.imag;
return a;
}*/
{
return Complex(real+s.real,imag+s.imag);
}
Complex Complex::operator+(int &s)
{
return Complex(real+s,imag);
}

Complex Complex::operator+(int &s , Complex &s1 )
{
return Complex(s+s1.real,s1.imag);
}

void Complex::disp()
{
cout<<"( "<<real<<","<<imag<<"i)";
}

int main()
{
Complex c1(3,3),c2(-1,0),c3,c4;
c3=c1+c2;
cout<<"c3=";
c3.disp();
int i=7;
c4=c1+i;
c3=i+c2;
c4.disp();
cout<<endl;
c3.disp();
return 0;
}


这个程序确定错误不少,多看看书吧,我也才学C++两周


天行健,君子以自强不息!!QQ:68660681
2006-03-17 12:24
名人
Rank: 1
等 级:新手上路
威 望:1
帖 子:205
专家分:0
注 册:2006-3-3
收藏
得分:0 

这个一个典型的操作符重载的一个例子,当返回一个局部变量或对象的时候不能返回一个引用.

#include<iostream>
using namespace std;

class Complex
{
private:
double real;
double imag;
public:
Complex(){
real=0;
imag=0;
}
Complex(double r,double i){
real=r;
imag=i;
}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
};

Complex Complex::operator+(Complex &c2)
{
return Complex( real + c2.real, imag + c2.imag );
}

Complex Complex::operator-(Complex &c2)
{
return Complex( real-c2.real, imag-c2.imag );
}

Complex Complex::operator*(Complex &c2)
{
return Complex( real * c2.real, imag * c2.imag );
}

Complex Complex::operator/(Complex &c2)
{
return Complex( real / c2.real, imag / c2.imag );
}

void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4), c2(5,-10), c3;

c3=c1+c2;
cout<<"c3=:";
c1.display();

c3=c1-c2;
cout<<"c3=:";
c2.display();

c3=c1*c2;
cout<<"c3=:";
c3.display();

return 0;
}


未必钱多乐便多,财多累己招烦恼。 清贫乐道真自在,无牵无挂乐逍遥。
2006-03-17 14:05
名人
Rank: 1
等 级:新手上路
威 望:1
帖 子:205
专家分:0
注 册:2006-3-3
收藏
得分:0 
以下是MSDN上对操作符重载的描述:

operator

C++ Specific —>

type operator operator-symbol ( parameter-list )

The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands.

Rules of Operator Overloading

  • You can overload the following operators:
    + - * / % ^
    ! = < > += –=
    ^= &= |= << >> <<=
    <= >= && || ++ ––
    ( ) [ ] new delete & |
    ~ *= /= %= >>= ==
    != , –> –>*

  • If an operator can be used as either a unary or a binary operator, you can overload each use separately.

  • You can overload an operator using either a nonstatic member function or a global function that's a friend of a class. A global function must have at least one parameter that is of class type or a reference to class type.

  • If a unary operator is overloaded using a member function, it takes no arguments. If it is overloaded using a global function, it takes one argument.

  • If a binary operator is overloaded using a member function, it takes one argument. If it is overloaded using a global function, it takes two arguments.

Restrictions on Operator Overloading

  • You cannot define new operators, such as **.

  • You cannot change the precedence or grouping of an operator, nor can you change the numbers of operands it accepts.

  • You cannot redefine the meaning of an operator when applied to built-in data types.

  • Overloaded operators cannot take default arguments.

  • You cannot overload any preprocessor symbol, nor can you overload the following operators:
  • The assignment operator has some additional restrictions. It can be overloaded only as a nonstatic member function, not as a friend function. It is the only operator that cannot be inherited; a derived class cannot use a base class's assignment operator.

END C++ Specific

Example

The following example overloads the + operator to add two complex numbers and returns the result.

// Example of the operator keyword
class Complex
{
public:
Complex( float re, float im );
Complex operator+( Complex &other );
friend Complex operator+( int first, Complex &second );
private:
float real, imag;
};

// Operator overloaded using a member function
Complex Complex::operator+( Complex &other )
{
return Complex( real + other.real, imag + other.imag );
};

// Operator overloaded using a friend function
Complex operator+( int first, Complex &second )
{
return Complex( first + second.real, second.imag );
}

[此贴子已经被作者于2006-3-17 14:22:37编辑过]


未必钱多乐便多,财多累己招烦恼。 清贫乐道真自在,无牵无挂乐逍遥。
2006-03-17 14:16
名人
Rank: 1
等 级:新手上路
威 望:1
帖 子:205
专家分:0
注 册:2006-3-3
收藏
得分:0 
以下是MSDN上的一个例子:

END C++ Specific

Example

The following example overloads the + operator to add two complex numbers and returns the result.

// Example of the operator keyword
class Complex
{
public:
   Complex( float re, float im );
   Complex operator+( Complex &other );
   friend Complex operator+( int first, Complex &second );
private:
   float real, imag;
};

// Operator overloaded using a member function
Complex Complex::operator+( Complex &other )
{
	return Complex( real + other.real, imag + other.imag );
};

// Operator overloaded using a friend function
Complex operator+( int first, Complex &second )
{
	return Complex( first + second.real, second.imag );
}
<SCRIPT language=JavaScript src=\"MS-ITS:dsmsdn.chm::/html/msdn_footer.js\">

</script>

未必钱多乐便多,财多累己招烦恼。 清贫乐道真自在,无牵无挂乐逍遥。
2006-03-17 14:18
快速回复:请教大家,着急,谢谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020074 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved