Upload
hadziq-fabroyir
View
1.047
Download
0
Tags:
Embed Size (px)
Citation preview
12/04/2023Hadziq Fabroyir - Informatics ITS 1
C++ <Template>
12/04/2023Hadziq Fabroyir - Informatics ITS 2
How to implement ↓ in
OOP ?¿several methods with
same function statementsbut take different type of parameters
12/04/2023Hadziq Fabroyir - Informatics ITS 3
What are you going to code ?¿
- to express swap function for various data type -
12/04/2023Hadziq Fabroyir - Informatics ITS 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
12/04/2023Hadziq Fabroyir - Informatics ITS 5
Make it more general , guys !
One by one declarationFunction Overloading
12/04/2023Hadziq Fabroyir - Informatics ITS 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; }
12/04/2023Hadziq Fabroyir - Informatics ITS 7
Here it is , C++ introduce
<template> !One by one declarationFunction Overloading
Template
12/04/2023Hadziq Fabroyir - Informatics ITS 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
12/04/2023Hadziq Fabroyir - Informatics ITS 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.
12/04/2023Hadziq Fabroyir - Informatics ITS 10
<template>
<template>
Function Class
12/04/2023Hadziq Fabroyir - Informatics ITS 11
Function Template
12/04/2023Hadziq Fabroyir - Informatics ITS 12
What’s it all about
Easily create a large range of related functions or classes
Function template - the blueprint of the related functions
Template function - a specific function made from a function template
12/04/2023Hadziq Fabroyir - Informatics ITS 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;
}
12/04/2023Hadziq Fabroyir - Informatics ITS 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;
}
12/04/2023Hadziq Fabroyir - Informatics ITS 15
<template> effects
It’s reusable ► we can gain the time for coding
It’s possible to swap 2 objects instantiated from a class
12/04/2023Hadziq Fabroyir - Informatics ITS 16
Overloading Template Function
#include
<iostream.h>
template <class Tipe>
void swap (Tipe
&a,Tipe &b)
{
Tipe tmp;
tmp = a;
a = b;
b = tmp;
}
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;
}
12/04/2023Hadziq Fabroyir - Informatics ITS 17
Class Template
12/04/2023Hadziq Fabroyir - Informatics ITS 18
Class Templatetemplate <class MyTipe>
class CMyTemplate
{
protected :
MyTipe m_a,m_b;
public :
void Done(void) {
memset (&m_a, 0,
sizeof(MyTipe) );
}
};
/* method is declared
outside the class */
template <class MyTipe>
void
CMyTemplate<MyTipe
>::Init (MyTipe
a,MyTipe b)
{
m_a = a;
m_b = b;
}
12/04/2023Hadziq Fabroyir - Informatics ITS 19
Class Template on …
struct
template <class
MyTipe>
struct tMyStruct
{
MyTipe a,b;
};
union
template <class
MyTipe>
union _MyUnion
{
MyTipe a;
int b;
};
12/04/2023Hadziq Fabroyir - Informatics ITS 20
Class Template Usage
void main(void)
{
CMyTemplate<short> a;
tMystruct<short> b;
_MyUnion<float> c;
}
12/04/2023Hadziq Fabroyir - Informatics ITS 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;
} };
void main(void) {
CMyTemplate<short,float> a;
}
12/04/2023Hadziq Fabroyir - Informatics ITS 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)
12/04/2023Hadziq Fabroyir - Informatics ITS
☺~ Next: Exception HIndling ~☺
[ 23 ]