Вы находитесь на странице: 1из 29

2.

:
class < >
{
< >
};


:
<> <>;

Fraction - ,
:

fNumerator - , ;

fDenominator - , .

//
class Fraction
{
int fNumerator;
//
int fDenominator; //
.......
};

( ),
:
< >
< >(<>)
{
< >
}

:
< >
< >(<>);


(
),

:
< >
< >::< >(<>)
{
< >
}

Fraction ():

isEqual - , :

true -

false -

:
Fraction fr1(1, 2);
Fraction fr2(4, 3);

// 1/2
// 4/3

// isEqual() - ,
// ,
// .
// ( ""),
// .. -

bool result = fr1.isEqual(fr2); /* fr1


fr2
true false */

//
class Fraction
{
int fNumerator;
//
int fDenominator; //
//
bool isEqual(const Fraction &other);
........
};
10

11


, ,
( )


fr1:Fraction
(-)
:
fNumerator
fDenominator


()

12

:
fNumerator
fDenominator
:

isEqual()

()

fr2:Fraction

isEqual()

void main(void)



()



. () -> ():
<>.< >
<>.< >(<>)

13


,
.. -:
< >
< >(<>)

14

isEqual ( )
,



//
bool Fraction::isEqual(const Fraction &other)
{
return ( fNumerator == other.fNumerator ) &&
( fDenominator == other.fDenominator );
}

()

void main(void)
{
Fraction fr(1, 2), fr2(3, 7);
bool result = fr.isEqual(fr2);
15 }



()



()

16




,
,



class Fraction
//
{
int fNumerator;
//
int fDenominator; //
//
double toDouble()
{ return (double)fNumerator/fDenominator; }
};
void main(void)
{
Fraction fr;
fr.fNumerator = 1; fr.fDenominator = 0;
double result = fr.toDouble(); //
//
17 }



class Fraction
//
{
// ....
//
void setNumerator(int n)
{
fNumerator = n;
}
//
bool setDenominator(int d)
{
if(d == 0) { fDenominator = 1; return false; }
else
{ fDenominator = d; return true; }
}
18 };

void main(void)
{
Fraction fr;
fr.setNumerator(1);
fr.setDenominator(0);
double result = fr.toDouble(); //
}

19


public, protected private

public
()
private
()
protected
()
20

class < >


{
public:
< >
protected:
< >

21

private:
< >
};

Fraction ()
,
,
class Fraction
//
{
int fNumerator;
//
int fDenominator; //

22

//
double toDouble();
//
void setNumerator(int n);
//
bool setDenominator(int d);
};

class Fraction //
{
public:
//
double toDouble();
//
void setNumerator(int n);
//
bool setDenominator(int d);

23

private:
int fNumerator;
//
int fDenominator; //
};

24

main() 23/15
.
result



void main(void)
{
Fraction fr;
fr.setNumerator(23);
fr.setDenominator(15);
//
// fr.fNumerator = 23;
// fr.fDenominator = 15;
double result = fr.toDouble(); // result = 1.5333
25 }

:
const Fracrion constFr(3, 5);

26


. ,


,
const



class Fraction //
{
public:
//
bool isEqual(const Fraction &other) const;
//
double toDouble() const;
//
void setNumerator(int n);
//
bool setDenominator(int d);
.....
27 };



Fraction fr1(2, 4);
const Fraction fr2(1, 3);
double n = fr2.toDouble();
double m = fr1.toDouble();
fr1.setNumerator(10);
fr2.setDenominator(9);

28

fr2 = fr1;
fr1 = fr2;


Fraction fr1(2, 4);
const Fraction fr2(1, 3);
double n = fr2.toDouble();
double m = fr1.toDouble();
fr1.setNumerator(10);
fr2.setDenominator(9);
/*
:

*/

29

fr2=fr1;
/*:

*/
fr1 = fr2;

Оценить