#pragma warning(disable : 4996) #include <iostream> #include <cstring> using namespace std; class String { private: char* _chars; public: String(const char* chars) : _chars(new char[strlen(chars) + 1]) { strcpy(_chars, chars); } bool operator==(const String& s) const { return strcmp(_chars, s._chars) == 0; } bool operator!=(const String& s) const { return !(*this == s); } bool operator<(const String& s) const { return strcmp(_chars, s._chars) < 0; } bool operator>(const String& s) const { return strcmp(_chars, s._chars) > 0; } bool operator>=(const String& s) const { return !(*this < s); } bool operator<=(const String& s) const { return !(*this > s); } void print() { cout << _chars << endl; } }; int main() { // ==, !=, <, >, <=, >=, <=> String s0("abc"); String s1("abd"); if (s0 != s1) cout << "Eq" << endl; if (s0 < s1) cout << "Less" << endl; } | cs |
'::public > C++' 카테고리의 다른 글
비트 연산자 오버로딩 (0) | 2020.11.23 |
---|---|
논리 연산자 오버로딩 (0) | 2020.11.23 |
연산자 오버로딩 (0) | 2020.09.15 |
extern (0) | 2020.09.01 |
공용체(union) (0) | 2020.07.13 |