C ?????? - PowerPoint PPT Presentation

1 / 65
About This Presentation
Title:

C ??????

Description:

Title: Author: zhengli Last modified by: zhengli Created Date: 6/7/1999 7:18:19 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 66
Provided by: zhe70
Category:
Tags:

less

Transcript and Presenter's Notes

Title: C ??????


1
??? ?????
C??????
  • ???? ? ?

2
??????
  • ???????
  • ????????
  • ???????
  • ???????????
  • ?????????

3
???????
  • ?????????????????????
  • ????????????????????????????
  • ???????????(???)?
  • ????????????

4
?????????
???????
5
?????????
???????
6
?????????
???????
7
?????????
???????
8
????????
???????
  • ????????????
  • ????????????,????????(???????)?,????????????

9
??????
???????
  • class ???????? ???
  • ????

10
????
????????
  • ??????????????
  • ???????????????
  • ?????????????????
  • ??????
  • ????
  • ????
  • ????

11
????(public)
????????
  • ???public?protected????????????????,????private???
    ??????
  • ???????????????????public?protected??,??????????pr
    ivate???
  • ???????????????public???

12
?7-1 ??????
????????
  • class Point //??Point????
  • public //??????
  • void InitP(float xx0, float yy0)
  • XxxYyy
  • void Move(float xOff, float yOff)
  • XxOffYyOff
  • float GetX() return X
  • float GetY() return Y
  • private //??????
  • float X,Y

13
  • class Rectangle public Point //?????
  • public //????????
  • void InitR(float x, float y, float w, float h)
  • InitP(x,y)WwHh//??????????
  • float GetH() return H
  • float GetW() return W
  • private //????????
  • float W,H

13
14
  • includeltiostreamgt
  • includeltcmathgt
  • using namecpace std
  • int main()
  • Rectangle rect
  • rect.InitR(2,3,20,10)
  • //???????????????
  • rect.Move(3,2)
  • coutltltrect.GetX()ltlt','
  • ltltrect.GetY()ltlt','
  • ltltrect.GetH()ltlt','
  • ltltrect.GetW()ltltendl
  • return 0

14
15
????(private)
????????
  • ???public?protected????private?????????,????privat
    e?????????
  • ???????????????????public?protected??,??????????pr
    ivate???
  • ???????????????????????

16
?7-2 ??????
????????
  • class Rectangle private Point //?????
  • public //??????
  • void InitR(float x, float y, float w, float h)
  • InitP(x,y)WwHh //????????
  • void Move(float xOff, float yOff)
    PointMove(xOff,yOff)
  • float GetX() return PointGetX()
  • float GetY() return PointGetY()
  • float GetH() return H
  • float GetW() return W
  • private //??????
  • float W,H

17
  • includeltiostreamgt
  • includeltcmathgt
  • using namecpace std
  • int main()
  • //???????????????
  • Rectangle rect
  • rect.InitR(2,3,20,10)
  • rect.Move(3,2)
  • coutltltrect.GetX()ltlt',' ltltrect.GetY()ltlt','
  • ltltrect.GetH()ltlt','ltltrect.GetW()ltltendl
  • return 0

17
18
????(protected)
????????
  • ???public?protected????protected?????????,????priv
    ate?????????
  • ???????????????????public?protected??,??????????pr
    ivate???
  • ??????????????????????

19
protected ????????
????????
  • ??????????????,?? private ????????
  • ????????,?? public ????????
  • ????????,?????,???????

20
?7-3 protected ????
????????
  • class A
  • protected
  • int x
  • int main()
  • A a
  • a.x5 //??

21
  • class A
  • protected
  • int x
  • class B public A
  • public
  • void Function()
  • void BFunction()
  • x5 //??

21
22
??????
????
  • ????????????????????????,???????????
  • ?????????????????
  • ?????????????????
  • ????????????????
  • ??????????????????????

23
?7-4 ????????
????
  • include ltiostreamgt
  • using namecpace std
  • class B0 //??B0??
  • public
  • void display()coutltlt"B0display()"ltltendl //??
    ????

24
  • class B1 public B0
  • public
  • void display()coutltlt"B1display()"ltltendl
  • class D1 public B1
  • public
  • void display()coutltlt"D1display()"ltltendl
  • void fun(B0 ptr)
  • ptr-gtdisplay() //"????-gt???"

24
25
  • int main() //???
  • B0 b0 //??B0???
  • B1 b1 //??B1???
  • D1 d1 //??D1???
  • B0 p //??B0???
  • pb0 //B0?????B0???
  • fun(p)
  • pb1 //B0?????B1???
  • fun(p)
  • pd1 //B0?????D1???
  • fun(p)

???? B0display() B0display() B0display()
25
26
???????????
???????
  • ???
  • ????????????
  • ???
  • ???????????
  • ????
  • ?????????????????
  • ????
  • ????????,????????

27
??????????
???????
  • class ????????1 ???1,????2 ???2,...
  • ????
  • ?????????,?????????????????

28
?????
???????
  • private
  • int b
  • class C public A, private B
  • public
  • void setC(int, int, int)
  • void showC()
  • private
  • int c
  • class A
  • public
  • void setA(int)
  • void showA()
  • private
  • int a
  • class B
  • public
  • void setB(int)
  • void showB()

29
  • void AsetA(int x)
  • ax
  • void BsetB(int x)
  • bx
  • void CsetC(int x, int y, int z)
  • //????????????
  • //????
  • setA(x)
  • setB(y)
  • cz
  • //???????
  • int main()
  • C obj
  • obj.setA(5)
  • obj.showA()
  • obj.setC(6,7,9)
  • obj.showC()
  • // obj.setB(6) ??
  • // obj.showB() ??
  • return 0

29
30
????????
???????????
  • ???????????,????????????????
  • ???????,????????????????,?????????????,???????????
    ??
  • ??????????????????????

31
??????????
???????????
  • ????????(???????,?????????)???(???)
  • ???????????

32
????????????
???????????
  • includeltiostreamgt
  • using namecpace std
  • class B
  • public
  • B()
  • B(int i)
  • B()
  • void Print() const
  • private
  • int b

33
  • BB()
  • b0
  • coutltlt"B's default constructor called."ltltendl
  • BB(int i)
  • bi
  • coutltlt"B's constructor called." ltltendl
  • BB()
  • coutltlt"B's destructor called."ltltendl
  • void BPrint() const
  • coutltltbltltendl

33
34
  • class Cpublic B
  • public
  • C()
  • C(int i,int j)
  • C()
  • void Print() const
  • private
  • int c

34
35
  • CC()
  • c0
  • coutltlt"C's default constructor called."ltltendl
  • CC(int i,int j)B(i)
  • cj
  • coutltlt"C's constructor called."ltltendl
  • CC()
  • coutltlt"C's destructor called."ltltendl
  • void CPrint() const
  • BPrint() coutltltcltltendl
  • int main()
  • C obj(5,6) obj.Print()

35
36
?????????
???????????
  • ????????(??1??,??2??,...??n??,????)???1(??),
    ???2(??), ...???n(??)
  • ???????????

37
???????????
???????????
  • ?????????????????????????,??????????????????????
  • ???????????,??????????,????????????
  • ???????????????,???????????????,??????????????

38
???????????????
???????????
  • ????????(??1??,??2??,...??n??,????)???1(??),
    ???2(??), ...???n(??),??????????
  • ???????????

39
?????????
???????????
  • 1. ????????,?????????????????(????)?
  • 2. ???????????,?????????????????
  • 3. ??????????????

40
??????
???????????
  • ???????????????????,?????????????????????
  • ?????????????,??????????????????????
  • CC(C c1)B(c1)

41
?7-5 ?????????
???????????
  • include ltiostreamgt
  • using namecpace std
  • class B1 //??B1,???????
  • public
  • B1(int i) coutltlt"constructing B1 "ltltiltltendl
  • class B2 //??B2,???????
  • public
  • B2(int j) coutltlt"constructing B2 "ltltjltltendl
  • class B3 //??B3,???????
  • public
  • B3()coutltlt"constructing B3 "ltltendl

42
  • class C public B2, public B1, public B3
  • public //????????
  • C(int a, int b, int c, int d)
  • B1(a),memberB2(d),memberB1(c),B2(b)
  • private //??????????
  • B1 memberB1
  • B2 memberB2
  • B3 memberB3
  • int main()
  • C obj(1,2,3,4)

???? constructing B2 2 constructing B1
1 constructing B3 constructing B1
3 constructing B2 4 constructing B3
42
43
????????
???????????
  • ?????????,???????
  • ???????(??????)?????????
  • ???????????????,??????????
  • ?????????????????

44
?7-6 ?????????
???????????
  • include ltiostreamgt
  • using namecpace std
  • class B1 //??B1??
  • public
  • B1(int i) coutltlt"constructing B1 "ltltiltltendl
  • B1() coutltlt"destructing B1 "ltltendl
  • class B2 //??B2??
  • public
  • B2(int j) coutltlt"constructing B2
    "ltltjltltendl
  • B2() coutltlt"destructing B2 "ltltendl
  • class B3 //??B3??
  • public
  • B3()coutltlt"constructing B3 "ltltendl
  • B3() coutltlt"destructing B3 "ltltendl

45
  • class C public B2, public B1, public B3
  • public
  • C(int a, int b, int c, int d)
  • B1(a),memberB2(d),memberB1(c),B2(b)
  • private
  • B1 memberB1
  • B2 memberB2
  • B3 memberB3
  • int main()
  • C obj(1,2,3,4)

45
46
?7-6 ????
  • constructing B2 2
  • constructing B1 1
  • constructing B3
  • constructing B1 3
  • constructing B2 4
  • constructing B3
  • destructing B3
  • destructing B2
  • destructing B1
  • destructing B3
  • destructing B1
  • destructing B2

47
??????
???????????
  • ??????????????
  • ??????,??????????????????????
  • ??????????????????????,?????????

48
?7-7 ?????????
???????????
  • include ltiostreamgt
  • using namecpace std
  • class B1 //????B1
  • public //????
  • int nV
  • void fun() coutltlt"Member of B1"ltltendl
  • class B2 //????B2
  • public //????
  • int nV
  • void fun()coutltlt"Member of B2"ltltendl
  • class D1 public B1, public B2
  • public
  • int nV //??????
  • void fun()coutltlt"Member of D1"ltltendl //??????

49
  • int main()
  • D1 d1
  • d1.nV1 //???.?????, ??D1???
  • d1.fun()
  • d1.B1nV2 //????????, ????B1??
  • d1.B1fun()
  • d1.B2nV3 //????????, ????B2??
  • d1.B2fun()

49
50
?????
???????????
  • ?????,????????,????????????,??????????(????)????
    ?(?8?)???????????
  • ???????????,??????????????,??????????????,??????
    ?????????

51
???????(?)
???????????
  • class C public A, piblic B
  • public
  • void g()
  • void h()
  • ????C c1
  • ? c1.f() ?????
  • ? c1.g() ????(????)
  • class A
  • public
  • void f()
  • class B
  • public
  • void f()
  • void g()

52
????????
???????????
  • ???????????c1.Af() ? c1.Bf()
  • ??????????C ???????????f(),f()??????? Af()
    ? Bf()

53
???????(?)
???????????
  • class B
  • public
  • int b
  • class B1 public B
  • private
  • int b1
  • class B2 public B
  • private
  • int b2
  • class C public B1,public B2
  • public
  • int f()
  • private
  • int d

54
  • ???C???????????
  • ????
  • c.B1b
  • c.B2b

???? C c c.b c.Bb
54
55
???
  • ??????
  • ??????????
  • ??
  • ?virtual???????class B1virtual public B
  • ??
  • ?????????????????????????????????.
  • ????????????????,??????????
  • ??
  • ?????????????????????

56
?????
? ? ?
  • class B private int b
  • class B1 virtual public B private int b1
  • class B2 virtual public B private int b2
  • class C public B1, public B2 private float
    d
  • ?????????
  • C cobj
  • cobj.b

57
  • ????????????????

57
58
?7-8?????
? ? ?
59
59
60
  • include ltiostreamgt
  • using namecpace std
  • class B0 //????B0
  • public //????
  • int nV
  • void fun()coutltlt"Member of B0"ltltendl
  • class B1 virtual public B0 //B0????,??B1?
  • public //??????
  • int nV1
  • class B2 virtual public B0 //B0????,??B2?
  • public //??????
  • int nV2

60
61
  • class D1 public B1, public B2 //???D1??
  • public //??????
  • int nVd
  • void fund()coutltlt"Member of D1"ltltendl
  • int main() //?????
  • D1 d1 //??D1???d1
  • d1.nV2 //????????
  • d1.fun()

61
62
????????????
? ? ?
  • ?????????????(?)????
  • ????????????????????????????????????
  • ????????,????????????????,????????????????????????
    ????????????,?????????????????
  • ??????,?????????????????????,?????????????????????
    ???

63
????????????
? ? ?
  • include ltiostreamgt
  • using namecpace std
  • class B0 //????B0
  • public //????
  • B0(int n) nVn
  • int nV
  • void fun()coutltlt"Member of B0"ltltendl
  • class B1 virtual public B0
  • public
  • B1(int a) B0(a)
  • int nV1
  • class B2 virtual public B0
  • public
  • B2(int a) B0(a)
  • int nV2

64
  • class D1 public B1, public B2
  • public
  • D1(int a) B0(a), B1(a), B2(a)
  • int nVd
  • void fund()coutltlt"Member of D1"ltltendl
  • int main()
  • D1 d1(1)
  • d1.nV2
  • d1.fun()

64
65
????
  • ?7-10(????)
  • ?????????
  • ????????pay() ??????,????????????,?????
  • ??main()???,???????????,???????????,??????????????
    ,???????
Write a Comment
User Comments (0)
About PowerShow.com