负号重载
#include
using namespace std;
class points
{
private:
int x;
int y;
public:
points (int, int);
void display();
const points operator-() const;
};
points::points(int x, int y) : x(x), y(y){};
void points::display()
{
cout << "x: " << this->x << "y: " << this->y;
};
const points points::operator-() const
{
return points(-this->x,-this->y);
}
int main()
{
points p1(11,22);
points p2=-p1;
points p3=-(-p1);
p2.display();
p3.display();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41