연산자 오버로딩을 통해 문자열 및 클래스간의 덧셈, 비교 구현하여 테스트하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #pragma once class CMyStr { public: CMyStr(); ~CMyStr(); CMyStr(const CMyStr & str); CMyStr(char* t); CMyStr operator+ (const CMyStr & str); CMyStr operator+ (char *t); void operator+= (const CMyStr & str); void operator+= (char *t); int operator== (const CMyStr & str); int operator== (char *t); int operator!= (const CMyStr & str); int operator!= (char *t); void erase(int start, int length); int Length() const; int Length(char *t); void gettext(); private: char* text; }; | cs |
헤더
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #include "stdafx.h" #include "CMyStr.h" #include <iostream> using namespace std; CMyStr::CMyStr() { } CMyStr::~CMyStr() { } CMyStr::CMyStr(const CMyStr & str) { // 입력받은 클래스의 text값을 현재 클래스의 text에 복사 int i = str.Length(); text = new char[i+1]; for (int j = 0; j < i; j++) text[j] = str.text[j]; text[i] = 0; } CMyStr::CMyStr(char * t) { int i = Length(t); text = new char[i+1]; for (int j = 0; j < i; j++) text[j] = t[j]; text[i] = 0; } CMyStr CMyStr::operator+(const CMyStr & str) { int i = str.Length(); int k = Length(); CMyStr result; result.text = new char[k + i + 1]; // 기존값은 그대로 복사 for (int h = 0; h<k; h++) result.text[h] = text[h]; // 이후 새값 붙여줌 for (int j = 0; j<i; j++) result.text[k + j] = str.text[j]; // 마지막은 널값 result.text[k + i] = 0; return result; } CMyStr CMyStr::operator+(char * t) { int i = Length(t); int k = Length(); CMyStr result; result.text = new char[k + i + 1]; for(int h=0; h<k; h++) result.text[h] = text[h]; for (int j = 0; j<i; j++) result.text[k + j] = t[j]; result.text[k + i] = 0; return result; } void CMyStr::operator+=(const CMyStr & str) { int i = str.Length(); int k = Length(); CMyStr temp; temp.text = new char[i + k + 1]; for (int h = 0; h<k; h++) temp.text[h] = text[h]; for (int j = 0; j<i; j++) temp.text[k + j] = str.text[j]; temp.text[k + i] = 0; delete text; text = temp.text; } void CMyStr::operator+=(char * t) { int i = Length(t); int k = Length(); CMyStr temp; temp.text = new char[i + k + 1]; for (int h = 0; h<k; h++) temp.text[h] = text[h]; for (int j=0; j<i; j++) temp.text[k + j] = t[j]; temp.text[k + i] = 0; delete text; text = temp.text; } int CMyStr::operator==(const CMyStr & str) { int i = str.Length(); int k = Length(); int check = 1; // 길이가 같을때만 비교 if (i == k) { for (int j = 0; j < i; j++) { // 다른게 하나라도 있으면 0 if (text[j] != str.text[j]) check = 0; } } // 길이가 다르면 0 else check = 0; return check; } int CMyStr::operator==(char * t) { int i = Length(t); int k = Length(); int check = 1; if (i == k) { for (int j = 0; j < i; j++) { if (text[j] != t[j]) check = 0; } } else check = 0; return check; } int CMyStr::operator!=(const CMyStr & str) { int i = str.Length(); int k = Length(); int check = 0; if (i == k) { for (int j = 0; j < i; j++) { if (text[j] != str.text[j]) check = 1; } } else check = 1; return check; } int CMyStr::operator!=(char * t) { int i = Length(t); int k = Length(); int check = 0; if (i == k) { for (int j = 0; j < i; j++) { if (text[j] != t[j]) check = 1; } } else check = 1; return check; } void CMyStr::erase(int start, int length) { int k = Length(); CMyStr temp; temp.text = new char[k-length+1]; int count=0; // 자르기전은 그대로 복사 for (int i = 0; i < start; i++) { temp.text[i] = text[i]; count++; } // 지울부분 이후부터 문자열 붙임 for (int j = start+length; j < k; j++) { temp.text[count] = text[j]; count++; } temp.text[count] = 0; delete text; text = temp.text; } int CMyStr::Length() const { int num = 0; while (text[num]) num++; return num; } int CMyStr::Length(char * t) { int num = 0; while (t[num]) num++; return num; } void CMyStr::gettext() { cout << text << endl; } | csㅊ |
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include "stdafx.h" #include "CMyStr.h" #include <iostream> using namespace std; int main() { char a[10] = "apple"; char b[10] = "banana"; char l[10] = "lemon"; // char 넣기 CMyStr apple = a; apple.gettext(); CMyStr banana = b; CMyStr lemon = l; // CMyStr 넣기(복사) CMyStr apple2 = apple; apple2.gettext(); cout << "\n"; // 연산자 오버로딩 + char CMyStr c = apple + b; c.gettext(); // 연산자 오버로딩 + CMyStr CMyStr d = banana + lemon; d.gettext(); cout << "\n"; apple += l; apple.gettext(); apple2 += banana; apple2.gettext(); cout << "\n"; apple2.erase(3, 4); apple2.gettext(); cout << "\n"; cout << (apple == apple2) << endl; cout << (apple2 == a) << endl; cout << "\n"; cout << (apple != apple2) << endl; cout << (apple != a) << endl; return 0; } | cs |
테스트