nakka soft world !

String 클래스 만들기 본문

프로그래밍언어/C++

String 클래스 만들기

nakka 2017. 3. 19. 16:54
728x90

String 클래스 만들기 (making String)



1. 기능 정의

#include <iostream>

using namespace std;


int main()

{

String s1 = "hello";

cout << s1 << endl;

String s2 =  s1;

cout << s2 <<endl;


String s3 = "world";

s3 = s1;

cout << s3 << endl; //hello


s1 =  s1;

cout << s1 << enld; // hello

}


2. 생성/소멸 구현

#include <iostream>

using namespace std;


class String

{

private:

char* buff;

int size;

public:

String(const char* s)

{

size =  strlen(s);


buff = new char[size + 1];

strcpy(buff, s);

}

~String()

{

delete[] buff;

}

};

int main()

{

String s1 = "hello";   // == String s1("hello")

}


3. cout으로 출력하기

#include <iostream>

using namespace std;


class String

{

private:

char* buff;

int size;

public:

String(const char* s)

{

size =  strlen(s);


buff = new char[size + 1];

strcpy(buff, s);

}

~String()

{

delete[] buff;

}


friend ostream& operator<<(ostream& os, const String& s);

};

ostream& operator<<(ostream& os, const String& s)

{

os << s.buff;

return os;

}

int main()

{

String s1 = "hello";   // == String s1("hello")


cout << s1 << endl; //    cout.operator<<(String )  //cout을 수정해야함.

// operator<<(ostream& os, const String& s) // 일반 함수 추가하면 됨.

}



4. 복사 생성자

#include <iostream>

using namespace std;


class String

{

private:

char* buff;

int size;

public:

String(const char* s)

{

size =  strlen(s);


buff = new char[size + 1];

strcpy(buff, s);

}

~String(){ delete[] buff; }


//복사 생성자

String(const String& s) : size(s.size)

{

buff = new char[strlen(s.buff) +1];

strcpy(buff, s.buff);

}


friend ostream& operator<<(ostream& os, const String& s);

};

ostream& operator<<(ostream& os, const String& s)

{

os << s.buff;

return os;

}

int main()

{

String s1 = "hello";   // == String s1("hello")


cout << s1 << endl; //    cout.operator<<(String )  //cout을 수정해야함.

// operator<<(ostream& os, const String& s) // 일반 함수 추가하면 됨.

String s2 = s1;  // 복사 생성자. default 복사 생성자는 얕은 복사를 하게 됨.

cout << s2 << endl;

}



5. 대입연산자

#include <iostream>

using namespace std;


class String

{

private:

char* buff;

int size;

public:

String(const char* s)

{

size =  strlen(s);


buff = new char[size + 1];

strcpy(buff, s);

}

~String(){ delete[] buff; }


//복사 생성자

String(const String& s) : size(s.size)

{

buff = new char[strlen(s.buff) +1];

strcpy(buff, s.buff);

}


//대입연산자

String& operator=(const String& s)

{

size =  s.size;

// 자신의 버퍼를 지우고,

delete[] buff;  // world 메모리 삭제


// 새로운 버퍼 할당

buff = new char[strlen(s.buff) + 1];

strcpy(buff, s.buff);


return *this;

}


friend ostream& operator<<(ostream& os, const String& s);

};

ostream& operator<<(ostream& os, const String& s)

{

os << s.buff;

return os;

}

int main()

{

String s1 = "hello";   // == String s1("hello")


cout << s1 << endl; //    cout.operator<<(String )  //cout을 수정해야함.

// operator<<(ostream& os, const String& s) // 일반 함수 추가하면 됨.

String s2 = s1;  // 복사 생성자. default 복사 생성자는 얕은 복사를 하게 됨.

cout << s2 << endl;


String s3 = "world"; // 생성후에

s3 = s1; 대입연산자


cout << s3 << endl; //hello

}



5. 대입연산자 주의사항

#include <iostream>

using namespace std;


class String

{

private:

char* buff;

int size;

public:

String(const char* s)

{

size =  strlen(s);


buff = new char[size + 1];

strcpy(buff, s);

}

~String(){ delete[] buff; }


//복사 생성자

String(const String& s) : size(s.size)

{

buff = new char[strlen(s.buff) +1];

strcpy(buff, s.buff);

}


//대입연산자

String& operator=(const String& s)

{

// 자신과 대입을 비교. 대입연산자 초입에는 항상 반드시 있어야 함.

if (&s = this) return *this;


size =  s.size;

// 자신의 버퍼를 지우고,

delete[] buff;  // world 메모리 삭제


// 새로운 버퍼 할당

buff = new char[strlen(s.buff) + 1];

strcpy(buff, s.buff);


return *this;

}


friend ostream& operator<<(ostream& os, const String& s);

};

ostream& operator<<(ostream& os, const String& s)

{

os << s.buff;

return os;

}

int main()

{

String s1 = "hello";   // == String s1("hello")


cout << s1 << endl; //    cout.operator<<(String )  //cout을 수정해야함.

// operator<<(ostream& os, const String& s) // 일반 함수 추가하면 됨.

String s2 = s1;  // 복사 생성자. default 복사 생성자는 얕은 복사를 하게 됨.

cout << s2 << endl;


String s3 = "world"; // 생성후에

s3 = s1; 대입연산자


cout << s3 << endl; //hello


s1 = s1;  // 대입 연산시에 src와 dest가 동일하기에 버퍼를 지운 뒤 해당 버퍼에 접근하면서 문제가 발생함.

// src와 dest가 동일하면 아무짓도 안하게 해줌

cout << s1 << endl;  // "hello"

}


728x90

'프로그래밍언어 > C++' 카테고리의 다른 글

컨테이너 (Container)  (0) 2017.03.19
STL (Standard Template Library)  (0) 2017.03.19
대입연산자 (assignment operator)  (0) 2017.03.19
스마트 포인터 (Smart Pointer)  (0) 2017.03.19
함수 객체(function object, fonctor)  (0) 2017.03.19
Comments