티스토리 뷰

LANGUAGE/C++

[C++] TEMPLATE - 템플릿

진심스테이크 2018. 3. 16. 10:31

 

템플릿

- 다형성의 종류 중 하나

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

 

 

기본 형태

template <typename 가상타입명>

 

 

종류

1. Template 함수

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

#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;
}

 

 

명시적 오버로딩

#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(10, 5) > 0) { cout << "첫번째 인수가 크다" << endl; }
    else { cout << "첫번째 인수가 크지 않다" << endl; }
    if (Compare("hellp","yahoo") > 0) { cout << "첫번째 인수가 크다" << endl; }
    else { cout << "첫번째 인수가 크지 않다" << endl; }
}​

 

 

2. Template 클래스

- 메소드를 다 써줘야 한다

 

 


 

 

Template을 사용한 Stack / Queue

#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;
    }
}

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

[C++] 오버로딩 불가능 연산자  (0) 2018.04.13
[C++] FILE INPUT/OUTPUT  (0) 2018.03.16
[C++] STRING 직접 구현  (0) 2018.03.15
[C++] OPERATOR FUNCTION - 연산자 함수  (0) 2018.03.14
[C++] FRIEND  (0) 2018.03.14
댓글