티스토리 뷰

항상 써오던 string 클래스는 문자열간 +, += 연산 등이 가능한데 앞서 배운 연산자 오버로딩이 되어 있단 것을 알 수 있다.

 

string 클래스를 비슷하게 구현해본 StringClass.cpp

#include <iostream>
#include <cstdio>
using namespace std;

class String
{
private:
    char * str;

public:
    String()
    {
        str = NULL;
    }
    // 생성자
    String(const char *_str)
    {
        str = new char[strlen(_str) + 1];
        strcpy(str, _str);
    }
    // 복사 생성자
    String(const String& ref)
    {
        str = new char[strlen(ref.str)];
        strcpy(str, ref.str);
    }
    // = 연산자 오버로딩
    String& operator= (const String& ref)
    {
        if(str != NULL) delete []str;
        str = new char[strlen(ref.str)];
        strcpy(str, ref.str);
        return *this;
    }

    // + 연산자 오버로딩
    String operator+ (const String& ref)
    {
        char * tmp = new char[strlen(str) + strlen(ref.str) - 1];
        strcpy(tmp, str);
        strcat(tmp, ref.str);
        String tmpStr(tmp);
        delete []tmp;
        return tmpStr;
    }

    // += 연산자 오버로딩
    String& operator+= (const String& ref)
    {
        char * tmp = new char[strlen(str) + strlen(ref.str) - 1];
        strcpy(tmp, str);
        strcat(tmp, ref.str);
        if(str != NULL) delete []str;
        str = tmp;
        return *this;
    }

    // == 연산자 오버로딩
    bool operator== (const String& ref)
    {
        if(strlen(str) != strlen(ref.str)) return false;
        for(int i = 0; i < strlen(str); i++)
        {
            if(str[i] != ref.str[i]) return false;
        }
        return true;
    }

    ~String()
    {
        if(str != NULL)
            delete []str;

    }

    friend ostream& operator<< (ostream& os, const String& ref);
    friend istream& operator>> (istream& is, String& ref);
};

// << 연산자 오버로딩
ostream& operator<< (ostream& os, const String& ref)
{
    os << ref.str;
    return os;
}

// >> 연산자 오버로딩
istream& operator>> (istream& is, String& ref)
{
    char str[100];
    is >> str;
    ref = String(str);
    return is;
}

int main()
{
    String str1 = "I like ";
    String str2 = "string Class";
    String str3 = str1 + str2;

    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;

    str1 += str2;
    if(str1 == str3)
        cout << "same string" << endl;
    else
        cout << "diff string" << endl;

    String str4;
    cout << "Enter String: ";
    cin >> str4;
    cout << "Your String: " << str4 << endl;

    return 0;
}

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
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
글 보관함