Chapter 11 Operator Overloading; String and Array Objects - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Chapter 11 Operator Overloading; String and Array Objects

Description:

Chapter 11 Operator Overloading; String and Array Objects Part III ( ) – PowerPoint PPT presentation

Number of Views:158
Avg rating:3.0/5.0
Slides: 53
Provided by: tcu3
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11 Operator Overloading; String and Array Objects


1
Chapter 11 Operator Overloading String and
Array Objects
  • Part III (????)

2
????
  • C??????????????????????,??????????
  • ?????
  • ?????????????
  • ????????????????(operator)???
  • ??????????????????

3
?????
4
??
  • C?C??????????????????
  • ????,???????????????????????,?????????????????
  • ??
  • int a
  • int aPtr a
  • cout ltlt aPtr ltlt endl

aPtr
a
5
????(1)
  • ????(???????)
  • ??

void swap(int a, int b) int temp
a a b b temp void
main() int x3, y2 swap(x, y)

?x, y?????????????swap(),?swap()?,?????????????mai
n??????
6
????(2)
  • ???????
  • ??
  • ?????????????????,???????????????,???????????????,
    ??????????(memory leak)?

void main() int length cin gtgt
length int A new intlength
delete A
7
??
  • C????(reference)???,?????????????????
  • ?????????????,??????????
  • ?????????,??????????????,????????????????

8
??
  • ??
  • int x 2
  • int y x //??y?????x???
  • y 3
  • cout ltlt x ltlt endl
  • y???????x?????,??????????????,???????y???x??????y
    ????,??x???????

9
????(1)
  • ????
  • ??

void swap(int a, int b) int temp
a a b b temp void main()
int x3, y2 swap(x, y)
x , y????????,a?b???????x?y?
?? ???????????,??????,?????????????????,?????????
??????
10
????(2)
  • ????????
  • C??????????,??????????????????????????????????,?
    ??(Pass-by-value)??????????????
  • ??????????????????
  • ??,???????C??????,????????memory leak?

11
?????????????
12
?????????
  • ???????????

int foo(int x, int c) int temp
return temp void main()
int g foo(a, b)
13
?????????
  • ?????????????????

push b push a call foo() add sp, 8 mov g,
register a
Locals of foo
Return address
a
b
main
int?char?double????????????????????,?????????????m
ov????????????????g??
14
????????
  • ????????????

class BigObj char buf100 int i
BigObj func(BigObj b)
return b int main() BigObj B
BigObj B2 fun(B)
????????????????,???????B,???func?,???????????pus
h?stack??
???????,???????????????push?stack?
15
?????????
BigObj func(BigObj b) return b
int main() BigObj B BigObj B2
func(B)
?????????????,?????????????????(????????????)
????????????,????????????????,?????????????????
??,????????????main??????
copy
b
??,????????????(copy constructor)????
16
BigObj B BigObj B2 func(B)
T??????
??B2??????
b
B2
????????,???????????
BigObj B BigObj B3 B3 func(B)
T??????
??B3?operator
b
B3
B3????????,?????????,?????????
17
?????????
  • ???main?????????????,?????????????
  • ?????????(???????),???????????(?????)?
  • ????????????????,???????????????,???main??????????
    ??????????,????????main??????????,????????????????
    ,????????????

BigObj B func(B)
T??????
b
18
????????????????(operator)???
19
?????????
  • ?????????????????????(),?????????????????????
  • ??

??B
class BigObj char buf100 int i
void main() BigObj A, B A
B
char buf100
int i
??A
20
????(1)
  • ???????????????????,??????

class BigObj2 public BigObj2(int
s100) capacity (s gt 0)? s
100 buf new charcapacity
BigObj2() delete
buf private int capacity
char buf
?????????????,?????????????capacity????????????
21
????(2)
  • ?????????????
  • func()??????????,????????
  • main????????A?B,??A??func?,?B??????????
  • ?????A???,?????????

BigObj2 func(BigObj2 X) return X void
main() BigObj2 A, B B func(A)
22
????(3)
main()
main()
func()
?????T
Copy
Copy
Copy
A
X
B
Copy Constructor
Copy Constructor
operator
  • ??
  • ?????????????????
  • A???????????????????X????????

23
????(4)
  • ???????,???????????????????,?????????????

Shallow Copy
???????,???????????
30000
24
????(5)
main()
main()
func()
?????T
Copy
Copy
Copy
A
X
B
operator
?????????????????
?
Dangling Pointer
30000
Memory Leak
25
???????????????
  • ????
  • ??????????????,??????????
  • ?????
  • ??????,????????????
  • ????????
  • ???????(operator)

26
???????
  • ????
  • ???????A?????????,??A???
  • ????
  • ?????
  • ????????,?
  • const XXX
  • XXX?????????
  • ?????const????????????????????????

27
???????
????capacity
??capacity,??new??????
??????????
???????????????????????,??????????????,??????????
?????????????????????
28
???????
Deep Copy
????????,??????????????
29
???????
  • ????
  • ???????B???????????A???
  • ???????
  • BigObj A B
  • BigObj A(B)
  • ???????(operator)
  • BigObj A, B
  • A B
  • A func(B)
  • ???????????
  • ??????????????????????,???????????????,???????????
    ???????,???????????????????

30
???????
31
???????
?? A B
Deep Copy
???????,??????A.operator()
?????????,????,???A?????,???????????
?????????,?????????
32
???????
  • ???????BigObj??
  • ??const BigObj source
  • ???????????????,???const,????????????
  • ??????????????????????,??const???????????????????
  • ???????????(this)??,?????????????????????????????
    ??????,??
  • A B C
  • ???????
  • A.operator(B.operator(C))

33
??????????????????
34
??????????????
  • ???????????,?????????,?????????????,?????????????
  • ???????????????
  • ????
  • ???????????,?????????,??????????????????

35
????????
  • ????????

class Set public Set(int s100)
Set() Set(const Set source) Set
operator (const Set source) Set
Union(const Set S) private int
capacity int buf
?????????,?????????????????????????
??????Union,????????,?????????????????const????
36
????????
Set SetUnion(const Set S) Set
Result(capacity S-gtcapacity) //or, Set
Result(capacity(S).capacity) .
return Result void main() Set A, B
Set R A.Union(B)
  • B??????????????,???????????(???????)??????Union(?
    ?Union?????????????)????????????????,??const??????
    ????(????????????)
  • ????????????,??????
  • ???????????,??????????

37
????????
class Set Set Union(const Set S)
Set SetUnion(const Set S)
Set Result(capacity S.capacity)
//??????X???B . return Result
void main() Set A, B Set R
A.Union(B) //????
  • ?????????????
  • ?????????????????????,???????????????????
  • ???????????????????????????????????,?????????????
    ?????,???,???????????const????,???????????(???????
    ????)?

38
?????????
  • ????????????,??????????????????,?????????????????
    ?????,??????
  • ???????

Set SetUnion(Set S) //???????,???????????
//????????????? Set Result(capacity
S.capacity) . return Result
void main() Set A, B Set R
A.Union(B)
39
????????????
  • ???Result??????????,?????????????????

Set SetUnion(Set S) Set
Result(capacity S.capacity) .
return Result void main() Set A,
B Set R A.Union(B)
A.Union?????????Result???????????????????Result??
?,?????Result??????main()?R?
main()
A.Union()
main()
B????????S?????????B
?????
B
S
R
x
copy
????!??Result???????,???????????????,????R?,??????
?,??R???????????
40
??????????????
  • ?????????????????,???????

Set SetUnion(Set S) Set Result
new Result(capacity S.capacity) .
return Result void main() Set A,
B Set R A.Union(B) delete R
?????????????????????????,????????????????????????
???main()???????delete R???,?????????memory
leak?,???????????????
41
??????????????
  • ??,????????,????????

Set SetUnion(Set X) Set Result
new Result(capacity X.capacity) .
return Result void main() Set
A, B Set R A.Union(B)
A.Union()
main()
A.Union??????(Heap,??Free Store)?????????????????
?????,?????Result????????main()?R?
?????
R
x
copy
????????????Union?????,????(Heap)
????????????????????,?????????R?,?????????????(mem
ory leak)?
42
??????????????,?????
  • ??,????????,????????

Set SetUnion(Set X) Set Result
new Result(capacity X.capacity) .
return Result void main() Set
A, B Set R A.Union(B) delete
R //??R??????,?????????
A.Union()
main()
?????
R
x
R????????????????,??main??????,???????????????????
?R??????????(????),?????????,main???????????,?????
memory leak?
????????????Union?????,????(Heap)
43
?????????
  • ?????????????,???????????????????,????????????????
    ??????,???????????

class Set Set Union(const Set B)
Set SetUnion(const Set S)
Set Result(capacity S.capacity)
//??????X???B . return Result
void main() Set A, B Set R
A.Union(B) //????
44
???????????
  • ?????????,?????????????,???????????????(???),?????
    ??????,?????????(???????)

A.Union()
main()
?????
R
copy
copy
45
?????????
  • ???????????????????????????????????????,??????,??
    ?????????????????

class Set void Union(Set R, const Set
S) void SetUnion(Set R, const
Set S) //??????R,????????,???????????????
//????????R,????const? void main()
Set A, B Set R A.Union(R, B)
??????????????????????????,?????????,?????????????
????????,??????????,???????,???????????????
46
???????????????
  • ??????????,?????????????????
  • ???

class Set Set Union(const Set S)
Set SetUnion(const Set S)
. return this //??????,????????,t
his??????? void main() Set A, B
Set R A.Union(B) //?????RA
?A???R,??Union?????????
47
???????????????
  • ???

class Set const Set Union(const Set
S) const Set SetUnion(const
Set S) . return S
//??????,????????main????B,???????
//??Union????????
//?????,??const??(???????),?????const
void main() Set A, B Set R
A.Union(B) //?????RB ?B???R,??Union???B?????

48
??
49
??
  • ??????????????????,???????,?????????,?????????????
    ?????,??????????,???memory leak???
  • ?Java?C?
  • ??????????????????????????????????????
  • C??????????????????,???????????????????????????,
    ????????????

50
??????????
  • ??????????,??????????????????????????,???????????
  • ?????
  • ????????
  • ???????(operator)

51
??????
  • ????????,??????????????????
  • ???????,?????????????????????????,?????????????co
    nst??

52
???????
  • ????????????????????
  • ?????????????,????????????????
  • ??,??????,?????????????,???????????
  • ???????????,?????????
  • ??????(?????????????)
  • ???????????????????
Write a Comment
User Comments (0)
About PowerShow.com