반응형
#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하는데 에러가 떠서 좀 고생함

반응형

+ Recent posts