过度使用“添加”操作者 [英] Overloding "add" operator
问题描述
我是这个网站的新手,我在C ++运算符重载方面有一些关于OOP的问题。
我尝试使用重载运算符添加两个数字+在更多方面:
1)。第一种方法是:const Point运算符+(const Point& point)const。
所以我用这种方式添加两个数字,使用类参数的类引用:
const Point Point :: operator +( const Point& point) const
{
return Point( this - > x + point.x,此 - > y + point.y);
}
此变体有效。
2)。第二种方法是:const Point运算符+(const int Value)const。
我添加两个带有常数整数值的数字,如参数:
const Point Point :: operator +( const int 值) const
{
return Point( this - > x + Value,此 - > y + Value);
}
此变体也有效。
3)。第三种方式:const Point :: operator +(const Point& point1,const Point& point2)。
但是此代码不正常 。我收到一个错误:
错误:'const Point Point :: operator +(const Point&,const Point&)'必须采用零或一个参数。 />
#include < iostream >
使用 命名空间标准;
class Point
{
private :
int x;
int y;
public :
Point( int x = 0 , int y = 0 );
int getX() const ;
int getY() const ;
void setX( int X);
void setY( int y);
void print() const ;
const 点运算符+( const Point& point1, const Point& point2);
};
Point :: Point( int a, int b)
{
x = a;
y = b;
}
int Point :: getX() const
{
return x;
}
int Point :: getY() const
{
return y;
}
void Point :: setX( int X)
{
此 - > x = X;
}
void Point :: setY( int Y)
{
此 - > y = Y;
}
void Point :: print() const
{
cout<< \ n x =<< x<< \ n \ n y =<< y<<< endl<< endl;
}
const Point :: operator +( const Point& point1, const Point& point2)
{
return Point(point1.x + point2.x,point2.y + point2.y);
}
int main()
{
Point p1( 2 , 8 ),p2( 4 , 5 跨度>);
点p3 = p1 + p2;
p1.print();
p2.print();
p3.print();
点p5 = p1 + p2 + p3;
p5.print();
return 0 ;
}
http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/ [ ^ ]
当操作员不修改操作数时,操作员超载的最佳方法是通过朋友功能。
因此
friend const 点运算符+( const Point& point1, const Point& point2);
和
const 点算子+( const Point& point1, const P oint和放大器; point2)
{
return Point(point1.x + point2.x,point1.y + point2.y);
}
感谢您的关注。
我还有一个问题。
在main()函数中:
点p1( 6 , 3 ),p2( 2 , 6 );
点p3 = p1 + p2;
p1.print();
p2.print();
p3.print();
点p5 = p1 + p2 + p3;
p5.print();
当我编译我获得下一个结果:
x = 6
y = 3
x = 2
y = 6
x = 8
y = 12
x = 16
y = 24
在第三组x,y中,结果应为:x = 8和y = 9当然下一组x,y应该是x = 16,y = 18.
我不明白为什么会这样?
Hi,
I am new on this site, i have some issues about OOP in C++ operator overloading.
I try to do add a two numbers using overloading operator "+" in more ways :
1). First way is : const Point operator+(const Point& point) const.
So i add two numbers in this way using an reference to Point class like parameter :
const Point Point::operator+(const Point& point) const
{
return Point(this->x + point.x, this->y + point.y);
}
This variant works.
2). Second way is : const Point operator+(const int Value) const.
I add two numbers with a constant integer value like parameter :
const Point Point::operator+(const int Value) const
{
return Point(this->x + Value, this->y + Value);
}
This variant works, also.
3). The third way : const Point::operator+(const Point& point1, const Point& point2).
but this code is not ok. I receive an error :
error: 'const Point Point::operator+(const Point&, const Point&)' must take either zero or one argument.
#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(int x = 0, int y = 0);
int getX() const;
int getY() const;
void setX(int X);
void setY(int y);
void print() const;
const Point operator+(const Point& point1, const Point& point2);
};
Point::Point(int a, int b)
{
x = a;
y = b;
}
int Point::getX() const
{
return x;
}
int Point::getY() const
{
return y;
}
void Point::setX(int X)
{
this->x = X;
}
void Point::setY(int Y)
{
this->y = Y;
}
void Point::print() const
{
cout << "\n x = " << x << "\n\n y = " << y <<endl<<endl;
}
const Point::operator+(const Point& point1, const Point& point2)
{
return Point(point1.x + point2.x, point2.y + point2.y);
}
int main()
{
Point p1(2, 8), p2(4, 5);
Point p3 = p1 + p2;
p1.print();
p2.print();
p3.print();
Point p5 = p1 + p2 + p3;
p5.print();
return 0;
}
http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/[^]
"When the operator does not modify its operands, the best way to overload the operator is via friend function."
Thus
friend const Point operator+(const Point& point1, const Point& point2);
and
const Point operator+(const Point& point1, const Point& point2) { return Point(point1.x + point2.x, point1.y + point2.y); }
Thank you for your interest.
I have one more question.
In main() function :
Point p1(6, 3), p2(2, 6); Point p3 = p1 + p2; p1.print(); p2.print(); p3.print(); Point p5 = p1 + p2 + p3; p5.print();
when i compile i obtain the next result :
x = 6 y = 3 x = 2 y = 6 x = 8 y = 12 x = 16 y = 24
In the third group of x,y the result should be : x = 8 and y = 9 and of course the next group of x,y should be x = 16, y = 18.
I don't understand why this ?
这篇关于过度使用“添加”操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!