operator overload

Download Operator Overload

Post on 04-Aug-2015

92 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

Operator OverloadingLp trnh hng i tng

Ti liu cn

Eckel, Bruce. Thinking in C++, 2nd Ed. Vol. 1. Chapter

8: Operator Overloading 8: Operator Overloading

n

Dietel. C++ How to Program, 4th Ed. Chapter

@ 2004 Trn Minh Chu. FOTECH. VNU

2

Operator Overloadingn n n n n n n

Gii thiu Cc ton t ca C++ L thuyt v operator overloading C php operator overloading nh ngha cc ton t thnh vin Php gn nh ngha cc ton t ton cc

n n n n n n n n

Lm vic vi tnh ng gi friend Ti sao s dng ton t ton cc Php chn ("", "->*" phi l thnh vin

n n n

Cc ton t n nn l thnh vin( m bo tnh ng gi) Khi ton hng tri c th c gn tr, ton t nn l thnh vin ("+=", "-=", "/=",) Mi ton t i khc khng nn l thnh vin

Tr khi ta mun cc ton t ny l hm o trong cy tha k

@ 2004 Trn Minh Chu. FOTECH. VNU

38

Phng thc hay hm ton cc?n

Cc ton t l thnh vin nn l hng hm mi khi c th

iu ny cho php tnh mm do khi lm vic vi hng

n

Nu ta cm thy khng nn cho php s dng mt ton t no vi lp ca ta (v khng mun cc nh thit k khc nh ngha n), ta khai bo ton t dng private (v khng ci t ton t )

@ 2004 Trn Minh Chu. FOTECH. VNU

39

V du: Kiu Daten

Date class Overloadn

php tng +=

thay i ngy, thng, nm

Overloaded hm

kim tra nm nhun hm kim tra xem mt ngy c phi cui thng

@ 2004 Trn Minh Chu. FOTECH. VNU

40

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// Fig. 8.10: date1.h // Date class definition. #ifndef DATE1_H #define DATE1_H #include using std::ostream; class Date { friend ostream &operator

Recommended

View more >