De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Tinpro015b-les6 Templates.

Verwante presentaties


Presentatie over: "Tinpro015b-les6 Templates."— Transcript van de presentatie:

1 tinpro015b-les6 Templates

2 Functietemplates void verwissel(int &x , int &y); { int w=x;x=y;y=w;}
Dezelfde functie voor meerdere types heet een functietemplate Er is een gegeneraliseerd type Voorbeeld: template <class T> void verwissel(T &x , T &y); { T w=x;x=y;y=w;}

3 Functietemplates int main() { int i= 1,j=2; float u=3.4 , v=5.6;
verwissel(i,j); // verwissel 2 int objecten verwissel(u,v); // verwissel 2 float objecten cout << i << “ “<< j << endl ; cout << u << “ “<< v << endl ; return (0); }

4 Functietemplates In het programma worden 2 functies gegenereerd
void verwissel(int &x , int &y); { int w=x;x=y;y=w;} void verwissel(float &x , int &y); { float w=x;x=y;y=w;} template <class T> Het woord class is opmerkelijk , omdat we met types werken

5 Meer dan 1 template-argument
We willen uitrekenen: x = an Als a = double , n = integer  x = double (floating point) Als a = integer , n = integer  x = long ( integer) Een functietemplate MACHT voor deze 2 situaties 2 types: grondtaltype G (a) resultaattype R (x)

6 Meer dan 1 template-argument
template <class G , class R> R macht(G a , int n , R &x) // x =an { x=1; for (int i=1; i <=n ; i++ ) x=x*a; return x; } int main() { double x1 ; macht(10.0 , 15, x1); cout << x1; // x1=10.015 long x2 ; macht(2 , 17, x2); cout << x2; // x2=217 long x3 ; cout << macht(2 , 20, x3 ); // x3=220 return 0; }

7 Meer dan 1 template-argument
<class G , class R> Hier worden 2 template-argumenten gebruikt R macht(G a , int n , R &x) een template-argument kan ook gebruikt worden voor return-waarde

8 Klassetemplates Zelfde verhaal als bij functietemplates
2 classes verschillen alleen qua types class 1 float class 2 int Daarom 1 klassetemplate Voorbeeld met vectoren

9 Klassetemplates template <class T> class vec { public:
vec(T xx =0, T yy =0 ) { x=xx ; y=yy; } void printvec() ; vec <T> operator+( vec <T> &a, vec <T> &b) { return vec <T> ( a.x+ b.x, a.y +b.y);} private: T x, y; };

10 Klassetemplates template <class T>
void vec <T>:: printvec() ; { cout << x <<“ “ << y << endl;} main() { vec <int> iu(1,2), iv(3,4) ; isom; vec <float> fu(1.1,2.2), fv(3.3,4.4) ; fsom; isom=iu + iv; fsom=fu + fv; isom.printvec(); fsom.printvec(); return 0; }

11 Klassetemplates Resultaat

12 Klassetemplates Klasse-template: template < template-argument-list> Concreet: template <class T> vec is een geparametriseerd type met T als parameter Er zijn in dit voorbeeld 2 Template-class-names vec <int> ; vec <float> Er worden a.h.w. 2 klassen gebruikt bijv: vec_int en vec_float

13 Klassetemplates Argument list kan ook een identifier bevatten zoals bij een functiedeclaratie template <int n> class floatrij{ public : float r[n] …..}; rij a 100 float , rij b 200 float declaratie: floatrij<100>a; floatrij<200>b; aanroep: a.r[i]; // i max 99 ; b.r[j]; // j max 199 voordeel t.o.v. array: a en b behoren tot een klasse : lidfuncties mogelijk

14 Klassetemplates combineren met type- argumenten
template <class T, int n, class S> class rij { ….;} voorbeeld template-class-name: rij <int,100,long>

15 Default-argumenten klassetemplates
template <class T ,int n, class S=T> nu kun je het 3e argument weglaten rij<float,3,float> a_float(0.3F , 0.1F); vervangen door: rij<float ,3> a_float(0.3F,0.1F);

16 Huiswerk les 6 Maak de opgaven 9.1, 9.2 uit C++ boek
Bestudeer Hfdst 11 uit C++boek


Download ppt "Tinpro015b-les6 Templates."

Verwante presentaties


Ads door Google