본문 바로가기
Programming/C&C++

[C++] String 클래스 직접 구현

String 클래스를 직접 만들어 보자!






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
#pragma warning(disable:4996)
#include <cstring>
#include<iostream>
using namespace std;
 
class MyString {
    char *str; //문자 메모리 공간 할당
    int len; //문자열 길이 저장 변수
public :
    MyString() {
        len = 0;
        str = NULL;
    }
 
    //소멸자
    ~MyString() { delete[] str; }
 
    //1번
    MyString(const char *p) {
        //문자열 길이 구하기
        len = strlen(p) + 1//길이를 알려주는 함수
        
        //메모리 할당
        str = new char[len];
 
        //문자열 복사
        strcpy(str, p); //복사 함수 strcpy(복사할곳, 복사할것)
    }
 
    MyString(int x, char p) {
        this->len = x + 1;
        this->str = new char[this->len]; //동적 할당
        for (int i = 0; i < x; i++) {
            str[i] = p;
        }
        str[x] = '\0'//안넣어주면 쓰레기값
    }
 
    
    MyString(const MyString &cms) {
        len = strlen(cms.str) + 1;
        str = new char[len];
        strcpy(str, cms.str);
    }
 
    MyString& operator+=(const char *p) {
        
        char *str2; //새로 옮겨 담을놈
        int len2;
        len2 = strlen(p) ;
        str2 = new char[len + len2];
        
        strcpy(str2, str);
        strcat(str2, p);
        delete[] str;
        str= new char[len + len2];
 
        strcpy(str, str2);
        delete[] str2;
        return *this;
    }
 
    MyString& operator=(const char *p) {
        delete[] str;
        len = strlen(p) + 1;
        str = new char[len];
        strcpy(str, p);
        return *this;
    }
 
 
    char& operator[](int i) { //i 직접 받아서 
        return this->str[i];
    }
 
    MyString operator+(const MyString &pms) {
        MyString tmp; //four에 저장
        int len2 = this->len + strlen(pms.str);
        tmp = new char[len2];
 
        strcpy(tmp.str, this->str);
        strcat(tmp.str, pms.str);
        return tmp;
    }
 
    MyString& operator=(const MyString &cms) {
        delete[] str;
        len = strlen(cms.str) + 1;
        str = new char[len];
        strcpy(str, cms.str);
        return *this;
    }
 
    MyString(char *c, int i) {
        char *tmp = new char[i + 1];
        for (int j = 0; j < i; j++) {
            tmp[j] = c[j];
        }
        tmp[i] = '\0';
        str = tmp; //얕은 복사
    }
 
    MyString(char *x, char *y) {
        int len2 = y - x + 1//주소
        str = new char[len2];
        for (int i = 0; i < len2; i++) {
            str[i] = x[i];
        }
        str[len2 - 1= '\0';
    }
 
    friend ostream& operator<<(ostream &coutconst MyString &ms);
    friend istream& operator>>(istream &cin, MyString &ms);
};
 
ostream& operator<<(ostream &coutconst MyString &ms) {
    cout << ms.str;
    return cout;
}
 
istream& operator>>(istream &cin, MyString &ms) {
    char *tmp = new char[10000];//받고
    cin >> tmp;
    ms.len = strlen(tmp) + 1;
    ms.str = new char[ms.len]; //마지막에
 
    //값을 복사
    strcpy(ms.str, tmp);
    return cin;
}
 
void main(){
    //1번
    MyString one("lottery winner!"); //생성자함수호출
    cout << one << endl//출력연산자함수호출
    //2번
    MyString two(20'$'); //생성자함수호출
    cout << two << endl//출력연산자함수호출
    //3번
    MyString three(one); //복사생성자호출
    cout << three << endl//출력연산자함수호출
    //4번
    one += "oops"//+=연산자함수호출 ( strcat함수 )
    cout << one << endl;//문자열결합 
    //5번
    two = "sorry!that was"//대입연산자함수 호출 
    cout << two << endl;
    //6번
    three[0= 'p';
    cout << three << endl;
    //7번
    MyString four;
    four = two + three; 
    cout << four << endl;
    //8번
    char alls[] = "all's well that ends well";
    MyString five(alls, 20); 
    cout << five << "!\n";
    //9번
    MyString six(alls + 6, alls + 10);  
    cout << six << ",";
    //10번
    MyString seven(&five[6], &five[10]);  
    cout << seven << "...\n";
    //11번
    MyString eight;
    cout << "문자열 입력하세요 :";
    cin >> eight;  // >>연산자호출
    cout << "입력한 문자열은 \"" << eight << "\" 입니다 " << endl;
}
cs


'Programming > C&C++' 카테고리의 다른 글

[C++] STACK / QUEUE  (3) 2018.04.15
[C++] 클래스 상속 관계  (0) 2018.04.15
[C++] POINTER ARRAY  (0) 2018.04.15
[C++] DYNAMIC MEMORY  (1) 2018.04.15
[C++] 오버로딩이 불가능 연산자  (2) 2018.04.15