?C ?,?????(polymorphism)??: ??????????????,????????????????,????????????????????? - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

?C ?,?????(polymorphism)??: ??????????????,????????????????,?????????????????????

Description:

Title: PowerPoint Presentation Author: yongzhang Last modified by: wbx Created Date: 1/1/1601 12:00:00 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 12
Provided by: yon130
Category:

less

Transcript and Presenter's Notes

Title: ?C ?,?????(polymorphism)??: ??????????????,????????????????,?????????????????????


1
????????? 2014?9?
? ? ? ? ?
2
?13? ??????
  • 1 ??
  • 2 ???
  • 3???
  • 4????

3
??
  • ?????????????????????????,???????????,????????,???
    ????,???????????????????????,????????????????

4
???
  • ?C?,?????(polymorphism)?? ??????????????,??????
    ??????????,?????????????????????
  • ???????????????????,?????????????(polymorphism)???
    ?????????????

5
//?13-1 ???????
  • include ltiostreamgt
  • using namespace std
  • template lttypename Tgt
  • T max(const T lsh, const T rhs)
  • return (lsh gt rhs) ? lsh rhs
  • void main()
  • int a 3 int b 4
  • cout ltlt max(a, b) ltlt endl
  • float c 2.4 float d 1.2
  • cout ltlt max(c, d) ltlt endl

6
//?13-2 ??????
  • include ltiostreamgt
  • using namespace std
  • int max (int a, int b)
  • return (a gt b) ? a b
  • int max (int a, int b, int c)
  • return max(max(a, b), c)
  • void main()
  • int a 3, b 4, c 5
  • cout ltlt max(a, b) ltlt endl
  • cout ltlt max(a, b, c) ltlt endl

7
3???
  • ?????????????????????????????????????????????????
    ????????????????????,????????????

8
  • //?13-3??????
  • // pointers to base class
  • include ltiostreamgt
  • using namespace std
  •  
  • class CPolygon
  • protected
  • int width, height
  • public
  • void set_values (int a, int b)
  • widtha heightb
  •  
  • class CRectangle public CPolygon
  • public
  • int area ()
  • return (width height)
  •  

9
  • class CTriangle public CPolygon
  • public
  • int area ()
  • return (width height / 2)
  •  
  • int main ()
  • CRectangle rect //??CRectangle???rect
  • CTriangle trgl //??CTriangle???trgl
  • CPolygon ppoly1 rect //????ppoly1,????CP
    olygon???,??????rect
  • CPolygon ppoly2 trgl //????ppoly2,????CPo
    lygon???,??????trgl
  • ppoly1-gtset_values (4,5)
  • ppoly2-gtset_values (4,5)
  • cout ltlt rect.area() ltlt endl
  • cout ltlt trgl.area() ltlt endl
  • return 0

10
4????
  • //?13-8 ?3???????(??????????????????)?
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int max(int a,int b,int c) //????
  • double max(double a,double b,double c) //????
  • long max(long a,long b,long c) //????
  • int i1 2, i2 3, i3 4, i
    //?????3???
  • imax(i1,i2,i3)
    //?3????????
  • coutltlt"i_max"ltltiltltendl
  • double d1 11.53,d2 12.72, d3 13.88, d
    //?????3?????
  • dmax(d1,d2,d3)
    //?3??????????
  • coutltlt"d_max"ltltdltltendl
  • long g1 67854, g2 -912456, g3 673456, g
    //?????3????
  • gmax(g1,g2,g3)
    //?3?????????
  • coutltlt"g_max"ltltgltltendl

11
  •  
  • int max(int a,int b,int c)
    //???3???????????
  • if(bgta) ab
  • if(cgta) ac
  • return a
  • double max(double a,double b,double c)
    //???3?????????????
  • if(bgta) ab
  • if(cgta) ac
  • return a
  • long max(long a,long b,long c)
    //???3????????????
  • if(bgta) ab
  • if(cgta) ac
  • return a
Write a Comment
User Comments (0)
About PowerShow.com