#include <iostream>
using namespace std;
class Coordinates {
int _x, _y, _z;
public:
Coordinates() { _x = _y = _z = 0; }
Coordinates(int a, int b, int c) {
_x = a;
_y = b;
_z = c;
}
friend Coordinates operator+(Coordinates obj1, Coordinates obj2);
void show() { cout << _x << " " << _y << " " << _z << endl; }
};
Coordinates operator+(Coordinates obj1, Coordinates obj2) {
Coordinates temp;
temp._x = obj1._x + obj2._x;
temp._y = obj1._y + obj2._y;
temp._z = obj1._z + obj2._z;
return temp;
};
void main()
{
setlocale(LC_ALL, "Rus");
Coordinates a(1, 2, 3), b(4, 5, 6), c;
a.show(); // 1 2 3
b.show(); // 4 5 6
c = a + b;
c.show(); // 5 7 9
system("pause");
}
Результата работы программы — Перегрузка операторов с использованием дружественной функции friend на C++ (Объектно-ориентированное программирование)

Также функция friend не имеет указателя this
#include <iostream>
using namespace std;
class Myclass {
int _x;
int _y;
public:
Myclass(int x, int y) { _x = x; _y = y; }
friend int multiplier(Myclass ob);
};
int multiplier(Myclass obj) {
int max = obj._x < obj._y ? obj._y : obj._x;
return max;
}
void main()
{
setlocale(LC_ALL, "Rus");
Myclass obj(3, 77); // 77
cout << multiplier(obj) << endl;
system("pause");
}
Результата работы программы
![]()
