Vous êtes sur la page 1sur 9

Chapter 3: A Closer Look at Classes

3.2 Passing Objects to Functions


Parameter passing, by #e$ object is calle e!ault, is calle by "alue. estructor oes not call constructor, but i / n( %inclu e &iostream' using namespace st ( count 0Constructing12n3( . class samp ) int i( public: samp*int n+( ,samp*+( "oi set-i*int n+( "oi samp:: set-i*int n+ ) samp:: ,samp*+ ) count 04estructing12n3( . && &&

int get-i*+( .( .

i / n(

samp:: samp*int n+ )

int samp:: get-i*+ )

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:

O<=P<=: Constructing. Copy: value of a: 100 Destructing. Main: value of a: 10 Destructing.

>! 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 .

%inclu e &iostream' %inclu e &cst lib' using namespace st (

, yna*+ )!ree*p+( cout && 0Freeing12n3( . int get *+ ) return 6p.

class

yna )

.(

int 6p( public: yna*int i+( yna:: yna*int i+)

p / *int malloc* si?eo!*int+ +( i! *@p+ ) cout && problem2n3( eAit*9+( . 6p / i( .

6+

cout && neg*o+ && 72n8(

cout && o.get*+ && 72n8( 0Allocation . return :(

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

neg * yna ob+ )

return Bob.get*+( . int main*+ ) yna o*B9:+( cout && o.get*+ && 72n8(

// free p! in destructor Freeing..//when destroyed o is

Colutions: =o pass the a itsel!. ress o! the object, not the object o! constructor *Chap D+. calle 0copy

A special type constructor3 is use

EAample: =o pass the a itsel!.

ress o! the object, not the object

%inclu e &iostream' using namespace st (

. int main*+ ) samp a*9:+(

class samp ) int i( public: samp*int n+ ) i / n( . "oi set-i *int n+ ) i / n( . .

s5r-it*Ga+( cout && 0main: "alue o! i: 3 && a.get-i*+ && 72n8( return :(

int get-i*+ ) return >( . .(

O<=P<=: copy: value of i: 100

Foi

s5r-it*samp 6o+ ) o.get-i*+ 6

&ain: value of i: 100

o.set-i* o.get-i*+ +(

cout && 0copy: "alue o! i: 3 && oB 'get-i*+ && 72n8(

3.3 Heturning objects !rom Functions


When an object is returned by a function, a temporary object is created which holds the return value. This object is return by the function. After the value has been returned, this object is destroyed. The destruction of this temporary object may cause unexpected side effects.

%inclu e &iostream' %inclu e &cstring' %inclu e &cst lib' using namespace st (

public: samp*+ ) s / 72:8( . ,samp*+ ) i! *s+ !ree*s+( count && 0Freeing C2n3(. "oi sho$*+ )cout && s && 82n8(. "oi set *char 6str+(

class samp ) char 6s(

.( 3(

samp str( cout &&3Enter a string:

"oi

samp::set*char 6str+ ) 6+

s / *char malloc*strlen*str+I9+( i!*@s+ ) cout error2n3( &&

cin '' s( str.set*s+( return str(

0Allocation

eAit*9+( . strcpy*s, str+( .

int main*+ ) samp ob( ob / input*+( ob.sho$*+( return :(

samp input*+ ) char sJK:L(

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.

A friend function is a re ular non'member function

%inclu e &iostream' using namespace st ( .( class truck(

!rien

int sp-greater*car c, truck t+(

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 (

t / sp-greater*c, t+( i! *t ' :+ cout 3Faster.2n3( &&

class truck ) int $eight( int spee ( public: car*int $, int s+ ) $eight / $( spee / s( .

else i! *t//:+ cout && 0E5ual.2n3( else count 0slo$er.2n3( &&

return :( .

(orward declaration) class truc*!

A friend function can be a member of one class and a friend of another.

%inclu e &iostream' using namespace st (

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+ )

car::sp-greater *truck N t.spee (

return spee .

int main*+ ) class truck ) int $eight( int spee ( int t( car c*O, DD+( truck t*2:::, P2+(

t / c. sp-greater*t+( i! *t ' :+ cout 3Faster.2n3( && t + c.sp, reater(t)!

else i! *t//:+ cout && 0E5ual.2n3( else count 0slo$er.2n3( &&

can be written by the scope resolution operator ())) as

t +c.car))sp, reater(t)! return :( . but this is unnecessary.

Vous aimerez peut-être aussi