๐ฅ ๋ค์ด๊ฐ๊ธฐ ์ ์
static ๋ฉค๋ฒ๋ ๋ฐ๋์ ์ ์ญ๊ณต๊ฐ์ ์ ์ธํจ์ผ๋ก์จ ๋ณ์๊ณต๊ฐ์ ํ ๋นํด์ผ ํ๋ค.
static ๋ฉค๋ฒํจ์๋ ์ค์ง static ๋ฉค๋ฒ๋ค๋ง ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
non-static ๋ฉค๋ฒํจ์๋ static๋ฉค๋ฒ,non-static ๋ฉค๋ฒ ๋ชจ๋ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
this์ฌ์ฉ ํ ์ ์๋ค. (๊ฐ์ฒด๊ฐ ์๊ธฐ๊ธฐ ์ ๋ถํฐ ํธ์ถ์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ)
2๋ฒ
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Person {
int id;
double weight;
string name;
public:
Person() {
id = 1;
name = "Grace";
weight = 20.5;
};
Person(int id,string name) {
this->id = id;
this->name = name;
weight = 20.5;
}
Person(int id, string name, double weight) {
this->id = id;
this->name = name;
this->weight = weight;
}
void show() {
cout << id << ' ' << weight << ' ' << name << endl;
}
};
int main() {
Person grace = Person();
Person ashely = Person(2, "Ashely");
Person helen=Person(3, "Helen", 32.5);
grace.show();
ashely.show();
helen.show();
}
(2) ๋ํดํธ ๋งค๊ฐ๋ณ์ ์ผ์ ๋
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Person {
int id;
double weight;
string name;
public:
Person(int id = 1, string name = "Grace", double weight = 20.5);
void show() {
cout << id << ' ' << weight << ' ' << name << endl;
}
};
Person::Person(int id, string name, double weight) {
this->id = id;
this->name = name;
this->weight = weight;
}
int main() {
Person grace = Person();
Person ashely = Person(2, "Ashely");
Person helen=Person(3, "Helen", 32.5);
grace.show();
ashely.show();
helen.show();
}
์ฐ์ ๋ํดํธ ๋งค๊ฐ๋ณ์๋ก ์ ์ํด์ค ๋ค์์
๋์ค์ ๋ณ์๋ก ๋ค์ด์ฌ ์ ๋ค์ ์ํด์ ์ ์ญ์ ๋ค์ ์ ์!
๋ชจ๋ default parameter๋ ์ค๋ฅธ์ชฝ๋ถํฐ ์ง์ ํด์ผ ํ๋ค. ๊ทธ๋์ ๋ค์์ ํ์ฉ๋์ง ์๋๋ค.
void printValue(int x=10, int y); // not allowed
5๋ฒ
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class ArrayUtility {
public:
static void intToDouble(int source[], double dest[], int size);
static void doubleToInt(double source[],int dest[], int size);
};
void ArrayUtility::intToDouble(int source[], double dest[], int size) {
for (int i = 0; i < size; i++) {
dest[i] = (double)source[i];
}
}
void ArrayUtility::doubleToInt(double source[], int dest[], int size) {
for (int i = 0; i < size; i++) {
dest[i] = (int)source[i];
}
}
int main() {
int x[] = { 1,2,3,4,5 };
double y[5];
double z[] = { 9.9,8.8,7.7,6.6,5.6 };
ArrayUtility::intToDouble(x, y, 5);
for (int i = 0; i < 5; i++) {
cout << y[i] << ' ';
}
cout << endl;
ArrayUtility::doubleToInt(z, x, 5);
for (int i = 0; i < 5; i++) {
cout << x[i] << ' ';
}
cout << endl;
}
7๋ฒ
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Random {
public:
static void seed() {
srand((unsigned)time(0));
}
static int nextInt(int min = 0, int max = 32767);
static char nextAlphabet();
static double nextDouble();
};
//void Random::seed() {}; // ์ด๋ ๊ฒ ์ฌ์ ์ํ๋ฉด ์๋จ ์ด๋ฏธ ํด๋์ค์์ ์ ์ธ๋ ์ ์ด๊ธฐ ๋๋ฌธ
int Random::nextInt(int min,int max) { // ๋ํดํธ ๋งค๊ฐ๋ณ์๋ ์ฌ์ ์ ํ๋ฉด ์๋จ! ๊ทธ๋ฅ min,max๋ก ๋๊ฒ
return rand() % (max - min + 1) + min; //srand๊ฐ ์๋๋ผ rand๋ค!
};
char Random::nextAlphabet() {
//A 65~90 a 97~122
int randArr[2];
randArr[0] = rand() % (90 - 65+1) + 65; //rand()%(b-a+1)+a
randArr[1] = rand() % (122 - 97+1) + 97;
int randIdx;
randIdx = rand() % 2;
return randArr[randIdx];
};
double Random::nextDouble() {
//์ค์ ๋์
//RAND_MAX๋ rand ํจ์์์ ๋ฐํํ ์ ์๋ ์ต๋๊ฐ
double d = Random::nextInt();
return d / RAND_MAX;
};
int main() {
cout << "1์์ 100๊น์ง ๋๋คํ ์ ์ 10๊ฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค" << endl;
Random::seed();
for (int i = 0; i < 10; i++) {
cout << Random::nextInt(1, 100) << ' ';
}
cout << endl;
cout << "์ํ๋ฒณ์ ๋๋คํ๊ฒ 10๊ฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค." << endl;
for (int i = 0; i < 10; i++) {
cout << Random::nextAlphabet() << ' ';
}
cout << endl;
cout << "๋๋คํ ์ค์ 10๊ฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค." << endl;
for (int i = 0; i < 5; i++) {
cout << Random::nextDouble() << ' ';
}
cout << endl;
for (int i = 0; i < 5; i++) {
cout << Random::nextDouble() << ' ';
}
}
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
int add(int* arr, int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int add(int* arr, int n, int* arr2) {
return add(arr, n) + add(arr2, n);
}
int main() {
int a[] = { 1,2,3,4,5 };
int b[] = { 6,7,8,9,10 };
int c = add(a, 5);
int d = add(a, 5, b);
cout << c << endl;//15
cout << d << endl;//55
}
|
cs |
๋๋ฌด ๋น์ฐํ ์๋ฆฌ์ธ๋ฐ addํจ์ mainํจ์ ๋ฐ์ ์ผ์ด์ผ ํ๋ค. ๊ณ์ ์๋ฐ๋ง ์ฐ๋ค๋ณด๋ ์ด์ฒ๊ตฌ๋ ์๋ ์ง๋ ํ๋๋ฏ..ใ
2๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
int big(int num1, int num2) {
int m = (num1 > num2) ? num1 : num2;
if (m > 100) {
return 100;
}
else {
return m;
}
}
int big(int num1, int num2, int num3) {
return big(num1, num2) > num3 ? num3 : big(num1, num2);
}
int main() {
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
}
|
cs |
4๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class MyVector {
int* mem;
int size;
public:
MyVector(int n=100, int val=0);
~MyVector() { delete[] mem; }
void show();
};
MyVector::MyVector(int n, int val) {
mem = new int[n];
size = n;
for (int i = 0; i < size; i++) {
mem[i] = val;
}
}
void MyVector::show() {
for (int i = 0; i < size; i++) {
cout << mem[i] << endl;
}
}
int main() {
MyVector a;
MyVector b(3,10);
a.show();
b.show();
}
|
cs |
์์์ ์ ์ธํ ๋ ๋ํดํธ ๋งค๊ฐ๋ณ์ ์จ์ฃผ๊ณ , ์ ์ํ ๋๋ ์์จ์ฃผ๋๊ฒ ์ ์์ธ๊ฒ ๊ฐ๋ค!
6๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class ArrayUtility2 {
public:
//
static int* concat(int s1[], int s2[], int size);
static int* remove(int s1[], int s2[],int size, int& retSize);
};
int* ArrayUtility2::remove(int s1[], int s2[], int size,int& retSize) {
retSize = size;
int idx = 0;
int* arr = new int[size];
for (int i = 0; i < size; i++) {
int std = s1[i];
for (int j = 0; j < size; j++) {
if (std == s2[j]) {
retSize--;
break;
}
}
arr[idx] = std;
idx++;
}
return arr;
}
int* ArrayUtility2::concat(int s1[], int s2[], int size) {
int* arr=new int[size*2];
int i;
for (i = 0; i < 5; i++) {
arr[i] = s1[i];
}
for (int j = 0; j < 5; j++) {
arr[i + j] = s2[j];
}
return arr;
}
int main() {
int x[5], y[5];
cout << "์ ์๋ฅผ 5 ๊ฐ ์
๋ ฅํ๋ผ. ๋ฐฐ์ด x์ ์ฝ์
ํ๋ค>>";
for (int i = 0; i < 5; i++) cin >> x[i];
cout << "์ ์๋ฅผ 5 ๊ฐ ์
๋ ฅํ๋ผ. ๋ฐฐ์ด y์ ์ฝ์
ํ๋ค>>";
for (int i = 0; i < 5; i++) cin >> y[i];
int* p = ArrayUtility2::concat(x, y, 5);
cout << "ํฉ์น ์ ์ ๋ฐฐ์ด์ ์ถ๋ ฅํ๋ค" << endl;
for (int i = 0; i < 10; i++) cout << p[i] << ' ';
cout << endl;
int retSize;
int* q = ArrayUtility2::remove(x, y, 5, retSize);
cout << "๋ฐฐ์ด x[]์์ y[]๋ฅผ ๋บ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค. ๊ฐ์๋ " << retSize << endl;
for (int i = 0; i < retSize; i++) cout << q[i] << ' ';
cout << endl;
delete[] p;
delete[] q;
}
|
cs |
9๋ฒ
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
|
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
using namespace std;
class Board {
public:
static string* text;
static int size;
static void add(string s);
static void print();
};
int Board::size = 0;
string* Board::text = new string[100];
void Board::add(string s) {
text[size] = s;
size++;
}
void Board::print() {
cout<<"********** "<<"๊ฒ์ํ์
๋๋ค."<<" **********"<<endl;
for (int i = 0; i < size; i++) {
cout << i << ": ";
cout << text[i] << endl;
}
}
int main() {
Board::add("์ค๊ฐ๊ณ ์ฌ๋ ๊ฐ๋
์๋ ์์จ ์ํ์
๋๋ค.");
Board::add("์ฝ๋ฉ ๋ผ์ด์ง ๋ง์ด ์ด์ฉํด ์ฃผ์ธ์.");
Board::print();
Board::add("์ง์๋ฆฐ ํ์์ด ๊ฒฝ์ง๋ํ ์
์ํ์์ต๋๋ค. ์ถํํด์ฃผ์ธ์.");
Board::print();
}
|
cs |
'C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ช ํ c++] 8์ฅ ์ฐ์ต๋ฌธ์ (0) | 2022.12.05 |
---|---|
[C++] ๋ช ํ C++ 7์ฅ (0) | 2022.12.04 |
[C++] ์์๊ฐ์ํจ์์ ์ถ์ํด๋์ค (0) | 2022.12.04 |
[C++] ์์๊ณผ ์ ๊ทผ์ง์ ์ ์ ๋ฆฌ (0) | 2022.12.03 |
[C++] ํ๋ ๋์ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ (0) | 2022.10.31 |
๋๊ธ