You are on page 1of 4

LUCRAREA DE LABORATOR 5 Elaborai programe, care implementeaz clasa numerelor raionale Clasa trebuie s conin !

ate pri"ate pentru numrtorul #i numitorul $raciei, meto!e publice% constructor implicit, constructor cu parametri, constructor !e copiere, !estructor, a$i#area $raciei &olosii meto!e ale clasei #i $uncii prieten clasei pentru a supra!e$ini operatorii in!icai 'n "arianta respecti" De$inii meto!ele simple 'n interiorul clasei, iar meto!ele mai comple(e 'na$ara clasei &olosii at)t obiecte, c)t #i pointeri #i re$erine la obiecte *nclu!ei 'n program enunul problemei #i mai multe comentarii Comentai un!e #i care constructor lucreaz #i cum lucreaz !estructorul% un!e, c)n! #i care obiecte le !istruge + . / 0 2 5 a a b b a c a d + b c A!unarea pentru $racii% + = b d b d a c a d b c ,c!erea pentru $racii% = b d b d a c a c 1nmulirea pentru $racii% = b d b d a c a d 1mprirea pentru $racii% % = b d b c ,c-imbarea semnului unei $racii%

a a+ b b b a a b 3 Decrementarea pre$i(at #i post$i(at pentru $racii% b b a c . . . 0 , 4 Operatorul 55 pentru $racii% = = Obser"aie% $raciile , ca #i , sunt egale6 b d / / / 3 a c 7 Operatorul 65 pentru $racii% 6= b d a c 8 Operatori !e relaie pentru $racii% < = #a b d a c a c c .+ Operatori logici pentru $racii% 9 9 , :: , 6 b d b d d *ncrementarea pre$i(at #i post$i(at pentru $racii% a a .. Operatorul ; pentru a ri!ica la o putere 'ntreag o $racie% = <; n = , n Z b b ./ Operatorul cast !e con"ersie a tipului, !e e(emplu =$loat< pentru a con"erti o $racie la un numr $loat sau alt tip a a+ . b b
n

Exemplul 1 *ncrementarea pre$i(at !up $ormula%


// // //

lobo5.cpp : Defines the entry point for the console application. Lucrarea de laborator 5 Exemplul 1. Overloading for operator in prefix form

!include "stdafx.h" !include #iostream$ using namespace std%

class rational & private: int a% int b% public: // default constructor rational'( & ) // constructor creating a fraction by parameters rational'int a* int b + 1( & rational::a + a% rational::b + b% ) // copy,constructor rational'const rational- .( & a + ..a% b + ..b% ) /rational'( & ) //rational- operator '(% friend rational- operator void afis'(% 'rational- 0(%

)%

//overloading 1ith a metod,function //rational- rational::operator '( //& // a% //return 2this% //) //overloading 1ith a friend function rational- operator 'rational- 0( & 0.a% return 0% ) void rational::afis'( & cout ## a ## 3/3 ## b% ) int main' ( & rational c'1*4(* r% cout ## " '"% c.afis'(% c% cout ## "(+"% c.afis'(% cout ## endl ## endl% cout ## " '"% c.afis'(%r+ c% cout ## "(+"% c.afis'(% cout ## endl ## endl% rational 5 + c* D + rational'6*7(% cout ## " '"% 5.afis'(% 5% cout ## "(+"% 5.afis'(% cout ## endl ## endl% cout ## " '"% D.afis'(%r+ D% cout ## "(+"% D.afis'(% cout ## endl ## endl%

rational 8'5(* 9':(% cout ## " '"% 8.afis'(% 8% cout ## "(+"% 8.afis'(% cout ## endl ## endl% cout ## " '"% 9.afis'(%r+ 9% cout ## "(+"% D.afis'(% cout ## endl ## endl% rational ;'4*6(* <'D(% cout ## " '"% ;.afis'(% ;% cout ## "(+"% ;.afis'(% cout ## endl ## endl% cout ## " '"% <.afis'(%r+ <% cout ## "(+"% <.afis'(% cout ## endl ## endl% rational 2p* x% p+ne1 rational% x+rational'4*6(% 2p+rational'<(% cout ## " '"% p,$afis'(% 2p% cout ## "(+"% p,$afis'(% cout ## endl ## endl% cout ## " '"% x.afis'(%r+ x% cout ## "(+"% x.afis'(% cout ## endl ## endl% ) return =%

Exemplul 2 Operaia !e relaie > > pentru $racii%


// // //

a c >= b d

lobo5.cpp : Defines the entry point for the console application. Lucrarea de laborator 5 Exemplul 4. Overloading for operator $+

!include "stdafx.h" !include #iostream$ using namespace std% class rational & private: int a% int b% public: // default constructor rational'( & ) // constructor creating a fraction by parameters rational'int a* int b + 1( & rational::a + a% rational::b + b% if' b ++ = ( & cout ## "8raction 1ith denominator =. >e substitute it 1ith fraction = ?" ## endl% rational::a+=% rational::b+1% ) ) // copy,constructor rational'const rational- .( &

a + ..a% b + ..b% ) /rational'( & ) //int operator $+'const rational- 1( const% friend int operator $+' const rational- 0* const rational- 1(% void afis'(% //overloading 1ith a metod,function //int rational::operator $+'const rational- 1( const //int p + =% //if' 0 2 1.b 2 ' a 2 1.b , b 2 1.a ( $+ = ( p + 1% //return p% //) //overloading 1ith a friend function int operator $+' const rational- 0* const rational- 1( & int p + =% if' 0.b 2 1.b 2 ' 0.a 2 1.b , 0.b 2 1.a ( $+ = ( p + 1% return p% ) void rational::afis'( & cout ## a ## 3/3 ## b% ) int main' ( & rational c'1*:(* r'1*6(% int .% .+'c$+r(% cout ## .% cout ## endl ## endl% rational 5 + c* D + rational'1*5(% .+'5$+D(% cout ## .% cout ## endl ## endl% rational 8'5(* 9':(% .+'8$+9(% cout ## .% cout ## endl ## endl% rational ;'1*5(* <'D(% cout ## ';$+<(% cout ## endl ## endl% rational 2p* x% p+ne1 rational% x+rational'4*6(% 2p+rational'<(% .+'2p$+x(% cout ## .% cout ## endl ## endl% return =% )

)%