Пример класса с конструкторами и перегрузками

class coordinate
{
private: 
    int x;
    int y;


public:

    /* ---КОНСТРУКТОРЫ--- */

    coordinate() // конструктор по умолчанию
    {
        x = 0;
        y = 0;
    }

    coordinate(int x, int y) // перегрузка конструктора
    {
        this->x = x;
        this->y = y;
    }

    coordinate(const coordinate& other) // конструктор копирования
    {
        this->x = other.x;
        this->y = other.y;
    }

    ~coordinate(){};  // деструктор


    /* ---МЕТОДЫ--- */

    void SetX(int value) // сеттеры
    {
        x = value;
    }
    void SetY(int value)
    {
        y = value;
    }

    void GetX() // геттеры
    {
        cout << "x = " << x << endl;
    }
    void GetY()
    {
        cout << "y = " << y << endl;
    }


    /* ---ПЕРЕГРУЗКА ОПЕРАТОРОВ--- */

    coordinate& operator=(const coordinate& other) // перегрузка равенства =
    {
        if (this != &other) {
            
            this->x = other.x;
            this->y = other.y;
        }
        return *this;
    }


    bool operator ==(const coordinate& other) // перегрузка ==
    {
        if (this->x == other.x && this->y == other.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator !=(const coordinate& other) // перегрузка !=
    {
        if (this->x != other.x || this->y != other.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    bool operator >(const coordinate &other) // перегрузка >
    {
        if (this->x > other.x && this->y > other.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator <(const coordinate& other) // перегрузка <
    {
        if (this->x < other.x && this->y < other.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    coordinate operator +(const coordinate& other) // перегрузка +
    {
        coordinate temp;
        temp.x = this->x + other.x;
        temp.y = this->y + other.y;
        return temp;
    }
    coordinate operator -(const coordinate& other) // перегрузка -
    {   
        coordinate temp;
        temp.x = this->x - other.x;
        temp.y = this->y - other.y;
        return temp;
    }


    coordinate& operator ++()  // перегрузка префиксного инкремента ++x
    {
        this->x++;
        this->y++;
        return *this;
    }
    coordinate& operator ++(int)  // перегрузка постфиксного инкремента x++
    {   
        coordinate temp(*this);
        this->x++;
        this->y++;

        return temp;
    }
    coordinate& operator --()  // перегрузка префиксного декремента --x
    {
        this->x--;
        this->y--;

        return *this;
    }
    coordinate& operator --(int)  // перегрузка постфиксного декремента x--
    {
        coordinate temp(*this);
        this->x--;
        this->y--;

        return temp;
    }


};

Last updated