??t?e? (templates) - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

??t?e? (templates)

Description:

Title: (templates) Author: stk Last modified by: stk Created Date: 1/12/2005 10:21:21 AM Document presentation format: On-screen Show Company – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 26
Provided by: stk89
Category:
Tags: templates

less

Transcript and Presenter's Notes

Title: ??t?e? (templates)


1
??t?e? (templates)
  • ??a µ?t?a e??a? ??a ??µµ?t? ??d??a p?? pe????e?
    pa?aµ?t???? ?? ?p??e? d????ta? ?? t?µ?? t?p???
    ded?µ????.
  • ???a? ??a? µ??a??sµ?? ??a t? d?µ??????a
    p????aµµ?t?? a?e???t?t?? ap? t?? t?p? t??
    ded?µ???? p?? ?a epe?e??ast???.
  • ?.?. ?? T e??a? pa?aµet????? t?p?? ? d???s?
  • ? x,y,z
  • se ??a ??µµ?t? ??d??a, ?a ???se? t?? t?p? t??
    µetaß??- t?? x,y ?a? z ?ta? ?? t?µ? t?? T d??e?
    ??a? t?p?? ded?µ?- ???.

2
  • O? pa?aµet????? t?p?? µp????? ?a d?????, e?t??
    ap? t??? ßas????? t?p??? ded?µ???? p?? ????e? ?
    C ?a? t?p?? ded?µ???? p?? ??????ta? ap? t??
    ???st?, ?p?? t?p?? ???se??, d?µ??, e??se?? ??p.
  • ? C ep?t??pe? st?? s??a?t?se??, st?? ???se??
    ?a??? ?a? se ???e? d?µ?? ?a pe???????
    pa?aµet?????? t?p???. ?ts? ? ??d??a? ???eta?
    a?e???t?t?? ap? t? e?d?? t?? ded?µ???? p?? ?a t??
    ???s?µ?p???s???.
  • ??a µ?t?a de? ???p??e? e?te??s?µ? ??d??a, ?
    ??d??a? ???p??e?ta? ?ta? d??e? st?? pa?aµ?t????
    ?? t?µ? ??a? s???e???µ???? t?p?? ded?µ????.

3
??t?e? s??a?t?se??
  • O? µ?t?e? s??a?t?se?? ??????ta? s??a?t?- se?? ??
    ?p??e? ?? ???sµata d????ta? pa?a- µ?t????. ?
    e?te??s?µ?? ??d??a? t?? s????t?s?? ???p??e?ta?
    ?ta? st? ??s? t?? pa?aµ?t??? d??e? s???e???µ????
    t?p?? ded?µ????.
  • ????s? µ?t?a? s????t?s??
  • template ltclass Tgt
  • ?p?st?ef?µe??_t?µ? ???µa(???sµata)

4
  • ?.?
  • template ltclass Tgt
  • T power(T a,int exp)
  • T ans a
  • while(--expgt0)
  • ansansa
  • return ans

5
  • St?? ???sµ? µ?a? µ?t?a? s????t?s?? ????µe
  • ??? ?efa??da
  • template ltclass T1,,class Tngt
  • ?p??
  • ? ???? ??e?d? template d????e? ?t? ? s????t?s?
    p?? a??????e? d??eta? pa?aµet?????? t?p???
    ded?µ????.
  • ? ???? class de? s?µa??e? ?t? ?? pa??µet??? e??a?
    st?? t?p? ??p??a? ???s?? ap?? s?µa??e? ?t? e??a?
    pa?aµet????? t?p?? ded?µ????.

6
  • ?? p??t?p? t?? s????t?s??
  • ?p??
  • St?? t?p???? pa?aµ?t???? p?? d??eta? ?? ???sµata
    ? s????t?s? p??pe? ?a a?a???feta? t??????st?? µ?a
    f??? ???e pa?aµet????? t?p?? p?? ????eta? st??
    ?efa??da t?? µ?t?a?.
  • ? eµf???s? t?? pa?aµet????? t?p?? ??
    ep?st?ef?µe??? t?µ?? de? a?t??a??st? t?? a?????
    eµf???s?? t?? a?t?st????? pa?aµ?t??? ?? ???sµa.

7
  • ?? s?µa t?? s????t?s??
  • ?p??
  • ???pe? ?a eµfa???eta? t??????st?? µ?a f??? ???e
    pa??µet??? p?? d??????e st?? ?efa??da.

8
  • ?at? t? µeta???tt?s? t?? ??d??a, st?? ?p???
    pe???aµß????ta? µ?t?e?, ?ta? ? µeta???tt?st??
    ft?se? st? s?µe?? ?p?? ?a?e?ta? µ?a s????t?s?,
    e????e? p??ta a? ?p???e? p??t?p? p?? ?a
    ta?t?-?eta? µe t?? d?ad??as?a ???s?? t??. ?? ???
    e????e? a? ?p???e? µ?t?a st?? ?p??a, a?
    a?t??atasta???? ?? pa??µet??? µe t??? t?p??? p??
    ??????ta? ap? t?? ???s? t?? s????t?s?? ?a
    p?????e? p??t?p? s?µßat? µe t?? s???e???µ???
    ???s?. ?? ?a? d?µ?????e? ?a? e?s?µat??e? st??
    a?t?st???? ??s? e?te??s?µ? ??d??a st?? ?p??? ??e?
    a?t??atast?se? t??? pa?aµet?????? t?p??? µe t???
    t?p??? ded?µ???? p?? apa?te? ? ???s? t??
    s????t?s??.

9
  • ???e st??µ??t?p? t?? µ?t?a? d?µ?????e? ?a? µ?a
    ?a???????a ??d?s? e?te??s?µ?? ??d??a ??a t?
    s????t?s?.
  • ? d?µ??????a ??a? ??d?s?? e?te??s?µ?? ??d??a
    ???eta? µ???? a? de? ??e? d?µ????-???e?
    p??????µ???? st??µ??t?p? t?? µ?t?a? µe t?? ?d??
    t?p? pa?aµ?t???.

10
?a??de??µa
  • include ltiostream.hgt
  • template ltclass Tgt
  • T power(T a,int exp)
  • T ans a
  • while(--expgt0)
  • ansansa
  • return ans
  • void main()
  • int k2,m
  • mpower(k,3) //??µ?????e?ta? ??d??a? ??a
    t? p??t?p? int power(int,int)
  • cout ltlt m ltlt"\n"
  • mpower(k,4) // ???s?µ?p??e?ta? ? ??d??a?
    p?? d?µ?????????e ??a t?

  • // p??t?p? int power(int,int)
  • cout ltlt m ltlt"\n"
  • float r2.5,x
  • xpower(r,3) //??µ?????e?ta? ??d??a? ??a
    t? p??t?p? int power(float,int)
  • cout ltlt x ltlt"\n"

11
  • ???a? p??fa??? ?t? ?? te?est??, ?? s??a?t?se?? ?
    ?? t???? ???e? p???e?? ?? ?p??e? ???s?µ?p?????ta?
    st? es?te???? µ?a? s????t?s??, ? ?p??a ????eta?
    ?? µ?t?a, ?a p??pe? ?a e??a? st???e?a s?µßat? µe
    t??? t?p??? ded?µ???? p?? ?a d????? st? ??s? t??
    pa?aµet????? t?p?? p?? ??????ta? s a?t??. Se
    d?af??et??? pe??pt?s? ?a p??pe? ?a ???s???? ??
    ?at?????e? ep??a???e?? ?ste ?a µp??e? ?a
    ?e?t?????se? ? s????t?s?.

12
?a?t?p???s? t?p???? pa?aµ?t??? se µ?t?e?
s??a?t?se??
  • ?? se µ?a µ?t?a s????t?s?? d??e? d?af??et????
    t?p?? ???sµ?t?? ??a ta ???sµata p?? de? ??????ta?
    pa?aµet???? ? µeta???tt?st?? ap?t?????e? ?a
    d?µ??????se? st??µ??t?p? t?? µ?t?a?. G?a ?a
    ap?f????µe t? s?et??? sf??µa s??des?? (Linking
    error) p??pe? ?a d????e? st? p????aµµa ??a
    p??t?p? s????t?s?? p?? ?a ta?t??eta? µe t?
    p??t?p? t?? µ?t?a? st? ?p??? ?? pa?aµet?????
    t?p?? ???sµ?t?? ?a ????? a?t??atasta?e? µe
    ??p???? s?µßat? µe t? µ?t?a t?p? ded?µ????.

13
  • ?.?.
  • include ltiostream.hgt
  • float power(float a,unsigned int exp) //???t?p?
    ??a t? µetat??p? t?? t?p??
  • template ltclass Tgt
  • T power(T a,unsigned int exp)
  • int i
  • T ra
    //??t?a
  • for(i1 iltexp i)
  • ra
  • return r
  • void main()
  • float t3.5
  • int k2
  • cout ltltpower(k,t)ltlt'\n' // ? t
    µetat??peta? se unsigned int µe t?µ? 3

14
  • ? µeta???tt?st??, se ???e st??µ??t?p? t?? µ?t?a?,
    e????e? t? p??t?p? ?a? µetat??pe? t??? µ?
    pa?aµet?????? t?p??? st?? t?p? p?? d????eta? st?
    p??t?p?. St? ??s? t?? pa?aµ?t??? t?p??ete? t??
    t?p? ded?µ???? p?? d??eta? st?? a?t?st???? ??s?
    µe t?? ???s? t?? s????t?s??.

15
?a?aµet??p???s? t?p??
  • ??t?? ap? s??a?t?se?? µp????µe ?a
    pa?aµet??p???s??µe ?a? t?p??? ded?µ???? p??
    ??????ta? ap? t?? ???st?. ?ts? µp????µe ?a
    ???s??µe ???se??, d?µ?? ? ?????? t?p??? s???et??
    ded?µ???? ?? ?p???? ?a ??????? es?te???? ded?µ??a
    ta ?p??a ?a d??????ta? se a?t??? pa?aµet????.

16
????s? µ?t?a? ??a pa?aµet??p???s? ???se??
  • templatelt ??sta pa?aµ?t??? ?a? sta?e??? gt
  • class name
  • .
  • .
  • T data_name
  • .
  • .

17
? d???s? µ?t?a? ??a t?? pa?aµet??p???s? µ?a?
???se?? pe????e?
  • ??? ?efa??da
  • templatelt ??sta pa?aµ?t??? ?a? sta?e??? gt
  • ? ?p??a pe???aµß??e?
  • ????se?? pa?aµet????? t?p?? ?? ?p??e?
    e?s????ta? µe t? ???? ??e?d? class.
  • ????se?? t?p?? ??a sta?e??? ?? t?µ?? t??
    ?p???? ?a d????? ?at? t? d?µ??????a t??
    st??µ??t?p?? t?? ???s??.

18
  • ?? d???s? t?? ???s??
  • st?? ?p??a µp????? ?a ?p?????? d???se??
    µetaß??t?? st?? t?p? t?? pa?aµ?t??? p?? ??????ta?
    st?? ?efa??da ?a? ?a ???eta? ???s? t?? sta?e???
    p?? d??????ta? se a?t??.

19
  • ?.?.
  • include ltiostream.hgt
  • template ltclass TYPE, int SIZEgt
  • class A
  • TYPE aSIZE
  • public
  • A()
  • void out()
  • template ltclass TYPE,int SIZEgt
  • AltTYPE,SIZEgtA()
  • int i
  • for(i0 iltSIZE i)
  • cin gtgtai
  • template ltclass TYPE,int SIZEgt
  • void AltTYPE,SIZEgtout()
  • int i

20
  • ?f?s?? µ?a ???s? d????eta? ?? µ?t?a ??
    s??a?t?se?? µ??? p?? a?????? s a?t?? p??pe? ?a
    d??????? ep?s?? µe t? µ??f? µ?t?a?.
  • ? d???s? µ?a? t?t??a? s????t?s?? apa?te? e?t??
    ap? t? ???µa t?? ???s?? st?? ?p??a a???e?, ?a
    a?af????ta? ?a? ta ???µata t?? pa?aµ?t??? t??
    ?p??e? d??eta? ? ???s?.
  • ?e? apa?te?ta? ?a ?p???e? a?af??? t?? pa?aµ?t???
    sta ???sµata ?a? t? s?µa t?? s????t?s?? ?p?? ?a
    s???ßa??e a? ? s????t?s? d?????ta? ?? µ?t?a
    a?t???µa.

21
  • ? d???s? µ?a? s????t?s?? µ???? se µ?a µ?t?a
    ???s?? p??pe? ?a pe????e? e?t?? ap? t? ???µa t??
    ???s??, p??? ap? t?? te?est? eµß??e?a? µ?sa se
    ltgt ?a? ta ???µata t?? pa?aµ?t??? t?? ?p??e?
    ???s?µ?p??e? ? µ?t?a. ???µ? p??? ap? t? d???s?
    p??pe? ?a p????e?ta? ? ?efa??da templatelt??sta
    pa?aµ?t???gt ? ?p??a ???s?µ?p??e?ta? ??a t? d???s?
    t?? ???s?? ?? µ?t?a.

22
????s? s????t?s?? µ????? µ?t?a?
  • templatelt??sta pa?aµ?t???gt
  • ??p?? ???s?lt???µata pa?aµ?t???gt???µa_s????t?s??(
    .)
  • ?.?
  • template ltclass TYPE,int SIZEgt
  • void AltTYPE,SIZEgtout()
  • ..

23
  • ?at? t? d?µ??????a a?t??e?µ???? st?? t?p? µ?a?
    ???s?? p?? p?????eta? ap? µ?t?a p??pe? e?t?? t??
    ltgt ?a d????e? ? t?p?? t?? ded?µ???? ??a t???
    pa?aµet??p???µ????? t?p??? ?a? ?? t?µ?? t??
    sta?e??? p?? d??????ta? st?? ?efa??da template.
  • ?.?.
  • Altfloat,4gt a

24
  • G?a ???e st??µ??t?p? st?? t?p? µ?a? µ?t?a? ?
    µeta???tt?st?? d?µ?????e? e?te??s?µ? ??d??a ??a
    µ?a ?a???????a ???s? a?t??a??st??ta? t??
    pa?aµ?t???? ?a? t?? t?µ?? t?? sta?e??? µe a?t??
    p?? d??????ta? st?? a?t?st???? ??s? st?
    p????aµµa.
  • ???? e?te??s?µ?? ??d??a? ?a d?µ???????e? a??µ?
    ?a? a? d?? st??µ??t?pa ????? t?? ?d?? t?p?
    pa?aµ?t??? a??? d?af????? st?? t?µ?? t?? sta?e???
    p?? d??????ta? pa?aµet????.
  • ?? ??a ??p??a a?t??e?µe?a d????e? ? ?d??? t?p??
    pa?aµ?t??? ?a? ?? ?d?e? t?µ?? ??a t?? sta?e???
    t?te d?µ?????e?ta? µ??? µ?a ??d?s? t?? ???s?? ??a
    a?t? ta a?t??e?µe?a.

25
  • ?.?. ?? d???se??
  • Altint, 3gt a
  • Altint ,4gt b
  • Altint , 3gt c
  • Ta d?µ??????s??? ????? ??d??a ??a ta
    a?t??e?µe?a a ?a? c e?? ?e????st?? ??d??a? ?a
    d?µ???????e? ??a t? a?t??e?µe?? b.
Write a Comment
User Comments (0)
About PowerShow.com