[C++] λͺ ν c++ 6μ₯
π₯ λ€μ΄κ°κΈ° μ μ
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 |