Académique Documents
Professionnel Documents
Culture Documents
int get-i*+( .( .
i / n(
samp:: samp*int n+ )
return i( .
s5r-it*a+( cout && 0;ain: "alue o! a:3 && a.get-i*+ && 72n8(
"oi
s5r-it*samp o+ ) 6 .
return :(
o.set-i*o.get-i*+ o.get-i*++(
cout && 0Copy: "alue o! a:3 && o.get-i*+ && 72n8( . int main*+ ) samp a*9:+( Problem:
>! the object use as the arguments allocates ynamic memory an !ree the memory then the estructor !unction is calle an the original object is amage .
class
yna )
.(
6+
O<=P<=: -10 Freeing // when o is passed to neg !" // copy is created and //destroyed in neg ! 10 #$%% pointer assign&ent o.get ! #$%% pointer assign&ent //
"oi
return Bob.get*+( . int main*+ ) yna o*B9:+( cout && o.get*+ && 72n8(
Colutions: =o pass the a itsel!. ress o! the object, not the object o! constructor *Chap D+. calle 0copy
s5r-it*Ga+( cout && 0main: "alue o! i: 3 && a.get-i*+ && 72n8( return :(
Foi
o.set-i* o.get-i*+ +(
public: samp*+ ) s / 72:8( . ,samp*+ ) i! *s+ !ree*s+( count && 0Freeing C2n3(. "oi sho$*+ )cout && s && 82n8(. "oi set *char 6str+(
.( 3(
"oi
samp::set*char 6str+ ) 6+
0Allocation
3.M Frien
Function
A friend function is not a member of a class but still has access to its private elements. Three uses of friend functions (1) to do operator overloadin ! (") creation of certain types of #$% functions! and (&) one function to have access to the private members of two or more different classes.
!rien
int sp-greater *car c, truck t+ ) class car ) int passengers( int spee ( public: car*int p, int s+ ) passengers / p( spee / s( . !rien .( int sp-greater*car c, truck t+( int main*+ ) int t( car c*O, DD+( truck t*2:::, P2+( . return c.spee N t.spee (
class truck ) int $eight( int spee ( public: car*int $, int s+ ) $eight / $( spee / s( .
return :( .
public: car*int $, int s+ ) $eight / $( spee / s( . !rien int car::sp-greater*tr uck t+(
class truck(
class car ) int passengers( int spee ( public: car*int p, int s+ ) passengers / p( spee / s( . int sp-greater*truck t+( .(
.(
int t+ )
return spee .
int main*+ ) class truck ) int $eight( int spee ( int t( car c*O, DD+( truck t*2:::, P2+(