일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- #C++ has~a
- #bubbleSort
- #binary
- html video
- #1차원배열
- border-box
- #자바상속#자바이즈어#is~a
- 하이퍼레저패브릭
- html plug-in
- html id
- html5 new tag
- #android activity
- #CallByAddress
- docker example
- #2차원배열
- html object
- #C++ 연산자함수오버로딩
- git flow
- 토큰경제
- hyperledger transaction
- mac terminal command
- html code
- html youtube
- #JAVASCRIPT
- #성적관리프로그램
- relative path
- html charset
- #다차원포인터
- #3차원배열
- html multimedia
Archives
- Today
- Total
A sentimental robot
Template 본문
템플릿
기능은 같지만 데이터 타입이 다를경우 하나로 여러개를 만족시켜주는 형태 > 다형성
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 |
#include<iostream>
using namespace std;
template<typename T>
void change(T &a, T&b) {
T temp;
temp = a;
a = b;
b = temp;
}
int main() {
int a = 3, b = 4;
float c = 3.4f, d = 4.7f;
char e = 'A', f = 'B';
change(a, b);
cout << a << "\t" << b << endl;
change(c, d);
cout << c << "\t" << d << endl;
change(e, f);
cout << e << "\t" << f << endl;
}
|
cs |
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
31
32
33
34 |
#include<iostream>
using namespace std;
template<typename T>
class Tem
{
T a;
public :
void setA(T a) { // 멤버함수
this->a = a;
}
T getA()const;
};
template<typename T> // 멤버함수를 외부함수로 빼기
T Tem<T>::getA()const {
return a;
}
int main() {
Tem<int> a;
a.setA(10);
cout << a.getA() << endl;
Tem<char>c;
c.setA('a');
cout << c.getA() << endl;
}
|
cs |
3. 템플릿 클래스 상속
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 |
#include<iostream>
using namespace std;
template<typename T>
class Memory {
protected:
T array1[5];
int top;
public:
Memory()
{
top = 0;
}
void push(T n) {
if (top >= 5)
{
cout << "Already full" << endl;
return;
}
array1[top++] = n;
}
virtual void pop() = 0;
};
template<typename T>
class Mystack : public Memory<T> {
public:
virtual void pop() {
if (Memory<T>::top <= 0)
{
cout << "Empty" << endl;
return;
}
cout << "나온 값 => " << Memory<T>::array1[--Memory<T>::top] << endl;
}
};
template<typename T>
class MyQueue : public Memory<T> {
int front;
public:
MyQueue()
{
front = 0;
}
virtual void pop() {
if (Memory<T>::top <= 0)
{
cout << "Empty" << endl;
return;
}
cout << "나온 값 => " << Memory<T>::array1[front++] << endl;
--Memory<T>::top;
--front;
}
};
int main() {
Memory <char> *mem;
Mystack<char> ms;
MyQueue<char> mq;
char p;
int s;
int s1;
while (true)
{
cout << "=== 1. Stack 2. Queue === " << endl;
cin >> s;
cout << "----- 1.push 2. pop ----- " << endl;
cin >> s1;
switch (s)
{
case 1:
mem = &ms;
if (s1 == 1) {
cout << "넣을 값 => ";
cin >> p;
mem->push(p);
continue;
}
else if (s1 == 2) {
mem->pop();
continue;
}break;
case 2:
mem = &mq;
if (s1 == 1) {
cout << "넣을 값 => ";
cin >> p;
mem->push(p);
continue;
}
else if (s1 == 2) {
mem->pop();
continue;
}break;
default:
break;
}
}
} |
cs |