VioletaBabel

7일 : 연산자 오버로딩 본문

BCA/1. C,C++,C#
7일 : 연산자 오버로딩
Beabletoet 2018. 2. 13. 10:23
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
//CNumber.cpp
#include "CNumber.h"
 
CNumber::CNumber()
{
}
 
 
CNumber::~CNumber()
{
}
 
CNumber CNumber::operator+(const CNumber & des)
{
    CNumber result;
    result.num = this->num + des.num;
    return result;
}
 
CNumber CNumber::operator+(int des)
{
    CNumber result;
    result.num = this->num + des;
    return result;
}
 
CNumber CNumber::operator-(const CNumber & des)
{
    CNumber result;
    result.num = this->num - des.num;
    return result;
}
 
CNumber CNumber::operator-(int des)
{
    CNumber result;
    result.num = this->num - des;
    return result;
}
 
CNumber CNumber::operator/(const CNumber & des)
{
    CNumber result;
    result.num = this->num / des.num;
    return result;
}
 
CNumber CNumber::operator/(int des)
{
    CNumber result;
    result.num = this->num / des;
    return result;
}
 
CNumber CNumber::operator*(const CNumber & des)
{
    CNumber result;
    result.num = this->num * des.num;
    return result;
}
 
CNumber CNumber::operator*(int des)
{
    CNumber result;
    result.num = this->num * des;
    return result;
}
 
CNumber CNumber::operator+=(const CNumber & des)
{
    this->num += des.num;
    return *this;
}
 
CNumber CNumber::operator+=(int des)
{
    this->num += des;
    return *this;
}
 
CNumber CNumber::operator-=(const CNumber & des)
{
    this->num -= des.num;
    return *this;
}
 
CNumber CNumber::operator-=(int des)
{
    this->num -= des;
    return *this;
}
 
CNumber CNumber::operator/=(const CNumber & des)
{
    this->num /= des.num;
    return *this;
}
 
CNumber CNumber::operator/=(int des)
{
    this->num /= des;
    return *this;
}
 
CNumber CNumber::operator*=(const CNumber & des)
{
    this->num *= des.num;
    return *this;
}
 
CNumber CNumber::operator*=(int des)
{
    this->num *= des;
    return *this;
}
 
CNumber CNumber::operator=(const CNumber & des)
{
    this->num = des.num;
    return *this;
}
 
CNumber CNumber::operator=(int des)
{
    this->num = des;
    return *this;
}
 
bool CNumber::operator==(const CNumber & des)
{
    return (this->num == des.num);
}
 
bool CNumber::operator==(int des)
{
    return (this->num == des);
}
 
cs


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
//CNumber.h
#pragma once
class CNumber
{
public:
    CNumber();
    ~CNumber();
    CNumber operator+(const CNumber& des);
    CNumber operator+(int des);
    CNumber operator-(const CNumber& des);
    CNumber operator-(int des);
    CNumber operator/(const CNumber& des);
    CNumber operator/(int des);
    CNumber operator*(const CNumber& des);
    CNumber operator*(int des);
    CNumber operator+=(const CNumber& des);
    CNumber operator+=(int des);
    CNumber operator-=(const CNumber& des);
    CNumber operator-=(int des);
    CNumber operator/=(const CNumber& des);
    CNumber operator/=(int des);
    CNumber operator*=(const CNumber& des);
    CNumber operator*=(int des);
    CNumber operator=(const CNumber& des);
    CNumber operator=(int des);
    bool operator==(const CNumber& des);
    bool operator==(int des);
    void set(int n) { num = n; }
    int get() { return num; }
    operator int() const
    {//이러면 get 없이도 cout<< 방식으로 num 값 출력 가능
        return num;
    }
private:
    int num;
};
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Main.cpp
#include<iostream>
#include "CNumber.h"
using namespace std;
int main()
{
    CNumber A, B;
    A.set(10);
    B.set(20);
    A += 10;
    cout << A << endl;
    B += A;
    cout << B << endl;
    A -= 10;
    cout << A << endl;
    B -= A;
    cout << B << endl;
}
cs


--

문자열을 담은 클래스를 이용한 연산자 오버로딩 예제


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
//CMyStr.h
#include<iostream>
using namespace std;
#pragma once
class CMyStr
{
public:
    CMyStr();
    CMyStr(CMyStr &c);
    CMyStr(const char* c);
    ~CMyStr();
    void say();
    CMyStr operator+(const char *c);
    CMyStr operator+(CMyStr c);
    CMyStr operator+=(const char *c);
    CMyStr operator+=(CMyStr c);
    bool operator==(const char *c);
    bool operator==(CMyStr c);
    bool operator!=(char *c);
    bool operator!=(CMyStr c);
    void erase(int st, int len);
    int length();
    
    friend CMyStr operator+(const char *c1, CMyStr c2);
    friend bool operator==(const char *c1, CMyStr c2);
    friend bool operator!=(const char *c1, CMyStr c2);
private:
    char* text;
};
cs


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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//CMyStr.cpp
#include "CMyStr.h"
 
CMyStr::CMyStr()
{
}
CMyStr::CMyStr(CMyStr &c)
{
    int cl = c.length() + 1;
    this->text = new char[cl];
    for (int i = 0; i < cl; ++i)
        this->text[i] = c.text[i];    
}
 
CMyStr::CMyStr(const char *c)
{
    int clen;
    for (clen = 0*(c + clen) != '\0'++clen);
    this->text = new char[clen + 1];
    for (int i = 0; i <= clen; ++i)
        this->text[i] = c[i];
}
 
CMyStr::~CMyStr()
{
}
 
void CMyStr::say()
{
    int len = this->length();
    for (int i = 0; i < len; ++i)
        cout << this->text[i];
    cout << endl;
}
 
CMyStr CMyStr::operator+(const char * c)
{
    CMyStr r;
    int cLen, tLen = this->length(), i;
    for (cLen = 0*(c + cLen) != '\0'++cLen);
    r.text = new char[cLen + tLen + 1];
    for (i = 0; i < tLen; ++i)
        r.text[i] = this->text[i];
    for (; i - tLen <= cLen; ++i)
        r.text[i] = c[i - tLen];
    return r;
}
 
CMyStr CMyStr::operator+(CMyStr c)
{
    CMyStr r;
    int cLen = c.length(), tLen = this->length(), i;
    r.text = new char[cLen + tLen + 1];
    for (i = 0; i < tLen; ++i)
        r.text[i] = this->text[i];
    for (; i - tLen <= cLen; ++i)
        r.text[i] = c.text[i - tLen];
    return r;
}
 
CMyStr operator+(const char * c1, CMyStr c2)
{
    CMyStr r;
    int c1Len, c2Len = c2.length(), i;
    for (c1Len = 0*(c1 + c1Len) != '\0'++c1Len);
    r.text = new char[c1Len + c2Len + 1];
    for (i = 0; i < c1Len; ++i)
        r.text[i] = c1[i];
    for (; i - c1Len <= c2Len; ++i)
        r.text[i] = c2.text[i - c1Len];
    return r;
}
 
 
CMyStr CMyStr::operator+=(const char * c)
{
    CMyStr r(this->text);
    int cLen, tLen = this->length(), i;
    for (cLen = 0*(c + cLen) != '\0'++cLen);
    delete[] this->text;
    this->text = new char[cLen + tLen + 1];
    for (i = 0; i < tLen; ++i)
        this->text[i] = r.text[i];
    for (; i - tLen <= cLen; ++i)
        this->text[i] = c[i - tLen];
    return *this;
}
 
CMyStr CMyStr::operator+=(CMyStr c)
{
    CMyStr r(this->text);
    int cLen = c.length(), tLen = this->length(), i;
    delete[] this->text;
    this->text = new char[cLen + tLen + 1];
    for (i = 0; i < tLen; ++i)
        this->text[i] = r.text[i];
    for (; i - tLen <= cLen; ++i)
        this->text[i] = c.text[i - tLen];
    return *this;
}
 
bool CMyStr::operator==(const char * c)
{
    int cl, tl = this->length();
    for (cl = 0*(c + cl) != '\0'++cl);
    if (cl != tl)
        return 0;
    else
        for (int i = 0; i < cl; ++i)
            if (c[i] != this->text[i])
                return 0;
    return 1;
}
 
bool CMyStr::operator==(CMyStr c)
{
    int cl = c.length(), tl = this->length();
    if (cl != tl)
        return 0;
    else
        for (int i = 0; i < cl; ++i)
            if (c.text[i] != this->text[i])
                return 0;
    return 1;
}
 
bool operator==(const char * c1, CMyStr c2)
{
    int c1l, c2l = c2.length();
    for (c1l = 0*(c1 + c1l) != '\0'++c1l);
    if (c1l != c2l)
        return 0;
    else
        for (int i = 0; i < c1l; ++i)
            if (c1[i] != c2.text[i])
                return 0;
    return 1;
}
bool operator!=(const char * c1, CMyStr c2)
{
    int c1l, c2l = c2.length();
    for (c1l = 0*(c1 + c1l) != '\0'++c1l);
    if (c1l != c2l)
        return 1;
    else
        for (int i = 0; i < c1l; ++i)
            if (c1[i] != c2.text[i])
                return 1;
    return 0;
}
bool CMyStr::operator!=(char * c)
{
    int cl, tl = this->length();
    for (cl = 0*(c + cl) != '\0'++cl);
    if (cl != tl)
        return 1;
    else
        for (int i = 0; i < cl; ++i)
            if (c[i] != this->text[i])
                return 1;
    return 0;
}
 
bool CMyStr::operator!=(CMyStr c)
{
    int cl = c.length(), tl = this->length();
    if (cl != tl)
        return 1;
    else
        for (int i = 0; i < cl; ++i)
            if (c.text[i] != this->text[i])
                return 1;
    return 0;
}
 
void CMyStr::erase(int st, int len)
{
    for (int i = 0; i < len; ++i)
        this->text[i + st] = this->text[i + st + len];
}
 
int CMyStr::length()
{
    int i;
    for (i = 0this->text[i] != '\0'++i);
    return i;
}
cs


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
//Main.cpp
#include<iostream>
#include "CMyStr.h"
using namespace std;
int main()
{
    CMyStr a("hi");
    cout << a.length() << endl;
    CMyStr b(a);
    cout << b.length() << endl;
    CMyStr c("hey");
    c = a + "man";
    c.say();
    c += a;
    c.say();
    c += "yeah";
    c.say();
    cout << (a == "hw")<<endl;
    c.erase(35);
    c.say();
 
    CMyStr dsss;
    dsss = "ads" + a;
    dsss.say();
    cout << ("hi" != a) << endl;
}
cs


Comments