#include <iostream>
using namespace std;
// ======================= VECTOR2 =======================
class VECTOR2 {
protected:
double x, y;
public:
// конструкторы
VECTOR2() : x(0), y(0) {}
VECTOR2(double x, double y) : x(x), y(y) {}
// display
void display() const {
cout << "(" << x << ", " << y << ")" << endl;
}
// умножение на число
VECTOR2 operator*(double k) const {
return VECTOR2(x * k, y * k);
}
// деление на число
VECTOR2 operator/(double k) const {
return VECTOR2(x / k, y / k);
}
// сложение
VECTOR2 operator+(const VECTOR2& v) const {
return VECTOR2(x + v.x, y + v.y);
}
// вычитание
VECTOR2 operator-(const VECTOR2& v) const {
return VECTOR2(x - v.x, y - v.y);
}
// скалярное произведение
double operator*(const VECTOR2& v) const {
return x * v.x + y * v.y;
}
// равенство
bool operator==(const VECTOR2& v) const {
return (x == v.x && y == v.y);
}
// формула: c = k*a + m*b
static VECTOR2 linear(double k, const VECTOR2& a, double m, const VECTOR2& b) {
return VECTOR2(k * a.x + m * b.x, k * a.y + m * b.y);
}
};
// ======================= VECTOR3 =======================
class VECTOR3 : public VECTOR2 {
protected:
double z;
public:
// конструкторы
VECTOR3() : VECTOR2(), z(0) {}
VECTOR3(double x, double y, double z) : VECTOR2(x, y), z(z) {}
// display
void display() const {
cout << "(" << x << ", " << y << ", " << z << ")" << endl;
}
// умножение на число
VECTOR3 operator*(double k) const {
return VECTOR3(x * k, y * k, z * k);
}
// деление на число
VECTOR3 operator/(double k) const {
return VECTOR3(x / k, y / k, z / k);
}
// сложение
VECTOR3 operator+(const VECTOR3& v) const {
return VECTOR3(x + v.x, y + v.y, z + v.z);
}
// вычитание
VECTOR3 operator-(const VECTOR3& v) const {
return VECTOR3(x - v.x, y - v.y, z - v.z);
}
// скалярное произведение
double operator*(const VECTOR3& v) const {
return x * v.x + y * v.y + z * v.z;
}
// равенство
bool operator==(const VECTOR3& v) const {
return (x == v.x && y == v.y && z == v.z);
}
};
// ======================= MAIN =======================
int main() {
VECTOR2 a(1, 2), b(3, 4);
cout << "VECTOR2:" << endl;
(a + b).display();
(a - b).display();
(a * 2).display();
(b / 2).display();
cout << "Dot: " << (a * b) << endl;
cout << "Equal: " << (a == b) << endl;
VECTOR2 c = VECTOR2::linear(2, a, 3, b);
cout << "k*a + m*b: ";
c.display();
cout << "\nVECTOR3:" << endl;
VECTOR3 v1(1, 2, 3), v2(4, 5, 6);
(v1 + v2).display();
(v1 - v2).display();
(v1 * 2).display();
(v2 / 2).display();
cout << "Dot: " << (v1 * v2) << endl;
cout << "Equal: " << (v1 == v2) << endl;
return 0;
}