1 / 21

12 장 템플릿 (template)

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 smjung@imtl.skku.ac.kr.

louvain
Download Presentation

12 장 템플릿 (template)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 12장 템플릿(template) Sung-Min Jung InternetManagement 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 smjung@imtl.skku.ac.kr

  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; } 템플릿화

  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); } 특수화

  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'); . . . . .

  5. 12-3 클래스 템플릿 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; } 선언/정의 분리

  6. 12-4 템플릿의 원리 이해 그림 12-2

  7. 13장 예외처리 Sung-Min Jung InternetManagement 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 smjung@imtl.skku.ac.kr

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

  9. 13-2. C++의 예외처리 메커니즘 • try • catch try { /* 예외 발생 예상 지역 */ } catch(처리 되어야 할 예외의 종류) { /* 예외를 처리하는 코드가 존재할 위치 */ }

  10. 13-2. C++의 예외처리 메커니즘 • try & catch • throw try { /* 예외 발생 예상 지역 */ } catch(처리 되어야 할 예외의 종류) { /* 예외를 처리하는 코드가 존재할 위치 */ } throw ex; // ex를 가리켜 보통은 그냥 “예외”라고 표현을 한다.

  11. 13-2. C++의 예외처리 메커니즘 그림13-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

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

  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-4 그림13-5

  15. 13-3. Stack Unwinding(스택 풀기) 그림13-8 그림13-7

  16. 13-3. Stack Unwinding(스택 풀기) • 예외가 처리되지 않으면 • stdlib.h에 선언되어 있는 abort 함수의 호출에 의해 프로그램 종료 • unhandle_exp1.cpp, unhandle_exp2.cpp • 전달되는 예외 명시 • 그 이외의 예외가 전달되면 abort 함수의 호출 int fct(double d) throw (int, double, char *) { . . . . . }

  17. 13-3. Stack Unwinding(스택 풀기) • 하나의 try, 여러 개의 catch • catch_understand.cpp, • catch_over1.cpp, catch_over2.cpp 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; }

  18. 13-4. 예외상황을 나타내는 클래스의 설계 • 예외 상황을 나타내는 클래스 & 객체 • 예외 클래스, 예외 객체 • 일반 클래스, 객체와 다르지 않다. • 예외 상황을 알려주기 위한 용도로 사용 • catch_over1.cpp • 예외는 클래스의 객체로 표현되는 것이 일반적

  19. 13-5. 예외를 나타내는 클래스와 상속 • catch 블록에 예외가 전달되는 방식 • inheri_catch1.cpp, inheri_catch2.cpp 그림13-10 그림13-9

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

  21. 13-7. 예외처리에 대한 나머지 문법 요소 • 모든 예외 처리 catch 블록 • 예외 다시 던지기(re_throw.cpp) try{ . . . . . } catch(…) { // …선언은 모든 예외를 다 처리 하겠다는 선언 . . . . . } try{ . . . . . } catch(Exception& t) { . . . . . throw; }

More Related