#include <iostream>
 
using namespace std;
 
class Vector
{
public:
    float x;
    float y;
    float z;
 
    // 단항 연산자 +, +vector = vector
    Vector operator+() const
    {
        return Vector{ +x, +y, +z };
    }
    // 단항 연산자 -, -vector = vector
    Vector operator-() const
    {
        return Vector{ -x, -y, -z };
    }
    // 이항 연산자 +, vector + vector = vector
    Vector operator+(const Vector& v) const
    {
        return Vector{ x + v.x, y + v.y, z + v.z };
    }
    // 이항 연산자 -, vector - vector = vector
    Vector operator-(const Vector& v) const
    {
        return Vector{ x - v.x, y - v.y, z - v.z };
    }
    // 이항 연산자 *, vector * vector = float
    float operator*(const Vector& v) const    // 백터 내적
    {
        return x * v.x + y * v.y + z * v.z;
    }
    // 이항 연산자 *, vector * 3.0f = vector
    Vector operator*(float f) const
    {
        return Vector{ x * f, y * f, z * f };
    }
    // 이항 연산자 /, vector / 3.0f = vector
    Vector operator/(float f) const
    {
        return Vector{ x / f, y / f, z / f };
    }
    // 전위 연산자++, ++vector
    Vector& operator++()    // 복사 되어서 가기 때문에 주소값을 넘김.
    {
        ++x;
        ++y;
        ++z;
        return *this;
    }
    // 전위 연산자 --, --vector
    Vector& operator--()    // 복사 되어서 가기 때문에 주소값을 넘김.
    {
        --x;
        --y;
        --z;
        return *this;
    }
    // 후위 연산자 ++, vector++
    Vector operator++(int)
    {
        Vector temp = *this;
        ++(*this);
        return temp;
    }
    // 후위 연산자 --, vector--
    Vector operator--(int)
    {
        Vector temp = *this;
        --(*this);
        return temp;
    }
 
    void print() const
    {
        cout << "x : " << x << ", y : " << y << ", z : " << z << endl;
    }
};
 
class VectorI
{
    friend class VectorF;
 
private:
    int x;
    int y;
    int z;
 
public:
    VectorI(int x, int y, int z)
        :x(x), y(y), z(z)
    {
    }
};
 
class VectorF
{
private:
    float x;
    float y;
    float z;
 
public:
    VectorF(float x, float y, float z)
        :x(x), y(y), z(z)
    {
    }
 
    VectorF operator+(const VectorI& v) const
    {
        return VectorF(x + v.x, y + v.y, z + v.z);
    }
};
 
 
int main()
{
    // 단항 연산자 +, +vector = vector
    // 단항 연산자 -, -vector = vector
    // 이항 연산자 +, vector + vector = vector
    // 이항 연산자 -, vector - vector = vector
    // 이항 연산자 *, vector * 3.0f = vector, vector * vector = float, 
    // 이항 연산자 /, vector / 3.0f = vector
    // 전위 연산자 ++ --, ++vector, --vector
    // 후위 연산자 ++ --, vector++, vector--
 
    const Vector v0{ 012 };
    const Vector v1{ 123 };
 
    v0.print();
    v1.print();
 
    Vector v2 = +v0;
    v2.print();
 
    Vector v3 = -v0;
    v3.print();
 
    Vector v4 = v0 + v1;
    v4.print();
 
    Vector v5 = v0 - v1;
    v5.print();
 
    float v6 = v0 * v1;
    cout << v6 << endl;
 
    Vector v7 = v0 * 3.0f;
    v7.print();
 
    Vector v8 = v0 / 3.0f;
    v8.print();
    Vector v9 = ++v2;
    v9.print();
 
    Vector v10 = v2++;
    v10.print();
 
    Vector v11 = --v3;
    v11.print();
 
    Vector v12 = v3--;
    v12.print();
 
}
 
cs

'::public > C++' 카테고리의 다른 글

논리 연산자 오버로딩  (0) 2020.11.23
비교&관계 연산자 오버로딩  (0) 2020.09.23
extern  (0) 2020.09.01
공용체(union)  (0) 2020.07.13
구조체(struct)  (0) 2020.07.13

+ Recent posts