Title: ????????????????
1?12? ?????
- ??
- ??
- ???????
- ????????
- ???
- ????????
2??????
- ??????????????????,????????????????
- ?????
- ??????????
- ???????????????????????????????????,??????????????
?
3???
- ??????
- ??????????????
- ????????????????
- ????????
- ???????
- ???????????????
4??????
- ????
- class ???????? ???
- //???????????????
-
- ????
- ???? public
- ????private
- ????protected
5????
- class base
- int x
- public
- void setx(int k)
-
- class derived1public base
- int y
- public
- void sety(int k)
-
Derived1???????x,y? ???????setx?sety
6???????????
- ?????????????????????
- protected????
- protected????????????,????????????????????,???????
?????? - protected?????????,???protected?????,?????????
7???
- ??????
- ??????????????
- ????????????????
- ????????
- ???????
- ???????????????
8????????????
????? ????? ????
public?? protected?? private??
public ??????public ??????protected ??????private ??????static ????????static ????????static ?????????? ????????? ????????? ??????? ?? ??
protecetd ??????proteced ??????protected ?????private ????????static ???????????
private ??????? ??????? ??????? ???????public ?protected??????static???????????
9 Derived1
???? Int x
private Int y
public Setx() Sety()
class base int x public void
setx(int k) class derived1public base
int y public void sety(int k)
10????
- ?????????????,????????,????????????,?????????????
??
11point_2d???
- class point_2d
- private int x,y
- public
- void setpoint2(int a, int b) x a y b
- int getx() return x
- int gety() return y
12point_3d???
- class point_3dpublic point_2d
- int z
- public
- void setpoint3(int a,int b,int c)
setpoint2(a,b) zc - int getz() return z
-
13point_3d???
- point_3d??????????x,y?z,??????????setpoint2,
setpoint3, getx, gety?getz? - point_3d??????????????x?y,???setpoint3????????poin
t_2d???????setpoint2??? - point_3d????????????,?????????????????????????,???
???????
14Point_3d???
- int main()
- point_2d p1
- point_3d p2
- p1.setpoint2(1, 2)
- cout ltlt "p1 (" ltlt p1.getx() ltlt ", " ltlt
p1.gety() ltlt ")" ltlt endl - p2.setpoint3(1, 2, 3)
- cout ltlt "p2 (" ltlt p2.getx() ltlt ", " ltlt
p2.gety() ltlt ", " - ltlt p2.getz() ltlt ")" ltlt endl
- return 0
P1 (1, 2) P2 (1, 2, 3)
15???
- ??????
- ??????????????
- ????????????????
- ????????
- ???????
- ???????????????
16?????????????
- ??????????????,??????????????,????????????????????
???,?????????? - ????????????????????????,?????????????????????????
??????????????????
17????????
- ???????????????????????????????????????????????
- ??????????????????????,???????????????????????????
????
18???????
- ??????????
- ????????(???)
- ???????(???)
- ???
- ???????????????????????????,????????
- ???????????????????????,???????????
- ???????????????????,???????,??????????,???????????
?,?????????????
19????????
- ????????????????????,?????????????????
- ???????????????????
- ????????,???????????,??????????
20???????
21point2.h
- // Definition of class Point
- ifndef POINT2_H
- define POINT2_H
- class Point public Point( int 0, int
0 ) // default constructor Point() //
destructorprotected // accessible by
derived classes int x, y // x and y
coordinates of Point - endif
22point2.cpp
- include "point2.h"
- // Constructor for class Point
- PointPoint( int a, int b )
- x a y b cout ltlt "Point
constructor" ltlt '' ltlt x ltlt ", "ltlt y
ltlt '' ltlt endl -
- // Destructor for class Point
- PointPoint()
- cout ltlt "Point destructor "
ltlt '' ltlt x ltlt ", "ltlt y ltlt '' ltlt endl -
23circle2.h
- ifndef CIRCLE2_H
- define CIRCLE2_H
- include "point2.h"
- class Circle public Point
- public // default constructor Circle(
double r 0.0, int x 0, int y 0 )
Circle() - private double radius
-
- endif
24circle2.cpp
- include "circle2.h"
- // Constructor for Circle calls constructor for
Point - CircleCircle( double r, int a, int b )
Point( a, b ) // call base-class Constructor - radius r // should validatecout ltlt
"Circle constructor radius is" ltlt radius
ltlt "" ltlt x ltlt ", "ltlt y ltlt '' ltlt endl -
- // Destructor roi class Circle
- CircleCircle()
- cout ltlt "Circle destructor radius is "
ltlt radius ltlt " " ltlt x ltlt ", "ltlt y ltlt
'' ltlt endl -
25Circle????
- include "point2.h
- include "circle2.h"
- int main()
- // Show constructor and destructor calls
for Point Point p( 11, 22 ) cout
ltlt endlCircle circle1( 4.5, 72, 29 )cout ltlt
endlCircle circle2( 10, 5, 5 )cout ltlt
endlreturn 0 -
Point constructor 11, 22 Point destructor
11, 22 Point constructor 72, 29 Circle
constructor radius is 4.5 72, 29Point
constructor 5, 5 Circle constructor radius
is 10 5, 5 Circle destructor radius is 10
5, 5 Point destructor 5, 5 Circle
destructor radius is 4.5 72, 29 Point
destructor 72, 29
26????????????
- ?????????????????,??????????????????????(???)????
???????????,?????????? - ???????????,???????????????,??????????????????????
??????? - ?????????????,??????????????????????????
27?????
- ??????????????,??????????????????????????????????
??????5??,???????10???
28????
- ??????????????????
- ????????????????????????
- ?????????????????????
- ??????????????,?????????????????????,?????????????
???????
29?????
- class reader
- int no
- char name10
- char dept20
- public
- reader(int n, char nm, char d)
- no n
- strcpy(name, nm)
- strcpy(dept, d)
-
30????????
- class readerTeacher public reader
- enum MAX 10
- int borrowed
- int recordMAX
- public
- readerTeacher(int n, char nm, char
d)reader(n, nm, d) - borrowed 0
- bool bookBorrow(int bookNo)
- bool bookReturn(int bookNo)
- void show() //???????
-
31????????
- class readerStudent public reader
- enum MAX 5
- int borrowed
- int recordMAX
- public
- readerStudent(int n, char nm, char
d)reader(n, nm, d) - borrowed 0
- bool bookBorrow(int bookNo)
- bool bookReturn(int bookNo)
- void show() //???????
-
32???
- ??????
- ??????????????
- ????????????????
- ????????
- ???????
- ???????????????
33????????
- ?????????,?????????????,??????????
- ?????????????????,????????????????????????
- ?????????,??????,????????????????????????,????????
??????????????????????
34??
- ???????,?????????????????????????????????,???????
??????
35?????
- ????????
- ?????????????????,????????????????????,?????????
36?????
- class circle
- protected
- double radius
- public
- circle(double r 0) radius r
- double getr() return radius
- double area() return 3.14 radius radius
- double circum() return 2 3.14 radius
-
37?????
- class ballpublic circle
- public
- ball(double r 0)circle(r)
- double area()
- return 4 3.14 radius radius
- double volumn()
- return 4 3.14 radius radius
radius / 3
38Ball??????
- Ball??????????,??????????????????????,??ball??????
?? - Ball??????????circle??????????
39Ball??area??
- Ball?????????????area???????????,????circle?????
- ??ball??????area???,????ball??????area??
40????????????
- ????????????????,???????????????????????,?????????
??????????? - ???????????????????,??????????????????????????????
????????,??????????
41??
- ?circle?????????cylinder????????????????
- ????
- ????????????????????
- ???????????????????????
- ?????????????????????????circle???????
42Cylinder????
- class cylinderpublic circle
- double height
- public
- cylinder(double r 0, double h 0)circle(r)
- height h
- double geth() return height
- double area()
- return 2 circlearea()
circum() height - double volumn() return circlearea() height
43Ball?cylinder????
- int main()
- circle c(3)
- ball b(2)
- cylinder cy(1,2)
- cout ltlt "circle r" ltlt c.getr() ltlt endl
- cout ltlt "area" ltlt c.area() ltlt "\tcircum" ltlt
c.circum() ltlt endl - cout ltlt "ball r" ltlt b.getr() ltlt endl
- cout ltlt "area" ltlt b.area() ltlt "\tvolumn" ltlt
b.volumn() ltlt endl - cout ltlt "cylinder r" ltlt cy.getr() ltlt "\th "
ltlt cy.geth() ltlt endl - cout ltlt "area" ltlt cy.area() ltlt "\tvolumn" ltlt
cy.volumn() ltlt endl -
- return 0
44????
- circle r3
- area28.26 circum18.84
- ball r2
- area50.24 volumn33.4933
- cylinder r1
- area18.84 volumn6.28
45???
- ??????
- ??????????????
- ????????????????
- ????????
- ???????
- ???????????????
46???????
- ????????????,?
- class base
- class d1public base
- class d2public d1
- ???????????????????
- ??????????????,??????????????????,?????
- ???d2?????,????d1?????,?d1?????????????base???????
?,??d2?????,???????base?????,????d1?????,?????d2??
???? - ??????????
47??
- include ltiostreamgt
- using namespace std
- class base int x
- public
- base(int xx) xxx coutltlt"constructing
base\n" - base() coutltlt"destructint base\n"
-
- class derive1public base int y
- public
- derive1(int xx, int yy) base(xx)
- y yy coutltlt"constructing
derive1\n" - derive1() coutltlt"destructing derive1\n"
48- class derive2public derive1 int z
- public
- derive2(int xx, int yy, int zz)derive1(xx, yy)
- z zzcoutltlt"constructing
derive2\n" - derive2() coutltlt"destructing derive2\n"
-
- main()
- derive2 op(1, 2, 3)
- return 0
constructing base constructing derive1 constructin
g derive2 destructing derive2 destructing
derive1 destructint base
49???
- ??????
- ??????????????
- ????????????????
- ????????
- ???????
- ???????????????
50???????????????
- ????????????
- ???????????
- ?????????????
51????????????
- ????????????????,?????????????????,???????????????
??
class base public int a
class d1public base public int b
d1 d d.a 1 d.b 2 base bb d cout ltlt
bb.a //??1 bb.a 3 cout ltlt d.a //??1
52???????????
- ??????????????????,??????????????,??????????,?????
???????????,?????????????? - ???????????,??????????
d1 d d.a 1 d.b 2 base bp d cout ltlt
bp-gta //??1 Bp-gta 3 cout ltlt d.a //??3
53?????????????
- ???????????????
- ???????,??????????
d1 d d.a 1 d.b 2 base bb d cout ltlt
bb.a //??1 bb.a 3 cout ltlt d.a //??3
54??
- class Shape
- public void printShapeName() coutltltShapeltltend
l -
- class Point public Shape
- public void printShapeName() coutltltPointltltend
l -
- class Circle public Point
- public void printShapeName() coutltltCircleltlten
dl -
- class Cylinder public Circle
- public void printShapeName()
coutltltCylinderltltendl
55????????????
- int i
- Point aPoint
- Circle aCircle
- Cylinder aCylinder
- Shape shapes3 aPoint, aCircle, aCylinder
- for (i0ilt3i) shapesi.printShapeName()
Shape Shape Shape
56???????????
Shape Shape Shape
- int i
- Point aPoint
- Circle aCircle
- Cylinder aCylinder
- Shape pShape3 aPoint, aCircle,
- aCylinder
- for (i0ilt3i) pShapei-gtprintShapeName()
57?????????????
- int i
- Point aPoint
- Circle aCircle
- Cylinder aCylinder
- Shape shape1 aPoint
- shape1.printShapeName()
shape
58??
- ??????????????,????????????????????
- ?????????????????????
- ??????????????????????????????????????????????????
??,????reinterpret_cast?????????????????????
59?12? ?????
- ??
- ??
- ???????
- ????????
- ???
- ????????
60???????
61???
- ???????????????????????
- ???????????????
62??????
- ???????????????????????
- ???????????????????????
63????
- ????????????????????
- ?????
- ??????????????????????????????????????????,?????
???????
64??????
- ???????????????????????????,??????????????????????
??????????????,???????????????????????????????C?
,?????(Virtual Functions)????
65???????
66???
- ???????????,???????????????????????
- ??????????????virtual??,?????????????????????????
????,?????,?????????????????????????????????????? - ?????????????,???????,????????????????????????????
???????????????
67??????
- ????????????????,??????????????????????
- ?????????????????????????,????????????????,???????
??????????????????,????????????????,??????????
68???
- class Shape
- publicvirtual void printShapeName()
coutltltShapeltltendl -
- class Pointpublic Shape
- publicvirtual void printShapeName()
coutltltPointltltendl -
- class Circlepublic Point
- public virtual void printShapeName()
coutltltCircleltltendl -
- class Cylinderpublic Circle
- public virtual void printShapeName()
coutltltCylinderltltendl
69????????????
- int i
- Point aPoint
- Circle aCircle
- Cylinder aCylinder
- Shape shapes3 aPoint, aCircle, aCylinder
- for (i0ilt3i) shapesi.printShapeName()
Shape Shape Shape
70???????????
- int i
- Point aPoint
- Circle aCircle
- Cylinder aCylinder
- Shape pShape3 aPoint, aCircle,
- aCylinder
- for (i0ilt3i) pShapei-gtprintShapeName()
Point Circle Cylinder
71?????????????
- int i
- Point aPoint
- Circle aCircle
- Cylinder aCylinder
- //Shape pShape3 aPoint, aCircle,
aCylinder - Shape shape1 aPoint
- //for (i0ilt3i) pShapei-gtprintShapeName()
- shape1.printShapeName()
Point
72??????????
- ?????????????,??????????????????????????????????,?
?????????? - ???????????????,???virtual???????????virtual?????,
???????????????????????virtual?
73?
- ???????????,??,???rectangle?????square????????,???
?????????
74- class rectangle
- int w, h
- public
- rectangle(int ww, int hh) w(ww), h(hh)
- virtual void display()
- cout ltlt this is a rectangle\n
-
- class squarepublic rectangle
- public
- square(int ss) rectangle(ss, ss)
- void display() //???
- cout ltlt this is a square\n
75???????
76??????????
- ??????????,???????????,????????
- ??????????????????,?????????,?????????????????????
????????????????????,?delete??????????????????
77????
- ???????????????
- ???????????????,???????????????????????,??????????
??,???????????
78?????????
- ?????????,????????????????
- ??????????????????????,?????????????????
79?12? ?????
- ??
- ??
- ???????
- ????????
- ???
- ????????
80????
- ?????????????????,??????????,????????????????,???
??????? - ?????????
- virtual ?? ???(???)0
81??????
- class shape
- protected
- double x, y
- public
- shape(double xx, double yy) xxx yyy
- virtual double area() 0
- virtual void display()
- cout ltlt "This is a shape. The position is ("
- ltlt x ltlt ", " ltlt y ltlt ")\n"
-
82???
- ??????????????????,?????????
- ???????
- ? ?????????????,???????????
- ? ???????????????,???????????,???????
- ? ?????????????????????????
- ? ????????????????????,???????????,???????
83??????
- ?????????????????????????,????????????????????????
???,???????????????????(???????????????????????)??
???????
84?????
- ????????????????
- include ltiostream.hgt
- class shape public virtual void area()0
- class rectanglepublic shapefloat w,h
- publicrectangle(float ww, float hh)www
hhh - void area() coutltlt"\narea is"ltltwh
- class circlepublic shapefloat r
- publiccircle(float rr) rrr
- void area()coutltlt"\narea is"ltlt3.14rr
- void main()
- shape ptr rectangle ob1(5,10) circle
ob2(10) - ptrob1 ptr-gtarea()
- ptrob2 ptr-gtarea()
85?12? ?????
- ??
- ??
- ???????
- ????????
- ???
- ????????
86????
- ?????????????????
- ????,????????????
87?????????
- class ???? ???
- ???????????????
-
- ????????1 ???1, ????2 ???2,,????n
???n
88????????
- ??????
- ????????????,????????????
89?????????
- ????????,?
- include ltiostream.hgt
- class Xint a
- public void setX(int x) ax
- void showX() coutltlta
- class Yint b
- public void setY(int x) bx
- void showY() coutltltb
- class Z public X, private Y int c
- public void setZ(int x, int y) cx
setY(y) - void showZ() coutltltc
- main()
- Z obj
- obj.setX(3) obj.showX()
- obj.setY(4) obj.showY() //??
- obj.setZ(5,6) obj.showZ()
?z??????????????
90??????????????
- ?????????
- ????????(???)??1?????(???),??2?????(???),?????
n?????(???) - ?????????(???????,????????????????),???????
- ??????????
91??????
- includeltiostream.hgt
- class A int i
- public
- A(int ii0) iii coutltlt"A... i"ltltiltltendl
- void show() coutltlt"Ashow()
i"ltltiltltendl - class B int i
- public
- B(int ii0) iii coutltlt"B... i"ltltiltltendl
- void show() coutltlt"Bshow()
i"ltltiltltendl - class C public A, public B
- int i
- public
- C(int i10, int i20, int i30) A(i1),
B(i2) - ii3 coutltlt"C... i"ltltiltltendl
- void show() coutltlt"Cshow()
i"ltltiltltendl - void main()
- C c(1,2,3)
- c.show()
92????
- A... i1
- B... i2
- C... i3
- Cshow() i3
93?????????
- ???
- ????????????
- ??????????????
94???
- ??????????????,????????????????
- class A public void f()
- class B public void f()
- void g()
- class C public A, public B
- public void g()
- void h()
- ??C x
- ? x.f () ?????
95x.f()?????
- ???C???????f ????A?f ??B?f??x.Af() ?
x.Bf() - ?????C??????C????????????????????
- ????C????????????C????void ha() Af()
- void hb()
Bf()
96???---cont.
- ??????????????,????????????,????????????????,?????
????????????????
97???---cont.
- ?
- 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
- ?? C c
???b????????? c.b c.Bb ???b??????? c.B1b b.B
2b
98?????
- class Apublic void fun()coutltlt"Afun()"ltltendl
- class B1public A
- public void fun1()coutltlt"B1fun1()"ltlte
ndl - class B2public A
- public void fun1()coutltlt"B2fun1()"ltlt
endl - class Dpublic B1,public B2
- void main()
- D obj
- //obj.fun1() //???????fun1()??)
- obj.B1fun1() obj.B2fun1()
- //obj.fun() //?????
- //obj.Afun() //????????
- obj.B1fun() //????????
99???
- ????????????????,????????????????????????????????
???????????????B, B1, B2?C,????????
- ??B????????,???C??B?????????????
100??????
- ??????????????
- ??????????virtual??
- Class B public int b
- Class B1 virtual public B private int b1
- Class B2 virtual public B private int b2
- Class C public B1, public B2
- public int f()
- private int d
- ??B1, B2?????B???,
- ?B??????????????
101???????
- ???????????????
- ???????????????????
- ??,???C????,?C?????????B?????,????B1?B2????
- ???????B?????,???B1?B2?????,????C??????
- ????????????
102?????????
- include ltiostream.hgt
- class B int a
- public B(int sa) asa coutltlt"constructing
B\n" - class B1virtual public B int b
- public B1(int sa, int sb)B(sa)
- bsb coutltlt"constructing B1\n"
- class B2virtual public B int c
- public B2(int sa, int sb)B(sa)
- csb coutltlt"constructing B2\n"
- class C public B1, public B2 int d
- public C(int sa, int sb, int sc, int sd)
- B(sa), B1(sa, sb), B2(sa,sc)
- dsd coutltlt"constructing
C\n" - main() C obj(2,4,6,8) return 0
103?????????
- ????
- constructing B
- constructing B1
- constructing B2
- constructing C
104?12? ?????
- ??
- ??
- ???????
- ????????
- ???
- ????????
105??????
- ?????????????
- ????????????????
- ?????????,????????move
- ?????????,????????move
106??????
- class Tortoise
- public
- int move()
- int probability rand() 10 / (RAND_MAX
1) - if (probability lt 5) return 3
//?? - else if (probability lt 7) return -6
//?? - else return 1
//?? -
107??????
- class Hare
- public
- int move()
- int probability rand() 10 / (RAND_MAX
1) - if (probability lt 2) return 0
//?? - else if (probability lt 4) return -9
//??? - else if (probability lt 5) return
14 //?? - else if (probability lt 8)
return 3//??? - else return -2
//??? -
-
108???????
- ??????
- ??????????????
- ???????
- ????????????????
- ???
- ??????,???????????
- 5?????
- ??????????update
- ????????????print
- ????????isFinish?????
- ?????????
- ???????
109???????
- class Judger
- int hare, tortoise
- enum RACE_END 100 // ????
- public
- Judger()
- hare tortoise 0
- srand(time(NULL))
-
- void restart() hare tortoise 0 //
?????? - void print_title() const //
????????? - cout ltlt " hare\ttortoise\n"
- cout ltlt " \n"
-
110- void update(int h, int t) //
???????????? - hare h
- tortoise t
-
- void print() const //
???????????? - cout ltlt hare ltlt '\t' ltlt tortoise ltlt endl
-
- bool isFinish(int winner) const //0???,1???,
2?? - if (hare gt RACE_END tortoise gt
RACE_END) - if (hare gt tortoise) winner 0
- else if (hare lt tortoise) winner 1
- else winner 2
- return true
-
- else return false
-
-
111????????
- int main()
- Tortoise tort
- Hare hare
- Judger judger
- int winner
- judger.print_title()
- while (!judger.isFinish(winner))
- judger.update(hare.move(), tort.move())
- judger.print()
-
- switch (winner) //
?????? - case 0cout ltlt "????!" break
- case 1cout ltlt "????!" break
- defaultcout ltlt "??"
-
- return 0
-
112??n????????
- int main()
- Tortoise tort Hare hare Judger
judger - int winner, num, win_cnt_hare 0,
win_cnt_tort 0, tie_cnt 0 - cout ltlt "??????????"
- cin gtgt num
- for (int i 0 i lt num i)
// ??num??? - judger.restart()
- while (!judger.isFinish(winner))
// ?????? - judger.update(hare.move(), tort.move())
- switch (winner) //
?????? - case 0 win_cnt_hare break
- case 1 win_cnt_tort break
- default tie_cnt
-
-
- cout ltlt "??? ??? ??\n"
- cout ltlt win_cnt_tort ltlt '\t' ltlt win_cnt_hare
ltlt '\t' ltlt tie_cnt ltlt endl - return 0
-
113??
114??
- ??????????????????????
- ???????????????
- ????????????????????????,??????????????????
- ???????(??)????????,??????,?????????????,?????????
,?????????????