회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


Templete (템플릿)

푸딩뱃살 | 2015.11.22 12:15 | 조회 2047
C++ Templete (템플릿)

[템플릿 클래스]
탬플릿 클래스란 클래스의 멤버 속성과 메소드들의 타입을 일반화한 클래스를 말한다.

[템플릿 문법]
template <typename T>
class 템플릿클래스 {
private:
    T 템플릿변수
public:
    T 템플릿메소드(T 매개변수)
};

[제네릭 문법]
public class 제네릭클래스<T> {
    private T 제네릭변수 = default(T);
    public T 제네릭메소드(T 매개변수)
}

// 제네릭클래스<T> 제네릭객체인스턴스변수 = new 제네릭클래스<T>();

template <typename T>
T 템플릿클래스<T>::템플릿메소드(T 매개변수)

// 템플릿클래스<T>* 템플릿객체인스턴스 = new 템플릿클래스<T>()

#include <iostream>
#include <string>

using namespace std;

// 일반 업/다운캐스팅
class NormalDataContainer
{
private:
    int iValue;
    double dValue;
    char cValue;
    string sValue;
public:
    void SetIntValue(int value);
    int GetIntValue();

    void SetDoubleValue(double value);
    double GetDoubleValue();

    void SetCharValue(char value);
    char GetCharValue();

    void SetStringValue(string value);
    string GetStringValue();
};

// 기본 입출력(Set, Get)
void NormalDataContainer::SetIntValue(int value) {
    iValue = value;
}

int NormalDataContainer::GetIntValue() {
    return iValue;
}

void NormalDataContainer::SetDoubleValue(double value) {
    dValue = value;
}

double NormalDataContainer::GetDoubleValue() {
    return dValue;
}

void NormalDataContainer::SetCharValue(char value) {
    cValue = value;
}

char NormalDataContainer::GetCharValue() {
    return cValue;
}

void NormalDataContainer::SetStringValue(string value) {
    sValue = value;
}

string NormalDataContainer::GetStringValue() {
    return sValue;
}

// 몬스터 클래스-템플릿 예제 추가
class CMonster {
private:
    int damage;
    float speed;
public:
    CMonster() {
        this->damage = 20;
        this->speed = 7;
    }

    void Move() {
        cout << "몬스터가 " << speed << "로 이동합니다." << endl;
    }

    void Attack() {
        cout << "몬스터가 " << damage << "로 공격을 합니다." << endl;
    }
};

// 템플릿
// 기본 입출력에서 형이 달라 형에 맞춰 입출력해야 하는데
// 템플릿은 같은 함수에 무슨 형이 들어오는지 명시하면 그 형에 맞게 출력
template <typename Type>  // 템플릿 타입 생성
class TemplateDataContainer {
private:
    Type value;
public:
    ~TemplateDataContainer() {
        delete value;
    }
    void SetValue(Type value);
    Type GetValue();
};

// 템플릿 입출력
template <typename Type>
void TemplateDataContainer<Type>::SetValue(Type value) {
    this->value = value;
}

template <typename Type>
Type TemplateDataContainer<Type>::GetValue() {
    return value;
}

void main() {
    // 기본 타입
    NormalDataContainer* contain1 = new NormalDataContainer();
    contain1->SetIntValue(10);
    cout << "Normal Contain object int value : " << contain1->GetIntValue() << endl;
    delete contain1;

    NormalDataContainer* contain2 = new NormalDataContainer();
    contain2->SetDoubleValue(10.123);
    cout << "Normal Contain object double value : " << contain2->GetDoubleValue() << endl;
    delete contain2;

    NormalDataContainer* contain3 = new NormalDataContainer();
    contain3->SetCharValue('B');
    cout << "Normal Contain object char value : " << contain3->GetCharValue() << endl;
    delete contain3;

    NormalDataContainer* contain4 = new NormalDataContainer();
    contain4->SetStringValue("문자열");
    cout << "Normal Contain object string value : " << contain4->GetStringValue() << endl;
    delete contain4;

    // 템플릿 타입
    TemplateDataContainer<int*>* contain5 = new TemplateDataContainer<int*>();
    contain5->SetValue(new int(10));
    cout << "Template Contain object int value : " << contain5->GetValue() << endl;
    delete contain5;

    TemplateDataContainer<double*>* contain6 = new TemplateDataContainer<double*>();
    contain6->SetValue(new double(100.123));
    cout << "Template Contain object double value : " << contain6->GetValue() << endl;
    delete contain6;

    TemplateDataContainer<char*>* contain7 = new TemplateDataContainer<char*>();
    contain7->SetValue(new char('C'));
    cout << "Template Contain object char value : " << *(contain7->GetValue()) << endl;
    delete contain7;

    TemplateDataContainer<string*>* contain8 = new TemplateDataContainer<string*>();
    contain8->SetValue(new string("나도문자열"));
    cout << "Template Contain object string value : " << contain8->GetValue()->c_str() << endl;
    delete contain8;  // 해제 꼭 필요

    // 몬스터 클래스-템플릿 예제 추가
    // 몬스터의 동적 객체를 저장할 템플릿 컨테이너 객체 생성
    TemplateDataContainer<CMonster*>* contain9 =
        new TemplateDataContainer<CMonster*>();

    // 몬스터 객체 생성
    CMonster* monster = new CMonster();

    // 컨테이너에 몬스터 객체 저장
    contain9->SetValue(monster);

    // 컨테이너에서 몬스터 추출
    CMonster* m = contain9->GetValue();
    // 몬스터 이동
    m->Move();

    // 몬스터 공격
    contain9->GetValue()->Attack();

    delete contain9;
}
285개(6/15페이지)
프로그래밍
번호 제목 글쓴이 조회 날짜
185 [PHP] 회원가입 + 로그인 스크립트 (with Unity) [2+1] 푸딩뱃살 12246 2015.12.06 17:31
184 [C#] C# 추천 서적 푸딩뱃살 1329 2015.12.06 17:16
183 [C/C++] C++ 참고 사이트 푸딩뱃살 1185 2015.12.04 16:13
182 [PHP] 클래스 푸딩뱃살 3585 2015.12.04 14:40
181 [PHP] CodeIgniter(코드이그나이트) 첨부파일 푸딩뱃살 2937 2015.12.04 14:40
180 [C#] Delegate (델리게이트) (with Unity) 푸딩뱃살 6270 2015.12.01 10:44
179 [PHP] php 함수 푸딩뱃살 2220 2015.11.30 15:33
178 [PHP] Dictionary (딕셔너리) 첨부파일 푸딩뱃살 2871 2015.11.27 12:37
177 [PHP] 배열 푸딩뱃살 2172 2015.11.27 12:37
176 [PHP] 변수 선언 / 산술 연산 푸딩뱃살 2185 2015.11.27 11:14
175 [C#] Dictionary (딕셔너리) (with Unity) 첨부파일 [3+3] 푸딩뱃살 14487 2015.11.25 10:29
174 [C#] List (리스트) (with Unity) 첨부파일 푸딩뱃살 12091 2015.11.24 10:22
173 [C#] Generic (제네릭) (with Unity) 푸딩뱃살 4891 2015.11.22 12:32
>> [C/C++] Templete (템플릿) 푸딩뱃살 2048 2015.11.22 12:15
171 [C#] 키보드 입력 푸딩뱃살 3574 2015.11.21 18:00
170 [C#] 예외와 예외 처리 푸딩뱃살 4128 2015.11.21 17:39
169 [C#] Struct (구조체) (with Unity) 푸딩뱃살 5129 2015.11.18 22:08
168 [C#] Property (프로퍼티) (with Unity) 푸딩뱃살 2445 2015.11.18 10:50
167 [C#] interface (인터페이스) (with Unity) 첨부파일 푸딩뱃살 3311 2015.11.15 17:32
166 [C#] 스트림 (stream) - 문자, 바이너리 읽기/쓰기 푸딩뱃살 12792 2015.11.14 18:24