๐ฅ ๋ค์ด๊ฐ๊ธฐ ์ ์
ํด๋์ค์ ๋ฉค๋ฒํจ์๋ก ๊ตฌํํ ๊ฒฝ์ฐ, ๋งค๊ฐ๋ณ์๊ฐ ํ๊ฐ๋ง ํ์
์ธ๋ถํจ์๋ก ๊ตฌํํ๊ณ ํด๋์ค์ ํ๋ ๋ ํจ์๋ก ์ ์ธํ ๊ฒฝ์ฐ, ๋งค๊ฐ๋ณ์๊ฐ 2๊ฐ ํ์
-> 100%๋ ์๋๋ฐ ์์ ๋ ์ฐ์ต๋ฌธ์ ์์์ ๊ทธ๋ผ
์๊ธฐ ์์ ์ ํด๋์ค ๋ฆฌํดํ ๋ ๋ฐํํ ํด๋์ค&์ผ๋ก ํด์ฃผ๊ณ ๋ฐํ์ this*๋ก ํ ๊ฒ!
6๋ฒ
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Matrix {
int arr[4];
public:
Matrix() {};
Matrix(int a, int b, int c, int d) {
arr[0] = a;
arr[1] = b;
arr[2] = c;
arr[3] = d;
};
void show();
Matrix operator+(Matrix m);
Matrix& operator+=(Matrix m);
bool operator==(Matrix m);
};
void Matrix::show() {
cout << "Matrix = { ";
for (int i = 0; i < 4; i++) {
cout << this->arr[i] << " ";
}
cout << "}" << endl;
}
Matrix Matrix::operator+(Matrix m) {
Matrix matrix;
for (int i = 0; i < 4; i++) {
matrix.arr[i] = this->arr[i] + m.arr[i];
}
return matrix;
}
Matrix& Matrix::operator+=(Matrix m) {
for (int i = 0; i < 4; i++) {
arr[i] += m.arr[i];
}
return *this;
}
bool Matrix::operator==(Matrix m) {
bool b = true;
for (int i = 0; i < 4; i++) {
if (this->arr[i] != m.arr[i]) {
b = false;
}
}
return b;
}
int main() {
Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
c = a + b;
a += b;
a.show();
b.show();
c.show();
if (a == c) {
cout << "a and c are the same" << endl;
}
}
(2)
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Matrix {
int arr[4];
public:
Matrix() {};
Matrix(int a, int b, int c, int d) {
arr[0] = a;
arr[1] = b;
arr[2] = c;
arr[3] = d;
};
void show();
friend Matrix operator+(Matrix m1,Matrix m2);
friend Matrix& operator+=(Matrix m1,Matrix m2);
friend bool operator==(Matrix m1,Matrix m2);
};
void Matrix::show() {
cout << "Matrix = { ";
for (int i = 0; i < 4; i++) {
cout << this->arr[i] << " ";
}
cout << "}" << endl;
}
Matrix operator+(Matrix m1,Matrix m2) {
Matrix matrix;
for (int i = 0; i < 4; i++) {
matrix.arr[i] = m1.arr[i] + m2.arr[i];
}
return matrix;
}
Matrix& operator+=(Matrix m1,Matrix m2) {
for (int i = 0; i < 4; i++) {
m1.arr[i] += m2.arr[i];
}
return m1;
}
bool operator==(Matrix m1,Matrix m2) {
bool b = true;
for (int i = 0; i < 4; i++) {
if (m1.arr[i] != m2.arr[i]) {
b = false;
}
}
return b;
}
int main() {
Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
c = a + b;
a += b;
a.show();
b.show();
c.show();
if (a == c) {
cout << "a and c are the same" << endl;
}
}
11๋ฒ
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Stack {
int size;
int* mem;//๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น, ์คํ๊ณต๊ฐ
int tos;
public:
Stack(int size = 10) {
this->size = size;
mem = new int[size];
tos = -1; //์คํ์ด ๋น์ด์์
}
~Stack() {
delete[] mem;
}
Stack& operator<<(int n);
Stack operator>>(int& n);
bool operator!();// operator! ์ ๋งค๊ฐ๋ณ์ ๋น์๋์ผํจ
};
Stack& Stack::operator<<(int n) {
if (tos == size - 1) {
return *this;
}
tos++;
mem[tos] = n;
return *this;
}
Stack Stack::operator>>(int& n) {
if (tos == -1) {
return *this;
}
n = mem[tos];
tos--;
return n;
}
bool Stack::operator!() {
if (tos == -1) {
return true;
}
else {
return false;
}
}
int main() {
Stack stack;
stack << 3 << 5 << 10;
while (true) {
if (!stack) {
break;
}
int x;
stack >> x;
cout << x << ' ';
}
cout << endl;
}
์ ์์ operator>> ์ ๋ฐํํ์ Stack&์ผ๋ก ์ก์์ฃผ๋ ๊ฒ
๊ทธ๋ฌ๋ฉด *this๋ฅผ ๋ฑ์ด์ผ ํ๋๋ฐ *this๋ ์๊ธฐ ์์ ์ ํด๋์ค ๊ทธ ์์ฒด์๋ฏธ, ๊ฒฐ๊ตญ Stack&๋ Stack ํด๋์ค์ ์ฐธ์กฐ์๊ฐ ๋๊ธฐ์ *this๋ฅผ ๋ฑ๋๊ฒ ๋ง์
๊ทผ๋ฐ ์ด๋ป๊ฒ ํด๋์ค ์์ฒด๋ฅผ ๋ฑ์๋๋ฐ ์ถ๋ ฅํ๋ฉด mem[tos]๊ฐ์ด ๋์ฌ๊น?
์๋๋ฉด
x๊ฐ ๋งค๊ฐ๋ณ์ n์ ๋ค์ด๊ฐ๋๋ฐ ์ด n์ด ๋ ํผ๋ฐ์ค ๋ณ์์. ์ฆ main๊ณผ operator>> ํจ์์์ ๋์ ๊ณต์ ๋จ(๊ฐ๊ณผ ์ฃผ์๊ฐ ๋ชจ๋ ๋ค!) ๊ทธ๋์ x๋ฅผ ์ถ๋ ฅํ์ ๋ ์ n๊ฐ(mem[tos])์ด ๋จ๋๊ฑฐ!
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Stack {
int size;
int* mem;//๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น, ์คํ๊ณต๊ฐ
int tos;
public:
Stack(int size = 10) {
this->size = size;
mem = new int[size];
tos = -1; //์คํ์ด ๋น์ด์์
}
~Stack() {
delete[] mem;
}
Stack& operator<<(int n);
Stack& operator>>(int& n);
bool operator!();// operator! ์ ๋งค๊ฐ๋ณ์ ๋น์๋์ผํจ
};
Stack& Stack::operator<<(int n) {
if (tos == size - 1) {
return *this;
}
tos++;
mem[tos] = n;
return *this;
}
Stack& Stack::operator>>(int& n) {
if (tos == -1) {
return *this;
}
n = mem[tos];
tos--;
return *this;
}
bool Stack::operator!() {
if (tos == -1) {
return true;
}
else {
return false;
}
}
int main() {
Stack stack;
stack << 3 << 5 << 10;
while (true) {
if (!stack) {
break;
}
int x;
stack >> x; // stack.>>(x)
cout << x << ' ';
}
cout << endl;
}
|
cs |
12๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class SortedArray {
int size;
int* p;
void sort();// ์ ์ ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
public:
SortedArray();
SortedArray(SortedArray& src); //๋ณต์ฌ ์์ฑ์
SortedArray(int p[], int size); //์์ฑ์
~SortedArray();
SortedArray operator+(SortedArray& op2);
SortedArray& operator=(const SortedArray& op2);
void show();
};
// ๋น์ด์๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํด์ค์ผํจ
void SortedArray::sort() {
//์ ํ์ ๋ ฌ
for (int i = 0; i < size - 1; i++) {
for (int j = i+1; j < size; j++) {
if (p[i] > p[j]) {
int temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
////๋ฒ๋ธ ์ ๋ ฌ
//for (int i = 0; i < size - 1; i++) {
// for (int j = i; j < size - 1; j++) {
// if (p[j] > p[j + 1]) {
// int temp = p[j];
// p[j] = p[j + 1];
// p[j + 1] = temp;
// }
// }
//}
}
void SortedArray::show() {
cout << "๋ฐฐ์ด ์ถ๋ ฅ : ";
for (int i = 0; i < size; i++) {
cout << this->p[i] << " ";
}
cout << endl;
}
SortedArray::SortedArray() {
p = NULL;
size = 0;
}
SortedArray::SortedArray(SortedArray& src) {
this->size = src.size;
p = new int[size];
for (int i = 0; i < size; i++) {
p[i] = src.p[i];
}
}
SortedArray::SortedArray(int p[], int size) {
this->size = size;
this->p = new int[size];
for (int i = 0; i < size; i++) {
this->p[i] = p[i];
}
sort();//์ด๊ฑฐ ํด์ค์ผํจ
}
SortedArray::~SortedArray() {
if (p != NULL) {
delete[] p;
}
}
SortedArray SortedArray::operator+(SortedArray& op2) {
////p์ ๋ค์ด์๋ ๊ฐ์ด NULL์ด ์๋ ๋
//if (p != NULL) {
// delete[] p;
//} // ์ด๊ฑฐ ํด์ฃผ๋ฉด ์๋๋ค! ๊ทธ๋ฌ๋ฉด ํฉ์น๋๊ฑฐ ํ์ง ๋ชปํจ! ํฉ์น๋ ค ํ๊ธฐ์ ์ ์ด๋ฏธ null์ ๋ง๋ค์ด ๋ฒ๋ฆฌ๋๊น..
SortedArray temp;
//๊ฐ์ฒด์ size๊ฐ 0์ผ๋
if (this->size == 0) {
temp.size = op2.size;
if (temp.size == 0) {
temp.p = NULL;
}
else {
temp.p = new int[temp.size];
for (int i = 0; i < temp.size; i++) {
temp.p[i] = op2.p[i];
}
}
}
//op2์ size๊ฐ 0์ผ๋
else if (op2.size == 0) {
temp.size = this->size;
temp.p=new int[temp.size];
for (int i = 0; i < temp.size; i++) {
temp.p[i] = this->p[i];
}
}
//๋๋ค 0์ด ์๋๋
else {
temp.size = this->size + op2.size;
temp.p = new int[temp.size];
int i;
for (i = 0; i <this->size; i++) {
temp.p[i] = this->p[i];
}
for (int j = 0; j < op2.size; j++) {
temp.p[i+j] = op2.p[j];
}
}
temp.sort();
return temp;
}
SortedArray& SortedArray::operator=(const SortedArray& op2) {
//p์ ๋ค์ด์๋ ๊ฐ์ด NULL์ด ์๋๋
if (p != NULL) {
delete[] p;
}
//op2.size๊ฐ 0์ผ๋
size = op2.size;
if (size == 0) {
p = NULL;
return *this;
}
//op2.size๊ฐ 0์ด ์๋ ๋
p = new int[size];
for (int i = 0; i <size; i++) {
p[i] = op2.p[i];
}
return *this;
}
int main() {
int n[] = { 2,20,6 };
int m[] = { 10,7,8,30 };
SortedArray a(n, 3);
SortedArray b(m, 4);
SortedArray c;
c = a + b;
a.show();
b.show();
c.show();
}
|
cs |
1๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Book {
string title;
int price, pages;
public:
Book(string title = "", int price = 0, int pages = 0) {
this->title=title;
this->price = price;
this->pages = pages;
}
friend bool operator<(string title,Book b);
void show() {
cout << getTitle() << ' ' << price << "์ " << pages << " ํ์ด์ง" << endl;
}
string getTitle() {
return title;
}
};
bool operator<(string title, Book b) {
if (title < b.title) {
return true;
}
return false;
}
int main() {
Book a("์ฒญ์ถ", 20000, 300);
string b;
cout << "์ฑ
์ด๋ฆ์ ์
๋ ฅํ์ธ์>>";
getline(cin, b);
if (b < a) {
cout << a.getTitle() << "์ด " << b << "๋ณด๋ค ๋ค์ ์๊ตฌ๋!" << endl;
}
}
|
cs |
friend ์จ์คฌ์ด์ผ!
'C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ช ํ C++] 9์ฅ ์ฐ์ต๋ฌธ์ (0) | 2022.12.05 |
---|---|
[๋ช ํ c++] 8์ฅ ์ฐ์ต๋ฌธ์ (0) | 2022.12.05 |
[C++] ๋ช ํ c++ 6์ฅ (0) | 2022.12.04 |
[C++] ์์๊ฐ์ํจ์์ ์ถ์ํด๋์ค (0) | 2022.12.04 |
[C++] ์์๊ณผ ์ ๊ทผ์ง์ ์ ์ ๋ฆฌ (0) | 2022.12.03 |
๋๊ธ