重载操作符以处理类对象? [英] Overload operators to work with class objects?
问题描述
如何修改下面的代码,我不需要重复 f2 = 11;
。代码用于重载最常见的运算符。
f3 = 12;
class FLOAT {
private:
float x;
public:
FLOAT(){x = 0.0; }
void setFloat(float f){x = f; }
float getFloat(){return x;};
FLOAT运算符+(FLOAT obj){x = x + obj.x; return * this;};
FLOAT operator-(FLOAT obj){x = x-obj.x; return * this;};
FLOAT operator *(FLOAT obj){x = x * obj.x; return * this;};
FLOAT运算符/(FLOAT obj){x = x / obj.x;返回* this;};
FLOAT& operator =(const FLOAT& obj){this-> x = obj.x; return * this; };
FLOAT& operator =(const float& y){this-> x = y; return * this; };
};
int main(){
FLOAT f,f2,f3;
f2 = 11;
f3 = 12;
f = f3-f2;
cout<<f3-f2 =<< f.getFloat()<< endl;
f2 = 11;
f3 = 12;
f = f3 + f2;
cout<<f3 + f2 =<< f.getFloat()<< endl;
f2 = 11;
f3 = 12;
f = f3 * f2;
cout<<f3 * f2 =<< f.getFloat()<< endl;
f2 = 11;
f3 = 12;
f = f3 / f2;
cout<<f3 / f2 =<< f.getFloat()<< endl;
系统(pause); //暂停控制台屏幕
return 0;
}
你为了使你的代码工作,你需要做什么 minimal 。但是,我看到(甚至@Oli看到)你的类的实现有很多缺陷。
因为你已经实现 FLOAT
,我解释你的实现 Double
( FLOAT
类似)。
class Double {
double data;
public:
double(double p = 0.0):data(p){}
double value(){return data; }
Double& operator + =(Double const& other)
{
data + = other.data;
return * this;
}
Double& operator - =(double const& other)
{
data - = other.data;
return * this;
}
// ...
};
注意,你不需要实现 operator =(Double const& )
和 Double(Double const&)
。编译器生成的就足够了。因为构造函数接受 double
类型的一个参数,所以不需要实现 operator =(double const&)
也。编译器生成的复制语义,以及构造函数,将照顾。
现在看这个,
//实现operator +作为非成员函数
双运算符+(Double a,Double const& b)
{
a + = b; // a是本地拷贝,所以我们可以改变它
return a;
}
Double operator-(Double a,Double const& b)
{
a - = b; // a是本地拷贝,所以我们可以改变它
return a;
}
注意,我已经实现了 operator + $
运算符+ =
和运算符 - 和
类似地,你可以实现
operator / =
和。
operator /
和 operator *
的他们! How can I modify the following code in such way I don't need to repeat f2=11;
f3=12;
in the main function. The code is for overloading the most common operators.
class FLOAT{
private:
float x;
public:
FLOAT(){ x=0.0; }
void setFloat(float f) { x=f; }
float getFloat() { return x;};
FLOAT operator+(FLOAT obj) {x=x+obj.x; return *this;};
FLOAT operator-(FLOAT obj) {x=x-obj.x; return *this;};
FLOAT operator*(FLOAT obj) {x=x*obj.x; return *this;};
FLOAT operator/(FLOAT obj) {x=x/obj.x; return *this;};
FLOAT& operator=(const FLOAT& obj) {this->x=obj.x; return *this; };
FLOAT& operator=(const float& y) {this->x=y; return *this; };
};
int main() {
FLOAT f,f2,f3;
f2=11;
f3=12;
f=f3-f2;
cout<<"f3-f2 ="<<f.getFloat()<<endl;
f2=11;
f3=12;
f=f3+f2;
cout<<"f3+f2 ="<<f.getFloat()<<endl;
f2=11;
f3=12;
f=f3*f2;
cout<<"f3*f2 ="<<f.getFloat()<<endl;
f2=11;
f3=12;
f=f3/f2;
cout<<"f3/f2 ="<<f.getFloat()<<endl;
system("pause"); // to pause console screen
return 0;
}
@Oli's answer pretty much says you what minimal thing you need to do in order to make your code work. However, I see (and I know even @Oli sees) that your implementation of the class has many flaws.
Since you've implemented FLOAT
, I'm explaining you the implementation of Double
(the implementation of FLOAT
would be similar).
class Double {
double data;
public:
Double (double p=0.0) : data(p){}
double value() { return data; }
Double & operator+=(Double const & other)
{
data += other.data;
return *this;
}
Double & operator-=(Double const & other)
{
data -= other.data;
return *this;
}
//...
};
Note that you don't need to implement operator=(Double const&)
and Double(Double const&)
. The compiler generated ones would be enough. Since the constructor takes one argument of type double
, you don't need to implement operator=(double const &)
also. The compiler generated copy-semantics, along with the constructor, would take care of that.
Now see this,
//implement operator+ and operator- as non-member functions
Double operator+(Double a, Double const & b)
{
a += b; //a is local copy, so we can change it
return a;
}
Double operator-(Double a, Double const & b)
{
a -= b; //a is local copy, so we can change it
return a;
}
Note that I've implemented operator+
and operator-
in terms of operator+=
and operator-=
respectively.
Similarly, you can implement operator/=
and operator*=
as member functions, and then implement operator/
and operator*
in terms of the them!
这篇关于重载操作符以处理类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!