SlideShare uma empresa Scribd logo
1 de 23
C++ <Template> 03/11/2009 1 Hadziq Fabroyir - Informatics ITS
How to implement ↓ in OOP ?¿ several methods withsame function statementsbut take different type of parameters 03/11/2009 Hadziq Fabroyir - Informatics ITS 2
What are you going to code ?¿ - to express swap function for various data type - 03/11/2009 Hadziq Fabroyir - Informatics ITS 3
Declare it one by one ?¿ void swapInt  (int *a, int *b); void swapFloat (float *c, float *d); void swapChar (char *p, char *q); … // every methods has a specific name 03/11/2009 Hadziq Fabroyir - Informatics ITS 4
Make it more general , guys ! 03/11/2009 Hadziq Fabroyir - Informatics ITS 5
Function Overloading void swap (int *a, int *b) { int temp;  temp = *a;  *a = *b;  *b = temp; } void swap (float *c, float *d) {float temp;  temp = *c;  *c = *d;  *d = temp;} void swap (char *p, char *q) {char temp;  temp = *p;  *p = *q;  *q = temp;} 03/11/2009 Hadziq Fabroyir - Informatics ITS 6
Here it is , C++ introduce <template>  ! 03/11/2009 Hadziq Fabroyir - Informatics ITS 7
Why <templates> C++ promotes code reusability Inheritance and composition provide a way to re-use object code Templates provide a way to re-use source code Example: Queue<T> 	generic Queue having a type parameter T 	T can be replaced with actual types 03/11/2009 Hadziq Fabroyir - Informatics ITS 8
Why use <templates> Templates are easier to write. You create only one generic version of your class or function instead of manually creating specializations.  Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.  Templates are type-safe. Because the types that templates act upon are known at compile time, the compiler can perform type checking before errors occur.  03/11/2009 Hadziq Fabroyir - Informatics ITS 9
<template> 03/11/2009 Hadziq Fabroyir - Informatics ITS 10
Function Template 03/11/2009 Hadziq Fabroyir - Informatics ITS 11
What’s it all about Easily create a large range of related functions or classes Function template - the blueprintof the related functions Template function - a specific function made from a function template 03/11/2009 Hadziq Fabroyir - Informatics ITS 12
Look at this declaration of Function Template  template <class tipe>  void swap(tipe &a,tipe &b)  {  tipe c;  c = a;  a = b;  b = c;  }  03/11/2009 Hadziq Fabroyir - Informatics ITS 13
Function Usage Example Instance of  int void main(void) {  int a,b;  a = 10;  b = 20;  	swap(a,b); cout << a << “ “ << b << endl;  }  Instance of  float void main(void) {  float a,b;  a = 10.0;  b = 20.0;  	swap(a,b); cout << a << “ “ << b << endl;  }  03/11/2009 Hadziq Fabroyir - Informatics ITS 14
<template> effects It’s reusable ► we can gain the time for coding It’s possible to swap 2 objects instantiated from a class 03/11/2009 Hadziq Fabroyir - Informatics ITS 15
Overloading Template Function  #include <iostream.h>  template <class Tipe>  void swap(Tipe &a,Tipe &b)  {  Tipe tmp;  tmp = a;  a = b;  b = tmp;  } 03/11/2009 Hadziq Fabroyir - Informatics ITS 16 void swap(float &a,float &b)  {  float c;  a = (a < 0.00)?–a:a;  b = (b < 0.00)?–b:b;  c = a;  a = b;  b = c;  }
Class Template 03/11/2009 Hadziq Fabroyir - Informatics ITS 17
Class Template template <class MyTipe>  class CMyTemplate  {  protected :  MyTipe m_a,m_b;  public :  void Done(void) {  		memset(&m_a,0,sizeof(MyTipe));  	} };  03/11/2009 Hadziq Fabroyir - Informatics ITS 18 /*   method is declared outside the class*/ template <class MyTipe>  void CMyTemplate<MyTipe>::Init(MyTipe a,MyTipe b)  {  m_a = a;  m_b = b;  }
Class Template on … struct  template <class MyTipe>  struct tMyStruct  {  MyTipe a,b;  };  union template <class MyTipe>  union _MyUnion  {  MyTipe a;  int b;  };  03/11/2009 Hadziq Fabroyir - Informatics ITS 19
Class Template Usage void main(void)  {  CMyTemplate<short> a;  tMystruct<short> b;  _MyUnion<float> c;  }  03/11/2009 Hadziq Fabroyir - Informatics ITS 20
Using 2 Type (Example) template <class MyTipe1,class MyTipe2>  class CMyTemplate  {  protected :  MyTipe1 m_a MyTipe2 m_b;  public :  void Init(MyTipe1 a,MyTipe2 b)  { m_a = a;  m_b = b;  }  };  03/11/2009 Hadziq Fabroyir - Informatics ITS 21 void main(void)  {  CMyTemplate<short,float> a;  }
For your practice … Exercise Declare and Define template for your latest Classes of ... Shape (ex: public getColor(), protected color) 2D Shape, 3D Shape (ex: public getArea(), protected edgeLength) Triangle, Circle, Square; Cube, Cylinder, Sphere Lab Session (Senin, 19.00 – 21.00) Please provide: 	the softcopy(send it by email – deadline Sunday 23:59) 	the hardcopy(bring it when attending the lab session) 03/11/2009 Hadziq Fabroyir - Informatics ITS 22
☺~ Next:  Exception HIndling ~☺ [ 23 ] Hadziq Fabroyir - Informatics ITS 03/11/2009

Mais conteúdo relacionado

Mais procurados

vlsi training in chandigarh
vlsi training in chandigarhvlsi training in chandigarh
vlsi training in chandigarhmatrixphagwara
 
data mining training in Bangalore
data mining training in Bangaloredata mining training in Bangalore
data mining training in Bangalorematrixphagwara
 
A lab report on modeling and simulation with python code
A lab report on modeling and simulation with python codeA lab report on modeling and simulation with python code
A lab report on modeling and simulation with python codeAlamgir Hossain
 
All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...
All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...
All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...Quoc-Sang Phan
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdSyed Mustafa
 
Matlab final year project in jalandhar
Matlab final year project in jalandharMatlab final year project in jalandhar
Matlab final year project in jalandhardeepikakaler1
 
Matlab final year project in ludhiana
Matlab final year project in ludhianaMatlab final year project in ludhiana
Matlab final year project in ludhianadeepikakaler1
 
Cut and Goal on prolog
Cut and Goal on prologCut and Goal on prolog
Cut and Goal on prologchauhankapil
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, iohtaitk
 
Intro to Functional Programming with Scala - #psuweb
Intro to Functional Programming with Scala - #psuwebIntro to Functional Programming with Scala - #psuweb
Intro to Functional Programming with Scala - #psuwebDerekMorr
 
Fsharp shdh 40 lightning talk
Fsharp shdh 40 lightning talkFsharp shdh 40 lightning talk
Fsharp shdh 40 lightning talkmbhwork
 
Roassal presentation
Roassal presentationRoassal presentation
Roassal presentationbergel
 

Mais procurados (19)

vlsi training in chandigarh
vlsi training in chandigarhvlsi training in chandigarh
vlsi training in chandigarh
 
data mining training in Bangalore
data mining training in Bangaloredata mining training in Bangalore
data mining training in Bangalore
 
Pcd201516
Pcd201516Pcd201516
Pcd201516
 
Unit iv
Unit ivUnit iv
Unit iv
 
5.program structure
5.program structure5.program structure
5.program structure
 
A lab report on modeling and simulation with python code
A lab report on modeling and simulation with python codeA lab report on modeling and simulation with python code
A lab report on modeling and simulation with python code
 
All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...
All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...
All-Solution Satisfiability Modulo Theories: applications, algorithms and ben...
 
Oops recap
Oops recapOops recap
Oops recap
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
Matlab final year project in jalandhar
Matlab final year project in jalandharMatlab final year project in jalandhar
Matlab final year project in jalandhar
 
Matlab final year project in ludhiana
Matlab final year project in ludhianaMatlab final year project in ludhiana
Matlab final year project in ludhiana
 
Cut and Goal on prolog
Cut and Goal on prologCut and Goal on prolog
Cut and Goal on prolog
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
Pointers
 Pointers Pointers
Pointers
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
Intro to Functional Programming with Scala - #psuweb
Intro to Functional Programming with Scala - #psuwebIntro to Functional Programming with Scala - #psuweb
Intro to Functional Programming with Scala - #psuweb
 
Fsharp shdh 40 lightning talk
Fsharp shdh 40 lightning talkFsharp shdh 40 lightning talk
Fsharp shdh 40 lightning talk
 
Roassal presentation
Roassal presentationRoassal presentation
Roassal presentation
 

Destaque

GD - 3rd - Game Genres Study Case [Part 2]
GD - 3rd - Game Genres Study Case [Part 2]GD - 3rd - Game Genres Study Case [Part 2]
GD - 3rd - Game Genres Study Case [Part 2]Hadziq Fabroyir
 
DW - 2nd - Introduction To DW & BI
DW - 2nd - Introduction To DW & BIDW - 2nd - Introduction To DW & BI
DW - 2nd - Introduction To DW & BIHadziq Fabroyir
 
Presentazione weissman 2013
Presentazione weissman 2013Presentazione weissman 2013
Presentazione weissman 2013Maurizio Bottaro
 
GD - 2nd - Introduction To Game (History And Genres)
GD - 2nd - Introduction To Game (History And Genres)GD - 2nd - Introduction To Game (History And Genres)
GD - 2nd - Introduction To Game (History And Genres)Hadziq Fabroyir
 
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented ProgrammingHadziq Fabroyir
 
Sponsorbrein Sanoma Mediaparade
Sponsorbrein Sanoma MediaparadeSponsorbrein Sanoma Mediaparade
Sponsorbrein Sanoma MediaparadeErik Ruts
 
Group - 6 1st Periodical Exam
Group - 6 1st Periodical ExamGroup - 6 1st Periodical Exam
Group - 6 1st Periodical Examearlgodwin
 
Drivers van rendement
Drivers van rendementDrivers van rendement
Drivers van rendementErik Ruts
 
Invloed van sponsors op de sport
Invloed van sponsors op de sportInvloed van sponsors op de sport
Invloed van sponsors op de sportErik Ruts
 
Neuromarketing, sponsoring en effectiviteit
Neuromarketing, sponsoring en effectiviteitNeuromarketing, sponsoring en effectiviteit
Neuromarketing, sponsoring en effectiviteitErik Ruts
 
Expertmeeting Sportsponsoring Den Haag
Expertmeeting Sportsponsoring Den HaagExpertmeeting Sportsponsoring Den Haag
Expertmeeting Sportsponsoring Den HaagErik Ruts
 
Aurora Heights Residences Presentation
Aurora Heights Residences PresentationAurora Heights Residences Presentation
Aurora Heights Residences Presentationleadahead
 
Opvallende sponsoring voor clubs
Opvallende sponsoring voor clubsOpvallende sponsoring voor clubs
Opvallende sponsoring voor clubsErik Ruts
 
Branding & sportmanagement
Branding & sportmanagementBranding & sportmanagement
Branding & sportmanagementErik Ruts
 
Red het sponsorbudget
Red het sponsorbudgetRed het sponsorbudget
Red het sponsorbudgetErik Ruts
 
Sponsorium Report #2
Sponsorium Report #2Sponsorium Report #2
Sponsorium Report #2Erik Ruts
 
How to Select Course at NTUST
How to Select Course at NTUSTHow to Select Course at NTUST
How to Select Course at NTUSTHadziq Fabroyir
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting StartedHadziq Fabroyir
 

Destaque (20)

GD - 3rd - Game Genres Study Case [Part 2]
GD - 3rd - Game Genres Study Case [Part 2]GD - 3rd - Game Genres Study Case [Part 2]
GD - 3rd - Game Genres Study Case [Part 2]
 
Manajemen Waktu
Manajemen WaktuManajemen Waktu
Manajemen Waktu
 
DW - 2nd - Introduction To DW & BI
DW - 2nd - Introduction To DW & BIDW - 2nd - Introduction To DW & BI
DW - 2nd - Introduction To DW & BI
 
GD - 3rd - Game Genres
GD - 3rd - Game GenresGD - 3rd - Game Genres
GD - 3rd - Game Genres
 
Presentazione weissman 2013
Presentazione weissman 2013Presentazione weissman 2013
Presentazione weissman 2013
 
GD - 2nd - Introduction To Game (History And Genres)
GD - 2nd - Introduction To Game (History And Genres)GD - 2nd - Introduction To Game (History And Genres)
GD - 2nd - Introduction To Game (History And Genres)
 
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
#OOP_D_ITS - 1st - Introduction To Object Oriented Programming
 
Sponsorbrein Sanoma Mediaparade
Sponsorbrein Sanoma MediaparadeSponsorbrein Sanoma Mediaparade
Sponsorbrein Sanoma Mediaparade
 
Group - 6 1st Periodical Exam
Group - 6 1st Periodical ExamGroup - 6 1st Periodical Exam
Group - 6 1st Periodical Exam
 
Drivers van rendement
Drivers van rendementDrivers van rendement
Drivers van rendement
 
Invloed van sponsors op de sport
Invloed van sponsors op de sportInvloed van sponsors op de sport
Invloed van sponsors op de sport
 
Neuromarketing, sponsoring en effectiviteit
Neuromarketing, sponsoring en effectiviteitNeuromarketing, sponsoring en effectiviteit
Neuromarketing, sponsoring en effectiviteit
 
Expertmeeting Sportsponsoring Den Haag
Expertmeeting Sportsponsoring Den HaagExpertmeeting Sportsponsoring Den Haag
Expertmeeting Sportsponsoring Den Haag
 
Aurora Heights Residences Presentation
Aurora Heights Residences PresentationAurora Heights Residences Presentation
Aurora Heights Residences Presentation
 
Opvallende sponsoring voor clubs
Opvallende sponsoring voor clubsOpvallende sponsoring voor clubs
Opvallende sponsoring voor clubs
 
Branding & sportmanagement
Branding & sportmanagementBranding & sportmanagement
Branding & sportmanagement
 
Red het sponsorbudget
Red het sponsorbudgetRed het sponsorbudget
Red het sponsorbudget
 
Sponsorium Report #2
Sponsorium Report #2Sponsorium Report #2
Sponsorium Report #2
 
How to Select Course at NTUST
How to Select Course at NTUSTHow to Select Course at NTUST
How to Select Course at NTUST
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 

Semelhante a #OOP_D_ITS - 9th - Template

#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
T3CON14EU: Migrating from TYPO3 CMS to TYPO3 Flow
T3CON14EU: Migrating from TYPO3 CMS to TYPO3 FlowT3CON14EU: Migrating from TYPO3 CMS to TYPO3 Flow
T3CON14EU: Migrating from TYPO3 CMS to TYPO3 Flowmhelmich
 
InspiringCon15: Bringing TYPO3 Legacy Applications into the Flow
InspiringCon15: Bringing TYPO3 Legacy Applications into the FlowInspiringCon15: Bringing TYPO3 Legacy Applications into the Flow
InspiringCon15: Bringing TYPO3 Legacy Applications into the Flowmhelmich
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta ProgrammingReggie Meisler
 
Augustus Overview Open Source Analytics
Augustus Overview  Open Source AnalyticsAugustus Overview  Open Source Analytics
Augustus Overview Open Source Analyticsjtrussell
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class StructureHadziq Fabroyir
 
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2 「エッジAIモダン計測制御の世界」オ...
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2  「エッジAIモダン計測制御の世界」オ...Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2  「エッジAIモダン計測制御の世界」オ...
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2 「エッジAIモダン計測制御の世界」オ...Mr. Vengineer
 
Implementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresImplementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresGowtham Reddy
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义yiditushe
 
What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9Yauheni Akhotnikau
 
First Failure Data Capture for your enterprise application with WebSphere App...
First Failure Data Capture for your enterprise application with WebSphere App...First Failure Data Capture for your enterprise application with WebSphere App...
First Failure Data Capture for your enterprise application with WebSphere App...Rohit Kelapure
 
닷넷 개발자를 위한 패턴이야기
닷넷 개발자를 위한 패턴이야기닷넷 개발자를 위한 패턴이야기
닷넷 개발자를 위한 패턴이야기YoungSu Son
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API OverviewRaymond Peck
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API OverviewSri Ambati
 

Semelhante a #OOP_D_ITS - 9th - Template (20)

#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
C++lecture9
C++lecture9C++lecture9
C++lecture9
 
T3CON14EU: Migrating from TYPO3 CMS to TYPO3 Flow
T3CON14EU: Migrating from TYPO3 CMS to TYPO3 FlowT3CON14EU: Migrating from TYPO3 CMS to TYPO3 Flow
T3CON14EU: Migrating from TYPO3 CMS to TYPO3 Flow
 
OSGi with the Spring Framework
OSGi with the Spring FrameworkOSGi with the Spring Framework
OSGi with the Spring Framework
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
InspiringCon15: Bringing TYPO3 Legacy Applications into the Flow
InspiringCon15: Bringing TYPO3 Legacy Applications into the FlowInspiringCon15: Bringing TYPO3 Legacy Applications into the Flow
InspiringCon15: Bringing TYPO3 Legacy Applications into the Flow
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta Programming
 
Augustus Overview Open Source Analytics
Augustus Overview  Open Source AnalyticsAugustus Overview  Open Source Analytics
Augustus Overview Open Source Analytics
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
 
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2 「エッジAIモダン計測制御の世界」オ...
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2  「エッジAIモダン計測制御の世界」オ...Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2  「エッジAIモダン計測制御の世界」オ...
Google Edge TPUで TensorFlow Liteを使った時に 何をやっているのかを妄想してみる 2 「エッジAIモダン計測制御の世界」オ...
 
Implementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphoresImplementing of classical synchronization problem by using semaphores
Implementing of classical synchronization problem by using semaphores
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
 
What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9What's new in SObjectizer 5.5.9
What's new in SObjectizer 5.5.9
 
First Failure Data Capture for your enterprise application with WebSphere App...
First Failure Data Capture for your enterprise application with WebSphere App...First Failure Data Capture for your enterprise application with WebSphere App...
First Failure Data Capture for your enterprise application with WebSphere App...
 
닷넷 개발자를 위한 패턴이야기
닷넷 개발자를 위한 패턴이야기닷넷 개발자를 위한 패턴이야기
닷넷 개발자를 위한 패턴이야기
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API Overview
 
H2O 3 REST API Overview
H2O 3 REST API OverviewH2O 3 REST API Overview
H2O 3 REST API Overview
 
Xdebug
XdebugXdebug
Xdebug
 

Mais de Hadziq Fabroyir

An Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld DeviceAn Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld DeviceHadziq Fabroyir
 
在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發Hadziq Fabroyir
 
NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)Hadziq Fabroyir
 
律法保護的五件事
律法保護的五件事律法保護的五件事
律法保護的五件事Hadziq Fabroyir
 
Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話Hadziq Fabroyir
 
Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西Hadziq Fabroyir
 
Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通Hadziq Fabroyir
 
Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳Hadziq Fabroyir
 
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹Hadziq Fabroyir
 
Living in Taiwan for Dummies
Living in Taiwan for DummiesLiving in Taiwan for Dummies
Living in Taiwan for DummiesHadziq Fabroyir
 
NTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students OrientationNTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students OrientationHadziq Fabroyir
 
NTUST Course Selection - How to
NTUST Course Selection - How toNTUST Course Selection - How to
NTUST Course Selection - How toHadziq Fabroyir
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class DiagramHadziq Fabroyir
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And ReferencesHadziq Fabroyir
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++Hadziq Fabroyir
 
Gd - 8th - Web Game Design Using Flash
Gd - 8th - Web Game Design Using FlashGd - 8th - Web Game Design Using Flash
Gd - 8th - Web Game Design Using FlashHadziq Fabroyir
 
GD - 6th - Graphic Design For Games
GD - 6th - Graphic Design For GamesGD - 6th - Graphic Design For Games
GD - 6th - Graphic Design For GamesHadziq Fabroyir
 
GD - 5th - The Theory Of Fun In Game Design Full Resolution
GD - 5th - The Theory Of Fun In Game Design Full ResolutionGD - 5th - The Theory Of Fun In Game Design Full Resolution
GD - 5th - The Theory Of Fun In Game Design Full ResolutionHadziq Fabroyir
 
GD - 4th - Game Architecture
GD - 4th - Game ArchitectureGD - 4th - Game Architecture
GD - 4th - Game ArchitectureHadziq Fabroyir
 

Mais de Hadziq Fabroyir (20)

An Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld DeviceAn Immersive Map Exploration System Using Handheld Device
An Immersive Map Exploration System Using Handheld Device
 
在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發在不同尺度遙現系統中具空間感知特性的使用者介面開發
在不同尺度遙現系統中具空間感知特性的使用者介面開發
 
NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)NTUST Course Selection (Revision: Fall 2016)
NTUST Course Selection (Revision: Fall 2016)
 
律法保護的五件事
律法保護的五件事律法保護的五件事
律法保護的五件事
 
Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話Pelajaran 5 第五課 • Telepon 給打電話
Pelajaran 5 第五課 • Telepon 給打電話
 
Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西Pelajaran 4 第四課 • Belanja 買東西
Pelajaran 4 第四課 • Belanja 買東西
 
Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通Pelajaran 3 第三課 • Transportasi 交通
Pelajaran 3 第三課 • Transportasi 交通
 
Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳Pelajaran 2 第二課 • Di Restoran 在餐廳
Pelajaran 2 第二課 • Di Restoran 在餐廳
 
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
Pelajaran 1 第一課 • Perkenalan Diri 自我介紹
 
Living in Taiwan for Dummies
Living in Taiwan for DummiesLiving in Taiwan for Dummies
Living in Taiwan for Dummies
 
NTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students OrientationNTUST-IMSA • International Students Orientation
NTUST-IMSA • International Students Orientation
 
NTUST Course Selection - How to
NTUST Course Selection - How toNTUST Course Selection - How to
NTUST Course Selection - How to
 
Brain Battle Online
Brain Battle OnlineBrain Battle Online
Brain Battle Online
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++
 
Gd - 8th - Web Game Design Using Flash
Gd - 8th - Web Game Design Using FlashGd - 8th - Web Game Design Using Flash
Gd - 8th - Web Game Design Using Flash
 
GD - 6th - Graphic Design For Games
GD - 6th - Graphic Design For GamesGD - 6th - Graphic Design For Games
GD - 6th - Graphic Design For Games
 
GD - 5th - The Theory Of Fun In Game Design Full Resolution
GD - 5th - The Theory Of Fun In Game Design Full ResolutionGD - 5th - The Theory Of Fun In Game Design Full Resolution
GD - 5th - The Theory Of Fun In Game Design Full Resolution
 
GD - 4th - Game Architecture
GD - 4th - Game ArchitectureGD - 4th - Game Architecture
GD - 4th - Game Architecture
 

Último

Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...
Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...
Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...Miss joya
 
VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...
VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...
VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...Miss joya
 
Sonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Sonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call NowSonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Sonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call NowRiya Pathan
 
Low Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service Mumbai
Low Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service MumbaiLow Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service Mumbai
Low Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service Mumbaisonalikaur4
 
VIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service Lucknow
VIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service LucknowVIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service Lucknow
VIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service Lucknownarwatsonia7
 
High Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service Jaipur
High Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service JaipurHigh Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service Jaipur
High Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service Jaipurparulsinha
 
Call Girl Service Bidadi - For 7001305949 Cheap & Best with original Photos
Call Girl Service Bidadi - For 7001305949 Cheap & Best with original PhotosCall Girl Service Bidadi - For 7001305949 Cheap & Best with original Photos
Call Girl Service Bidadi - For 7001305949 Cheap & Best with original Photosnarwatsonia7
 
Book Call Girls in Yelahanka - For 7001305949 Cheap & Best with original Photos
Book Call Girls in Yelahanka - For 7001305949 Cheap & Best with original PhotosBook Call Girls in Yelahanka - For 7001305949 Cheap & Best with original Photos
Book Call Girls in Yelahanka - For 7001305949 Cheap & Best with original Photosnarwatsonia7
 
Call Girls In Andheri East Call 9920874524 Book Hot And Sexy Girls
Call Girls In Andheri East Call 9920874524 Book Hot And Sexy GirlsCall Girls In Andheri East Call 9920874524 Book Hot And Sexy Girls
Call Girls In Andheri East Call 9920874524 Book Hot And Sexy Girlsnehamumbai
 
Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...
Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...
Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...narwatsonia7
 
Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...
Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...
Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...narwatsonia7
 
Book Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbers
Book Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbersBook Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbers
Book Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbersnarwatsonia7
 
Call Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort Service
Call Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort ServiceCall Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort Service
Call Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort Serviceparulsinha
 
Kolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Kolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call NowKolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Kolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call NowNehru place Escorts
 
Housewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment Booking
Housewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment BookingHousewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment Booking
Housewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment Bookingnarwatsonia7
 
Glomerular Filtration and determinants of glomerular filtration .pptx
Glomerular Filtration and  determinants of glomerular filtration .pptxGlomerular Filtration and  determinants of glomerular filtration .pptx
Glomerular Filtration and determinants of glomerular filtration .pptxDr.Nusrat Tariq
 
Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...
Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...
Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...narwatsonia7
 
Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...
Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...
Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...narwatsonia7
 

Último (20)

Escort Service Call Girls In Sarita Vihar,, 99530°56974 Delhi NCR
Escort Service Call Girls In Sarita Vihar,, 99530°56974 Delhi NCREscort Service Call Girls In Sarita Vihar,, 99530°56974 Delhi NCR
Escort Service Call Girls In Sarita Vihar,, 99530°56974 Delhi NCR
 
Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...
Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...
Russian Call Girls in Pune Riya 9907093804 Short 1500 Night 6000 Best call gi...
 
VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...
VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...
VIP Call Girls Pune Vrinda 9907093804 Short 1500 Night 6000 Best call girls S...
 
Sonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Sonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call NowSonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Sonagachi Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
 
Low Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service Mumbai
Low Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service MumbaiLow Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service Mumbai
Low Rate Call Girls Mumbai Suman 9910780858 Independent Escort Service Mumbai
 
VIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service Lucknow
VIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service LucknowVIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service Lucknow
VIP Call Girls Lucknow Nandini 7001305949 Independent Escort Service Lucknow
 
High Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service Jaipur
High Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service JaipurHigh Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service Jaipur
High Profile Call Girls Jaipur Vani 8445551418 Independent Escort Service Jaipur
 
Call Girl Service Bidadi - For 7001305949 Cheap & Best with original Photos
Call Girl Service Bidadi - For 7001305949 Cheap & Best with original PhotosCall Girl Service Bidadi - For 7001305949 Cheap & Best with original Photos
Call Girl Service Bidadi - For 7001305949 Cheap & Best with original Photos
 
sauth delhi call girls in Bhajanpura 🔝 9953056974 🔝 escort Service
sauth delhi call girls in Bhajanpura 🔝 9953056974 🔝 escort Servicesauth delhi call girls in Bhajanpura 🔝 9953056974 🔝 escort Service
sauth delhi call girls in Bhajanpura 🔝 9953056974 🔝 escort Service
 
Book Call Girls in Yelahanka - For 7001305949 Cheap & Best with original Photos
Book Call Girls in Yelahanka - For 7001305949 Cheap & Best with original PhotosBook Call Girls in Yelahanka - For 7001305949 Cheap & Best with original Photos
Book Call Girls in Yelahanka - For 7001305949 Cheap & Best with original Photos
 
Call Girls In Andheri East Call 9920874524 Book Hot And Sexy Girls
Call Girls In Andheri East Call 9920874524 Book Hot And Sexy GirlsCall Girls In Andheri East Call 9920874524 Book Hot And Sexy Girls
Call Girls In Andheri East Call 9920874524 Book Hot And Sexy Girls
 
Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...
Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...
Call Girls Electronic City Just Call 7001305949 Top Class Call Girl Service A...
 
Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...
Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...
Housewife Call Girls Bangalore - Call 7001305949 Rs-3500 with A/C Room Cash o...
 
Book Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbers
Book Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbersBook Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbers
Book Call Girls in Kasavanahalli - 7001305949 with real photos and phone numbers
 
Call Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort Service
Call Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort ServiceCall Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort Service
Call Girls Service In Shyam Nagar Whatsapp 8445551418 Independent Escort Service
 
Kolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Kolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call NowKolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
Kolkata Call Girls Services 9907093804 @24x7 High Class Babes Here Call Now
 
Housewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment Booking
Housewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment BookingHousewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment Booking
Housewife Call Girls Hoskote | 7001305949 At Low Cost Cash Payment Booking
 
Glomerular Filtration and determinants of glomerular filtration .pptx
Glomerular Filtration and  determinants of glomerular filtration .pptxGlomerular Filtration and  determinants of glomerular filtration .pptx
Glomerular Filtration and determinants of glomerular filtration .pptx
 
Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...
Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...
Call Girls Service in Bommanahalli - 7001305949 with real photos and phone nu...
 
Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...
Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...
Call Girls Frazer Town Just Call 7001305949 Top Class Call Girl Service Avail...
 

#OOP_D_ITS - 9th - Template

  • 1. C++ <Template> 03/11/2009 1 Hadziq Fabroyir - Informatics ITS
  • 2. How to implement ↓ in OOP ?¿ several methods withsame function statementsbut take different type of parameters 03/11/2009 Hadziq Fabroyir - Informatics ITS 2
  • 3. What are you going to code ?¿ - to express swap function for various data type - 03/11/2009 Hadziq Fabroyir - Informatics ITS 3
  • 4. Declare it one by one ?¿ void swapInt (int *a, int *b); void swapFloat (float *c, float *d); void swapChar (char *p, char *q); … // every methods has a specific name 03/11/2009 Hadziq Fabroyir - Informatics ITS 4
  • 5. Make it more general , guys ! 03/11/2009 Hadziq Fabroyir - Informatics ITS 5
  • 6. Function Overloading void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void swap (float *c, float *d) {float temp; temp = *c; *c = *d; *d = temp;} void swap (char *p, char *q) {char temp; temp = *p; *p = *q; *q = temp;} 03/11/2009 Hadziq Fabroyir - Informatics ITS 6
  • 7. Here it is , C++ introduce <template> ! 03/11/2009 Hadziq Fabroyir - Informatics ITS 7
  • 8. Why <templates> C++ promotes code reusability Inheritance and composition provide a way to re-use object code Templates provide a way to re-use source code Example: Queue<T> generic Queue having a type parameter T T can be replaced with actual types 03/11/2009 Hadziq Fabroyir - Informatics ITS 8
  • 9. Why use <templates> Templates are easier to write. You create only one generic version of your class or function instead of manually creating specializations. Templates can be easier to understand, since they can provide a straightforward way of abstracting type information. Templates are type-safe. Because the types that templates act upon are known at compile time, the compiler can perform type checking before errors occur. 03/11/2009 Hadziq Fabroyir - Informatics ITS 9
  • 10. <template> 03/11/2009 Hadziq Fabroyir - Informatics ITS 10
  • 11. Function Template 03/11/2009 Hadziq Fabroyir - Informatics ITS 11
  • 12. What’s it all about Easily create a large range of related functions or classes Function template - the blueprintof the related functions Template function - a specific function made from a function template 03/11/2009 Hadziq Fabroyir - Informatics ITS 12
  • 13. Look at this declaration of Function Template template <class tipe> void swap(tipe &a,tipe &b) { tipe c; c = a; a = b; b = c; } 03/11/2009 Hadziq Fabroyir - Informatics ITS 13
  • 14. Function Usage Example Instance of int void main(void) { int a,b; a = 10; b = 20; swap(a,b); cout << a << “ “ << b << endl; } Instance of float void main(void) { float a,b; a = 10.0; b = 20.0; swap(a,b); cout << a << “ “ << b << endl; } 03/11/2009 Hadziq Fabroyir - Informatics ITS 14
  • 15. <template> effects It’s reusable ► we can gain the time for coding It’s possible to swap 2 objects instantiated from a class 03/11/2009 Hadziq Fabroyir - Informatics ITS 15
  • 16. Overloading Template Function #include <iostream.h> template <class Tipe> void swap(Tipe &a,Tipe &b) { Tipe tmp; tmp = a; a = b; b = tmp; } 03/11/2009 Hadziq Fabroyir - Informatics ITS 16 void swap(float &a,float &b) { float c; a = (a < 0.00)?–a:a; b = (b < 0.00)?–b:b; c = a; a = b; b = c; }
  • 17. Class Template 03/11/2009 Hadziq Fabroyir - Informatics ITS 17
  • 18. Class Template template <class MyTipe> class CMyTemplate { protected : MyTipe m_a,m_b; public : void Done(void) { memset(&m_a,0,sizeof(MyTipe)); } }; 03/11/2009 Hadziq Fabroyir - Informatics ITS 18 /* method is declared outside the class*/ template <class MyTipe> void CMyTemplate<MyTipe>::Init(MyTipe a,MyTipe b) { m_a = a; m_b = b; }
  • 19. Class Template on … struct template <class MyTipe> struct tMyStruct { MyTipe a,b; }; union template <class MyTipe> union _MyUnion { MyTipe a; int b; }; 03/11/2009 Hadziq Fabroyir - Informatics ITS 19
  • 20. Class Template Usage void main(void) { CMyTemplate<short> a; tMystruct<short> b; _MyUnion<float> c; } 03/11/2009 Hadziq Fabroyir - Informatics ITS 20
  • 21. Using 2 Type (Example) template <class MyTipe1,class MyTipe2> class CMyTemplate { protected : MyTipe1 m_a MyTipe2 m_b; public : void Init(MyTipe1 a,MyTipe2 b) { m_a = a; m_b = b; } }; 03/11/2009 Hadziq Fabroyir - Informatics ITS 21 void main(void) { CMyTemplate<short,float> a; }
  • 22. For your practice … Exercise Declare and Define template for your latest Classes of ... Shape (ex: public getColor(), protected color) 2D Shape, 3D Shape (ex: public getArea(), protected edgeLength) Triangle, Circle, Square; Cube, Cylinder, Sphere Lab Session (Senin, 19.00 – 21.00) Please provide: the softcopy(send it by email – deadline Sunday 23:59) the hardcopy(bring it when attending the lab session) 03/11/2009 Hadziq Fabroyir - Informatics ITS 22
  • 23. ☺~ Next: Exception HIndling ~☺ [ 23 ] Hadziq Fabroyir - Informatics ITS 03/11/2009

Notas do Editor

  1. Dalam pemrograman , terutama yang sangat tergantung pada tipe variable , sering kali kita direpotkan dengan harus membuat fungsi yang berfungsi sama tapi dengan tipe variable berbeda.Untuk itu pada C++ dikeluarkan lah sebuah keyword baru , yaitu template. Dengan penggunaan template kita bisa membuat sebuah fungsi yang bisa mendukung segala macam tipe variable , tidak terbatas pada variable yang di definisikan oleh keyword C/C++ tapi juga mendukung penggunaan untuk tipe variable masa depan. Penggunaan template tidak terbatas hanya pada fungsi tapi juga mencakup class ( termasuk juga struct , union)
  2. Secara garis besar penggunaan template dapat dibagi 2 yaitu template pada fungsi (function template)dan template pada Class (class template). Oke saya rasa kita langsung saja membahas penggunaan template.
  3. Dapat dilihat , dengan menggunakan template kita telah menghemat waktu untuk menulis fungsi Pada fungsi swap diatas , juga tidak menutup kemungkinan untuk menukar 2 buah kelas. Pada contoh diatas fungsi tidak akan bekerja jika variable yang ditukar berbeda tipe . Walaupun kekerabatannya cukup dekat . Anda tidak bisa menukar tipe int (16bit) dengan tipe short (16 bit). Ini terkait dengan pendeklarasian template pada fungsi swap. template <class tipe> void swap(tipe &a,tipe &b) Pada pendeklarasian fungsi swap dapat dilihat bahwa formal parameter a sama dengan formal parameter b . Oleh sebab itu sudah seharusnya tipe pada aktual parameter pun harus sama. Untuk contoh fungsi swap diatas sebaiknya anda tidak menggunakan type-casting untuk mengatasi masalah diatas. Mengingat fungsi swap diatas dikirim by reference . (Yang mana dalam pengiriman fungsi menggunakan reference berkaitan dengan pointer). Tentu saja ukuran dari variable menjadi sangat penting karena kita telah merubah ukuran dari variable (type casting) bisa saja terjadi hasil yang tak terduga . Walupun kadang – kadang memberikan hasil yang benar. Kecuali jika ukuran kedua tipe variable tersebut benar - benar sama , anda dapat menggunakan type–casting dalam hal ini. Untuk mendefiniskan lebih dari satu type pada template anda dapat menggnakan koma sebagai pemisah contoh : Template dengan 2 buah tipe template <class tipe1,class tipe2>
  4. Jikalau suatu saat ada sebuah variable yang harus diperlakukan khusus untuk menukarkannya anda dapat mengoverload sebuah template function. Sebagai contoh : misalkan jika anda ingin menukar 2 buah variable float tanpa perlu memperhatikan negatif atau positif . Anda tinggal membuat sebuah fungsi tambahan dengan variable float. Jadi ketika dijalankan compiler akan memprioritaskan dulu pada fungsi yang memiliki formal parameter dan aktual parameter dengan tipe yang sama. Baru kemudian jika tidak ditemukan maka compiler akan membuat sebuah instance dari template function yang telah anda buat. Untuk lebih jelasnya mari kita lihat contoh source code berikut ini .
  5. Jenis template yang berikutnya ialah Class template. Class Template , tidak hanya berlaku untuk pendefinisian template pada class tapi juga berlaku pada pendefinisian struct dan union.Cara pendefinisan sama seperti Funtion template yang beda hanya pada caramembuat instance dari class template tersebut. Untuk lebih jelasnya mari kita perhatikan contoh pendefinisian Class Template.
  6. Untuk menggunakan sebuah class template sebelumnya kita harus mendefiniskan instance dari template yang akan dibuat. Untuk mendefinisikan instance dari class template dapt dilakukan dengan cara menuliskan tipe data dari instance yang diapit oleh tanda “<” ,”>” . void main(void) { CMyTemplate<short> a; // membuat instance CMyTemplate dengan tipe short tMystruct<short> b; // membuat instance tMyStruct dengan tipe short _MyUnion<float> c; // membuat instance _MyUnion dengan tipe float }