bin554385863 发表于 2019-11-16 22:39:42

C++运算符重载

本帖最后由 bin554385863 于 2019-11-16 23:03 编辑

#include <iostream>
class Circle
{
private:
    double Radius; //半径
public:
    static const double PI;
    Circle(double r = 0) : Radius(r) //构造函数
    {
    }
    Circle(Circle &circle) //赋值构造
    {
      this->Radius = circle.Radius;
    }
    void setCircleRadius(double r) //设置半径
    {
      Radius = r;
    }
    double getCircleRadius() //获取半径
    {
      return Radius;
    }
    double getCirclePerimeter() //获取周长
    {
      return 2 * PI * Radius;
    }
    double getCircleArea() //获取面积
    {
      return PI * Radius * Radius;
    }
    /*扩大倍数*/
    Circle &operator*(int n)
    {
      this->Radius = this->Radius * n;
      return *this;
    }
    friend std::ostream &operator<<(std::ostream &os, Circle &circle) //重载<<运算符
    {
      os << "CircleRadius: " << circle.Radius << "\n"
         << "CirclePerimeter: " << circle.getCirclePerimeter() << "\n"
         << "CircleArea: " << circle.getCircleArea() << std::endl;
      return os;
    }
    ~Circle() {}
};
double const Circle::PI = 3.141592653;
int main(int argc, char const *argv[])
{
    Circle circle, ncircle;
    std::cout << circle << std::endl;
    circle.setCircleRadius(10);
    std::cout << circle << std::endl;
    ncircle = circle * 2;
    std::cout << ncircle;
    return 0;
}

-------------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-q01elhji.d3w --stdout=Microsoft-MIEngine-Out-cvizs1af.xw1 --stderr=Microsoft-MIEngine-Error-i5s3bovj.1rj --pid=Microsoft-MIEngine-Pid-hxzv5xmu.hvn --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
CircleRadius: 0
CirclePerimeter: 0
CircleArea: 0

CircleRadius: 10
CirclePerimeter: 62.8319
CircleArea: 314.159

CircleRadius: 20
CirclePerimeter: 125.664
CircleArea: 1256.64

E:\Users\admin\Documents\VScode>
----------------------------------------------------------------------------------------------------------
这是我写的一个简单求圆的周长和面积的类
第32行对*的重载使得ncircle = circle * 2成立, 怎么重载也让ncircle = 2*circle成立;
Circle &operator*(Circle &circle, int n)---总是报错参数过多

bin554385863 发表于 2019-11-16 22:50:45

本帖最后由 bin554385863 于 2019-11-16 23:02 编辑

{:10_256:}
找到方法了
/*扩大倍数*/
    Circle &operator*(int n)
    {
      this->Radius = n*this->Radius;
      return *this;
    }
    friend Circle operator*(int n, Circle &circle)
    {
      return circle*n;
    }

bin554385863 发表于 2019-11-16 23:03:26

本帖最后由 bin554385863 于 2019-11-17 01:00 编辑

#include <iostream>
class Circle
{
private:
    double Radius; //半径
public:
    static const double PI;
    Circle(double r = 0) : Radius(r) //构造函数
    {
    }
    Circle(const Circle &circle) //赋值构造
    {
      this->Radius = circle.Radius;
    }
    void setCircleRadius(double r) //设置半径
    {
      Radius = r;
    }
    double getCircleRadius() const //获取半径
    {
      return Radius;
    }
    double getCirclePerimeter() const //获取周长
    {
      return 2 * PI * Radius;
    }
    double getCircleArea() const//获取面积
    {
      return PI * Radius * Radius;
    }
    /*扩大倍数*/
    Circle &operator*(int n)
    {
      static Circle circle;
      circle.Radius = n*this->Radius;
      return circle;
    }
    friend Circle operator*(int n, Circle &circle)
    {
      return circle*n;
    }
    friend std::ostream &operator<<(std::ostream &os, Circle &circle) //重载<<运算符
    {
      os << "CircleRadius: " << circle.Radius << "\n"
         << "CirclePerimeter: " << circle.getCirclePerimeter() << "\n"
         << "CircleArea: " << circle.getCircleArea() << std::endl;
      return os;
    }
    ~Circle() {}
};
double const Circle::PI = 3.141592653;
int main(int argc, char const *argv[])
{
    Circle circle0, circle1, circle2,circle3;
    circle1.setCircleRadius(5);
    circle2 = circle1*2;
    circle3 = 5*circle2;
    std::cout<<circle0<<std::endl;
    std::cout<<circle1<<std::endl;
    std::cout<<circle2<<std::endl;
    std::cout<<circle3<<std::endl;
    return 0;
}


------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-bq53flzb.pdd --stdout=Microsoft-MIEngine-Out-0ezshnul.zpd --stderr=Microsoft-MIEngine-Error-dzndlmdf.zut --pid=Microsoft-MIEngine-Pid-xianufj3.alm --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
CircleRadius: 0
CirclePerimeter: 0      
CircleArea: 0

CircleRadius: 5
CirclePerimeter: 31.4159
CircleArea: 78.5398

CircleRadius: 10
CirclePerimeter: 62.8319
CircleArea: 314.159

CircleRadius: 50
CirclePerimeter: 314.159
CircleArea: 7853.98


E:\Users\admin\Documents\VScode>
页: [1]
查看完整版本: C++运算符重载