본문 바로가기
Programming/C&C++

[C++] TEMPLATE

TEMPLATE







템플릿

- 다형성의 종류 중 하나

- 일반화 프로그램 -> 타입 제한이 없다. 즉, 타입을 지정해 줄 수 없다

 

기본 형태

template <typename 가상타입명>

 

1. Template 함수

- C++에서만 존재 -> JAVA에서는 없다

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
 
template <typename T>
void change(T &t1, T &t2);
 
void main() {
    int a = 1, b = 2;
    char c = 'A', d = 'B';
    float e = 3.7f, f = 4.3f;
 
    change(a, b);
    cout << a << setw(10<< b << endl;
 
    change(c, d);
    cout << c << setw(10<< d << endl;
 
    change(e, f);
    cout << e << setw(10<< f << endl;
}
 
template <typename T> //밑에도 써줘야 한다
void change(T &t1, T &t2) {
    T tmp;
    tmp = t1;
    t1 = t2;
    t2 = tmp;
}
 
cs



명시적 오버로딩



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
 
template <typename T>
int Compare(T t1, T t2) { return t1 - t2; }
 
template <typename T> //명시적 오버로딩
int Compare(const char *str1, const char *str2) {
    return strcmp(str1, str2);
}
 
void main() {
    if (Compare(105> 0) { cout << "첫번째 인수가 크다" << endl; }
    else { cout << "첫번째 인수가 크지 않다" << endl; }
    if (Compare("hellp","yahoo"> 0) { cout << "첫번째 인수가 크다" << endl; }
    else { cout << "첫번째 인수가 크지 않다" << endl; }
}
 
cs



2. Template 클래스

- 메소드를 다 써줘야 한다




Template을 사용한 Stack / Queue

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
 
//동적 바인딩 스택, 큐 template으로 생성
 
template <typename T>
class Memory {
protected:
    T * m;
public:
    int count;
    Memory() {
        m = new T[20];
        count = 0;
    }
 
    virtual ~Memory() { //생성자 함수와 소멸자 함수는 세트
        delete[] m;
    }
 
    void push(T i) { //stack과 queue의 공통적을 데이터를 넣는 일을 하는 함수 
        if (count == 20) {
            cout << "FULL" << endl;
        }
        else {
            m[count++= i;
        }
    }
 
    virtual T pop() = 0;
};
 
 
template <typename T>
class MyStack : public Memory<string> { //stack
public:
    T pop() {
        if (count == 0) {
            return "EMPTY";
        }
        else {
            return m[--count];
        }
    }
};
 
template <typename T>
class MyQueue : public Memory<string> { //queue
public:
    int front;
 
    MyQueue() {
        front = 0;
    }
 
    T pop() {
        if (count == front) {
            return "EMPTY";
        }
        else {
            if (front == 20) {
                front = 0;
            }
            return m[front++];
        }
    }
};
 
 
void main() {
 
    Memory<string>* s = NULL;
 
    MyStack<string> ms;
    MyQueue<string> mq;
 
    int choose = 0;
    while (1) {
        cout << "1.Stack  2.Queue  3.Exit" << endl;
        cout << "선택 : ";
        cin >> choose;
        switch (choose) {
        case 1:
            s = &ms;
            break;
        case 2:
            s = &mq;
            break;
    
        while (1) {
            int qc = 0;
            cout << "1.Push  2.Pop  3.Exit" << endl;
            cout << "선택 : ";
            cin >> qc;
            switch (qc) {
            case 1: { //하나씩 입력
                string i;
                cout << "입력 : ";
                cin >> i;
                s->push(i);
            }break;
            case 2: { //하나씩 삭제
                cout << s->pop() << endl;
            }break;
            case 3
                exit(-1);
                break;
            }
        }
        case 3:
            exit(-1);
        
        }break;
    }
}
 
cs


'Programming > C&C++' 카테고리의 다른 글

[C/C++] Pointers  (1) 2018.12.27
[C++] STACK / QUEUE  (3) 2018.04.15
[C++] 클래스 상속 관계  (0) 2018.04.15
[C++] POINTER ARRAY  (0) 2018.04.15
[C++] DYNAMIC MEMORY  (1) 2018.04.15