21
12 장 장장장 (template) Sung-Min Jung Internet Management Technology Lab. School of Information & Communication Engineering, Sungkyunkwan Univ. 300 Cheoncheon-dong, Jangan-gu, Suwon-si, Gyeonggi-do, Korea. Tel : +82-31-290-7222, Fax : +82-31-299-6673 [email protected]

12 장 템플릿 (template)

  • Upload
    louvain

  • View
    49

  • Download
    0

Embed Size (px)

DESCRIPTION

12 장 템플릿 (template). Sung-Min Jung Internet Management Technology Lab. School of Information & Communication Engineering, Sungkyunkwan Univ. 300 Cheoncheon-dong, Jangan-gu, Suwon-si, Gyeonggi-do, Korea. Tel : +82-31-290-7222, Fax : +82-31-299-6673 [email protected]. - PowerPoint PPT Presentation

Citation preview

Page 1: 12 장 템플릿 (template)

12 장 템플릿 (template)

Sung-Min JungInternet Management Technology Lab.

School of Information & Communication Engineering,Sungkyunkwan Univ.

300 Cheoncheon-dong, Jangan-gu, Suwon-si, Gyeonggi-do, Korea. Tel : +82-31-290-7222, Fax : +82-31-299-6673

[email protected]

Page 2: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 2

12-1 템플릿 (template) 에 대한 이해

IntroTemplate1.cpp

int Add(int a, int b)

{

return a+b;

}

template <typename T>

T Add(T a, T b)

{

return a+b;

}

템플릿화

Page 3: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 3

12-2 함수 템플릿

함수 템플릿 vs. 템플릿 함수 둘 이상의 타입에 대해서 템플릿화

IntroTemplate3.cpp

함수 템플릿의 특수화 SepciFuncTemplate2.cpp

template <typename

T>

int SizeOf(T a)

{

return sizeof(a);

}

template<>

int SizeOf(char* a)

{

return strlen(a);

}

특수화

Page 4: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 4

12-3 클래스 템플릿

class Data{ int data;public: Data(int d){ data=d; } void SetData(int d){ data=d; } int GetData(){ return data; }};

template <typename T>class Data{ T data;public: Data(T d){ data=d; } void SetData(T d){ data=d; } T GetData(){ return data; }};

템플릿화

int main(void){

Data<int> d1(0); d1.SetData(10);

Data<char> d2('a');   . . . . .

Page 5: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 5

template <typename T>class Data{ T data;public: Data(T d){ data=d; } void SetData(T d){ data=d; } T GetData(){ return data; }};

선언 /정의 분리

template <typename T>class Data{ T data;public: Data(T d); void SetData(T d); T GetData();}; template <typename T> Data<T>::Data(T d){

data=d;}template <typename T> void Data<T>::SetData(T d){

data=d;}template <typename T>T Data<T>::GetData(){

return data;}

12-3 클래스 템플릿

Page 6: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 6

12-4 템플릿의 원리 이해

그림 12-2

Page 7: 12 장 템플릿 (template)

13 장 예외처리

Sung-Min JungInternet Management Technology Lab.

School of Information & Communication Engineering,Sungkyunkwan Univ.

300 Cheoncheon-dong, Jangan-gu, Suwon-si, Gyeonggi-do, Korea. Tel : +82-31-290-7222, Fax : +82-31-299-6673

[email protected]

Page 8: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 8

13-1. 기존의 예외처리 방식

int main(void){ int a, b; cout<<"두개의 숫자 입력 : "; cin>>a>>b;  cout<<"a/b의 몫 : "<<a/b<<endl; cout<<"a/b의 나머지 : "<<a%b<<endl; return 0;}

int main(void){ int a, b;

cout<<"두개의 숫자 입력 : "; cin>>a>>b; if(b==0){ cout<<"입력오류 !"<<endl; } else { cout<<"a/b의 몫 : "<<a/b<<endl; cout<<"a/b의 나머지 : "<<a%b<<endl; } return 0;}

예외 처리 일반적이지 않은 프로그램의 흐름의 처리 에러가 아니다 !

Page 9: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 9

13-2. C++ 의 예외처리 메커니즘

try

catch

try {

/* 예외 발생 예상 지역 */

}

catch( 처리 되어야 할 예외의 종류 ) {

/* 예외를 처리하는 코드가 존재할 위치 */

}

Page 10: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 10

13-2. C++ 의 예외처리 메커니즘

try & catch

throw

try {

/* 예외 발생 예상 지역 */

}

catch( 처리 되어야 할 예외의 종류 ) {

/* 예외를 처리하는 코드가 존재할 위치 */

}

throw ex; // ex “ ” 를 가리켜 보통은 그냥 예외 라고 표현을 한다 .

Page 11: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 11

13-2. C++ 의 예외처리 메커니즘

그림 13-2

Page 12: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 12

13-2. C++ 의 예외처리 메커니즘

int main(void){ int a, b;

cout<<" 두개의 숫자 입력 : "; cin>>a>>b;

try{ if(b==0) throw b; cout<<"a/b 의 몫 : "<<a/b<<endl; cout<<"a/b 의 나머지 : "<<a%b<<endl; } catch(int exception){ cout<<exception<<" 입력 ."<<endl; cout<<" 입력오류 ! 다시 실행 하세요 ."<<endl; }

return 0;}

그림 13-3

Page 13: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 13

13-2. C++ 의 예외처리 메커니즘

OOhandlingflow.cpp

예외가 발생하면 try 블록의 나머지 부분 무시

예외 처리 후 catch 블록 이후부터 실행

int main(void){ int a, b;

cout<<" 두개의 숫자 입력 : "; cin>>a>>b;

try{ cout<<"try block start"<<endl;

if(b==0) throw b; cout<<"a/b 의 몫 : "<<a/b<<endl; cout<<"a/b 의 나머지 : "<<a%b<<endl;

cout<<"try block end"<<endl; } catch(int exception){ cout<<"catch block start"<<endl;

cout<<exception<<" 입력 ."<<endl; cout<<" 입력오류 ! 다시 실행 하세요 ."<<endl; }

cout<<"THANK YOU!"<<endl; return 0;}

Page 14: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 14

13-3. Stack Unwinding( 스택 풀기 )

int divide(int a, int b); // a/b 의 몫만 반환

int main(void){ int a, b;

cout<<" 두개의 숫자 입력 : "; cin>>a>>b;

try{ cout<<"a/b 의 몫 : "<<divide(a, b)<<endl; } catch(int exception){ cout<<exception<<" 입력 ."<<endl; cout<<" 입력오류 ! 다시 실행 하세요 ."<<endl; } return 0;}

int divide(int a, int b){ if(b==0) throw b; return a/b;}

그림 13-5

그림 13-4

Page 15: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 15

13-3. Stack Unwinding( 스택 풀기 )

그림 13-7 그림 13-8

Page 16: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 16

13-3. Stack Unwinding( 스택 풀기 )

예외가 처리되지 않으면 stdlib.h 에 선언되어 있는 abort 함수의 호출에 의해 프로그램 종료 unhandle_exp1.cpp, unhandle_exp2.cpp

전달되는 예외 명시 그 이외의 예외가 전달되면 abort 함수의 호출

int fct(double d) throw (int, double, char *)

{

. . . . .

}

Page 17: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 17

하나의 try, 여러 개의 catch catch_understand.cpp, catch_over1.cpp, catch_over2.cpp

13-3. Stack Unwinding( 스택 풀기 )

int main(void){ int num;

cout<<"input number: "; cin>>num;

try{ if(num>0) throw 10; // int 형 예외 전달 . else throw 'm'; // char 형 예외 전달 . } catch(int exp){ cout<<"int 형 예외 발생 "<<endl; } catch(char exp){ cout<<"char 형 예외 발생 "<<endl; } return 0;}

Page 18: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 18

13-4. 예외상황을 나타내는 클래스의 설계

예외 상황을 나타내는 클래스 & 객체 예외 클래스 , 예외 객체 일반 클래스 , 객체와 다르지 않다 . 예외 상황을 알려주기 위한 용도로 사용 catch_over1.cpp

예외는 클래스의 객체로 표현되는 것이 일반적

Page 19: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 19

13-5. 예외를 나타내는 클래스와 상속

catch 블록에 예외가 전달되는 방식 inheri_catch1.cpp, inheri_catch2.cpp

그림 13-9그림 13-10

Page 20: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 20

13-6. new 연산자에 의해 전달되는 예외

bad_alloc new 연산자가 메모리 공간 할당을 실패 했을 때 발생시키는 예외

using std::bad_alloc;

int main(void){ try{ int i=0; while(1){ cout<<i++<<" 번째 할당 "<<endl; double(*arr)[10000]=new double[10000][10000]; } } catch(bad_alloc ex){ ex.what(); cout<<endl<<"END"<<endl; }

return 0;}

Page 21: 12 장 템플릿 (template)

컴퓨터 공학 실습 2 21

13-7. 예외처리에 대한 나머지 문법 요소

모든 예외 처리 catch 블록

예외 다시 던지기 (re_throw.cpp)

try{

. . . . .

}

catch(…) { // … 선언은 모든 예외를 다 처리 하겠다는 선언

. . . . .

}

try{

. . . . .

}

catch(Exception& t) {

. . . . .

throw;

}