-
C++) 연산자 오버로딩C++ 2022. 11. 24. 16:24
https://www.inflearn.com/course/following-c-plus/dashboard
홍정모의 따라하며 배우는 C++ - 인프런 | 강의
만약 C++를 쉽게 배울 수 있다면 배우지 않을 이유가 있을까요? 성공한 프로그래머로써의 경력을 꿈꾸지만 지금은 당장 하루하루 마음이 초조할 뿐인 입문자 분들을 돕기 위해 친절하고 자세하
www.inflearn.com
산술 연산자 오버로딩
#include <iostream> using namespace std; class Cents { private: int _cents; public: Cents(int cents) { _cents = cents; } int getCents() const { return _cents; } Cents operator + (const Cents& c2) { return Cents(this->_cents + c2._cents); } Cents operator * (const Cents& c2) { return Cents(this->_cents * c2._cents); } }; int main() { Cents cents1(6); Cents cents2(8); cout << (cents1 + cents2 * Cents(5)).getCents() << endl; // 6 + 8 * 5 = 46 return 0; }
입출력 연산자 오버로딩
#include <iostream> #include<fstream> using namespace std; class Point { private: double _x, _y, _z; public: Point(double x = 0.0, double y = 0.0, double z = 0.0) : _x(x), _y(y), _z(z) {} double getX() { return _x; } double getY() { return _y; } double getZ() { return _z; } // 첫번째 parameter가 ostream이라 member function으로 만들 수 없다 friend ostream& operator << (ostream& out, const Point& point) { out << "(" << point._x << " " << point._y << " " << point._z << ")"; return out; } friend istream& operator >> (istream& in, Point& point) { in >> point._x >> point._y >> point._z; return in; } }; int main() { ofstream of("out.txt"); //Point p1(0.1, 0.2, 0.3), p2(0.4, 1.1, 2.3); Point p1, p2; cin >> p1 >> p2; cout << p1 << " " << p2 << endl; of << p1 << " " << p2 << endl; of.close(); return 0; }
단항 연산자 오버로딩
// 단항 연산자 오버로딩 #include <iostream> using namespace std; class Cents { private: int _cents; public: Cents(int cents = 0) { _cents = cents; } int getCents() const { return _cents; } int& getCents() { return _cents; } Cents operator - () const { return Cents(-_cents); } bool operator ! () const { return (_cents == 0 ) ? true : false; } friend ostream& operator << (ostream& out, const Cents& cents) { out << cents._cents; return out; } }; int main() { Cents cents1(6); Cents cents2(0); int a = 3; cout << -cents1 << " " << cents2 << endl; // -6 0 cout << !cents1 << " " << !cents2 << endl; // 0 1 return 0; }
비교 연산자 오버로딩
#include <iostream> #include <vector> #include <algorithm> #include <random> using namespace std; class Cents { private: int _cents; public: Cents(int cents = 0) { _cents = cents; } int getCents() const { return _cents; } int& getCents() { return _cents; } bool operator < (const Cents& s2) { return this->_cents < s2._cents; } bool operator == (const Cents& s2) { return this->_cents == s2._cents; } friend ostream& operator << (ostream& out, const Cents& cents) { out << cents._cents; return out; } }; int main() { Cents s1(1), s2(1); if (s1 == s2) cout << "Equal" << endl; vector<Cents> arr(20); for (unsigned i = 0; i < 20; ++i) arr[i].getCents() = i; random_device rd; mt19937 g(rd()); shuffle(begin(arr), end(arr),g); // C++17 for (auto& e : arr) cout << e << " "; cout << endl; sort(begin(arr), end(arr)); for (auto& e : arr) cout << e << " "; cout << endl; return 0; }
증감 연산자 오버로딩
#include <iostream> using namespace std; class Digit { private: int _digit; public: Digit(int digit = 0) : _digit(digit) {} // prefix Digit& operator ++ () { ++_digit; return *this; } // postfix Digit operator ++ (int) { Digit temp(_digit); ++(*this); return temp; } friend ostream& operator << (ostream& out, const Digit& d) { out << d._digit; return out; } }; int main() { int a = 10; cout << ++a << endl; // 11 cout << a << endl; // 11 cout << a++ << endl; // 11 cout << a << endl; // 12 Digit d(5); cout << ++d << endl; // 6 cout << d++ << endl; // 6 cout << d << endl; // 7 return 0; }
첨자 연산자 오버로딩
#include <iostream> #include<cassert> using namespace std; class IntList { private: int _list[10] = {1,2,3,4,5,6,7,8,9,10}; public: int& operator [] (const int index) { assert(index >= 0); assert(index < 10); return _list[index]; } const int& operator [] (const int index) const { assert(index >= 0); assert(index < 10); return _list[index]; } void setItem(int index, int value) { _list[index] = value; } int getItem(int index) { return _list[index]; } int* getList() { return _list; } }; int main() { IntList my_list; my_list[3] = 10; cout << my_list[3] << endl; return 0; }
괄호 연산자 오버로딩
#include <iostream> using namespace std; class Accumulator { private: int _counter = 0; public: int operator () (int i) { return (_counter += i); } }; int main() { Accumulator acc; cout << acc(10) << endl; cout << acc(20) << endl; return 0; }
형변환 오버로딩
#include <iostream> using namespace std; class Cents { private: int _cents; public: Cents(int cents = 0) { _cents = cents; } int getCents() { return _cents; } void setCents(int cents) { _cents = cents; } operator int() { cout << "cast here" << endl; return _cents; } }; class Dollar { private: int _dollars = 0; public: Dollar(const int& input): _dollars(input) {} operator Cents() { return Cents(_dollars * 100); } }; void printInt(const int& value) { cout << value << endl; } int main() { Cents cents(7); int value = (int)cents; printInt(value); printInt(cents); printInt((Cents) Dollar(5)); return 0; }
'C++' 카테고리의 다른 글
C++) destructor (1) 2022.11.23 C++) 클래스 (0) 2022.11.23 C++) 함수 포인터 (0) 2022.11.22 C++) 인수 전달 방법 (0) 2022.11.22 C++) std::array, vector (0) 2022.11.22