반응형
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
private:
char* str;
int lenght;
int capacity;
public:
String() : lenght(0), capacity(0) {}
String(const char* _str)
: lenght(strlen(_str)),
capacity(lenght * 2 + 1)
{
str = new char[capacity];
strcpy_s(str, lenght + 1, _str);
}
String(const String& _str)
: lenght(strlen(_str.str)),
capacity(lenght * 2 + 1)
{
str = new char[capacity];
strcpy_s(str, lenght + 1, _str.str);
}
~String()
{
delete[] str;
}
int Size() { return lenght; }
int Capacity() { return capacity; }
bool operator==(const char* _dest)
{
return (strcmp(this->str, _dest) == 0);
}
bool operator==(const String& _dest)
{
return (strcmp(this->str, _dest.str) == 0);
}
void operator=(const char* _str)
{
int len = strlen(_str);
if (len + 1 >= capacity)
{
delete[] str;
capacity = len * 2;
str = new char[capacity];
}
strcpy_s(str, len + 1, _str);
lenght = len;
}
void operator=(const String& _str)
{
int len = _str.lenght;
if (len + 1 >= capacity)
{
delete[] str;
capacity = len * 2;
str = new char[capacity];
}
strcpy_s(str, len + 1, _str.str);
lenght = len;
}
String& operator+=(const char* _str)
{
int len = strlen(_str);
if (lenght + len + 1 > capacity)
{
capacity = (lenght + len) * 2 + 1;
char* tempStr = new char[capacity];
strcpy_s(tempStr, capacity, str);
delete[] str; // 기존 메모리 해제
str = tempStr; // 새로운 메모리로 교체
}
strcat_s(str, capacity, _str); // str에 _str 추가
lenght += len;
return *this;
}
String& operator+=(const String& _str)
{
int len = _str.lenght;
if (lenght + len + 1 > capacity)
{
capacity = (lenght + len) * 2 + 1;
char* tempStr = new char[capacity];
strcpy_s(tempStr, capacity, str);
delete[] str; // 기존 메모리 해제
str = tempStr; // 새로운 메모리로 교체
}
strcat_s(str, capacity, _str.str); // str에 _str 추가
lenght += len;
return *this;
}
String operator+(const char* _str)
{
char* string = new char[lenght + strlen(_str) + 1];
strcpy_s(string, lenght + 1, str);
strcat_s(string, lenght + strlen(_str) + 1, _str);
String temp = string;
delete[] string;
return temp;
}
String operator+(String& _str)
{
char* string = new char[lenght + _str.lenght + 1];
strcpy_s(string, lenght + 1, str);
strcat_s(string, lenght + strlen(_str.str) + 1, _str.str);
String temp = string;
delete[] string;
return temp;
}
char& operator[](int _index)
{
if (_index > lenght) assert(false && "IndexError");
return str[_index];
}
friend ostream& operator <<(ostream& _outStream, const String& _str);
};
ostream& operator <<(ostream& _outStream, const String& _str) {
cout << _str.str;
return _outStream;
}
int main()
{
String s1 = "abc";
cout << "s1 : " << s1 << '\n';
String s2 = "def";
cout << "s2 : " << s2 << '\n';
String s3 = s1 + s2;
cout << "s3 : " << s3 << '\n';
String s4 = "";
s4 += s3;
cout << "s4 : " << s4 << '\n';
String s5;
s5 = s4 + "qwe";
cout << "s5 : " << s5 << '\n' << '\n';
cout << "s1 == s2 : " << (s1 == s2) << '\n';
cout << "s3 == s4 : " << (s3 == s4) << '\n';
cout << "s1 == abc : " << (s1 == "abc") << '\n';
cout << "s1[0] ~ s1[3] : ";
for (int i = 0; i < s1.Size(); i++)
{
cout << s1[i];
if (i != s1.Size() - 1) cout << ", ";
}
return 0;
}
과제하는겸 string클래스를 구현해보았다.
메모리 할당 delete하는데 에러가 떠서 좀 고생함
반응형
'프로그래밍 > C++' 카테고리의 다른 글
[C++] is_base_of<> 특정 클래스를 상속 받았는지 확인하는 함수 (2) | 2024.09.08 |
---|---|
[C++] 생성자 상속(C++11) (1) | 2024.09.06 |
[C++] DFS 구현해보기 (0) | 2024.07.10 |
[C++] operator 연산자 (0) | 2024.06.14 |
[C++] 중첩 클래스를 알아보자.araboza (0) | 2024.06.12 |