C++ 重载运算符以处理类对象?

C++ 重载运算符以处理类对象?,c++,class,overloading,C++,Class,Overloading,如何修改下面的代码,这样我就不需要重复f2=11; f3=12在主功能中。该代码用于重载最常见的运算符 class FLOAT{ private: float x; public: FLOAT(){ x=0.0; } void setFloat(float f) { x=f; } float getFloat() { return x;}; FLO

如何修改下面的代码,这样我就不需要重复
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 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;
}
类浮点{
私人:
浮动x;
公众:
FLOAT(){x=0.0;}
void setFloat(float f){x=f;}
float getFloat(){return x;};
浮点运算符+(FLOAT obj){x=x+obj.x;返回*this;};
浮点运算符-(FLOAT obj){x=x-obj.x;返回*this;};
浮点运算符*(FLOAT obj){x=x*obj.x;返回*this;};
浮点运算符/(FLOAT obj){x=x/obj.x;返回*this;};
FLOAT&operator=(常量FLOAT&obj){this->x=obj.x;返回*this;};
FLOAT&operator=(常量FLOAT&y){this->x=y;返回*this;};
};
int main(){
浮点数f、f2、f3;
f2=11;
f3=12;
f=f3-f2;

cout您的操作符应该创建一个新实例;它们不应该修改自己(事实上,应该声明它们为
const
,以防止这种情况发生)

e、 g:


请注意,定义运算符重载的惯用方法有很多(例如,根据
运算符+=
定义
运算符+
,以及定义采用
浮点值的构造函数)。但是上述方法应该足够了。

您的运算符应该创建一个新实例;它们不应该修改自己(事实上,它们应该声明为
const
,以防止出现这种情况)

e、 g:


请注意,定义运算符重载的惯用方法有很多(例如,根据
运算符+=
定义
运算符+
,以及定义采用
浮点值的构造函数)。但是上面的内容就足够了。

@Oli的回答基本上说明了为了使代码正常工作,您需要做的最简单的事情。但是,我看到(甚至我知道@Oli看到)您的类实现有很多缺陷

既然您已经实现了
FLOAT
,我将向您解释
Double
的实现(
FLOAT
的实现将类似)

请注意,您不需要实现
运算符=(双常量&)
Double(双常量&)
。编译器生成的参数就足够了。由于构造函数接受一个类型为
Double
的参数,因此不需要实现
运算符=(双常量&)
同样。编译器生成的复制语义以及构造函数将处理该问题

现在看这个,

//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;
}
请注意,我分别在
operator+=
operator-
方面实现了
operator+
operator-=


类似地,您可以将
operator/=
operator*=
实现为成员函数,然后根据它们实现
operator/
operator*

@Oli的答案几乎告诉您需要做什么最小的事情才能使代码正常工作您的类实现有许多缺陷

既然您已经实现了
FLOAT
,我将向您解释
Double
的实现(
FLOAT
的实现将类似)

请注意,您不需要实现
运算符=(双常量&)
Double(双常量&)
。编译器生成的参数就足够了。由于构造函数接受一个类型为
Double
的参数,因此不需要实现
运算符=(双常量&)
同样。编译器生成的复制语义以及构造函数将处理该问题

现在看这个,

//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;
}
请注意,我分别在
operator+=
operator-
方面实现了
operator+
operator-=


类似地,您可以将
operator/=
operator*=
实现为成员函数,然后根据它们实现
operator/
operator*

添加一个
FLOAT(FLOAT a_x)
构造函数将改进这一点:
FLOAT operator+(FLOAT obj)const{return FLOAT(x+obj.x)}
添加一个
FLOAT(FLOAT a_x)
构造函数将改进这一点:
FLOAT操作符+(FLOAT obj)const{return FLOAT(x+obj.x);}
推荐阅读:简短摘要是指您编写的运算符的行为几乎与+=、-=、*=、和/=。要获得所需的操作,您需要与+、-、*、和/.缩进类似的运算符。请阅读。推荐阅读:简短摘要是指您编写的运算符的行为几乎与+=、-=、*=、和/=。要获得所需的操作,请行为类似于+、-、*和/.Indentation的eed运算符。请。
//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;
}