У меня проблема с перегрузкой унарного оператора.Таким образом, в коде, показанном ниже, я в основном не получаю желаемый результат, который соответствует моей школе.Смотрите ниже для получения дополнительной информации.Есть некоторые ограничения на функцию, я не могу добавить какие-либо параметры, иначе это дает мне ошибку компиляции.Так что мне с этим делать?Дайте мне знать, если вам нужна дополнительная информация.Спасибо!
Point& Point::operator-()
{
x = -x;
y = -y;
return *this;
}
Вот результат:
********** Мой унарный тест **********
pt1 = (3,4)
pt2 = -pt1
pt1 = (-3, -4)
pt2 = (-3, -4)
pt3 = (-3,4)
pt4 = - - -pt3
pt3 = (3, -4)
pt4 = (3, -4)
********** Одинарный школьный тест **********
pt1 = (3, 4)
pt2= -pt1
pt1 = (3, 4) //
pt2 = (-3, -4)
pt3 = (-3, 4)
pt4 = - - -pt3
pt3 = (-3, 4) //
pt4 = (3, -4)
Файл драйвера
void UnaryTest(void)
{
cout << "\n********** Unary test ********** " << endl;
Point pt1(3, 4);
cout << "pt1 = " << pt1 << endl;
Point pt2 = -pt1;
cout << "pt2 = -pt1" << endl;
cout << "pt1 = " << pt1 << endl;
cout << "pt2 = " << pt2 << endl;
cout << endl;
Point pt3(-3, 4);
cout << "pt3 = " << pt3 << endl;
Point pt4 = - - -pt3;
cout << "pt4 = - - -pt3" << endl;
cout << "pt3 = " << pt3 << endl;
cout << "pt4 = " << pt4 << endl;
}
файл list.h
class Point
{
public:
explicit Point(double x, double y);
Point();
double getX() const;
double getY() const;
Point operator+(const Point& other)const ;
Point& operator+(double value);
Point operator*(double value) ;
Point operator%(double angle);
double operator-(const Point& other)const ;
Point operator-(double value);
Point operator^(const Point& other);
Point& operator+=(double value);
Point& operator+=(const Point& other) ;
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
Point& operator-() ;
// Overloaded operators (14 member functions)
friend std::ostream &operator<<( std::ostream &output, const Point
&point );
friend std::istream &operator>>( std::istream &input, Point
&point );
// Overloaded operators (2 friend functions)
private:
double x; // The x-coordinate of a Point
double y; // The y-coordinate of a Point
// Helper functions
double DegreesToRadians(double degrees) const;
double RadiansToDegrees(double radians) const;
};
// Point& Add(const Point& other); // Overloaded operators (2 non-member,
non-friend functions)
// Point& Multiply(const Point& other);
Point operator+( double value, const Point& other );
Point operator*( double value, const Point& other );