#include <iostream> #include <string> using namespace std; class Vector { private: int x; int y; int z; public: Vector() { } Vector(int x, int y, int z) :x(x), y(y), z(z) { } friend ostream& operator<<(ostream& os, const Vector& v) { os << v.x << " " << v.y << " " << v.z; return os; } friend istream& operator>>(istream& is, Vector& v) { string temp; is >> temp; v.x = stoi(temp); is >> temp; v.y = stoi(temp); is >> temp; v.z = stoi(temp); return is; } Vector operator~() const { return Vector(~x, ~y, ~z); } Vector operator&(const Vector& v) const { return Vector(x & v.x, y & v.y, z & v.z); } Vector operator|(const Vector& v) const { return Vector(x | v.x, y | v.y, z | v.z); } Vector operator^(const Vector& v) const { return Vector(x ^ v.x, y ^ v.y, z & v.z); } Vector operator<<(int v) const { return Vector(x << v, y << v, z << v); } Vector operator>>(int v) const { return Vector(x >> v, y >> v, z >> v); } }; int main() { Vector v{ 1, 2, 3 }; cout << v << endl; // operator<<(cout, v); operator<<(cout, v).operator<<(endl); cin >> v; // operator>>(cin, v) cout << v << endl; Vector v0{ 0, 0, 0 }; cout << ~v0 << endl; Vector v1{ 1, 2, 3 }; cout << (v0 & v1) << endl; cout << (v0 | v1) << endl; cout << (v1 ^ v1) << endl; Vector v2{ 2, 4, 8 }; cout << (v2 << 2) << endl; cout << (v2 >> 2) << endl; } | cs |
'::public > C++' 카테고리의 다른 글
대입 연산자 오버로딩, 복사 생성자 (0) | 2020.11.23 |
---|---|
첨자 연산자 오버로딩 (0) | 2020.11.23 |
논리 연산자 오버로딩 (0) | 2020.11.23 |
비교&관계 연산자 오버로딩 (0) | 2020.09.23 |
연산자 오버로딩 (0) | 2020.09.15 |