Public Member Functions | |
Standard constructors. | |
| RationalNumber (int num, unsigned int denom) | |
| Constructor from numerator and denominator. | |
| RationalNumber () | |
| Default constructor. Initialize to zero. | |
| RationalNumber (const RationalNumber &nb) | |
| Copy constructor. | |
| template<typename T> | RationalNumber (const T num) |
| Generic constructor. | |
Accessors | |
| const int & | num () const |
| Get the numerator. | |
| const unsigned & | denom () const |
| Get the denominator. | |
Usual Operators. | |
| RationalNumber | operator+ (const RationalNumber &nb) const |
| Usual numerical operator. | |
| RationalNumber | operator- (const RationalNumber &nb) const |
| Usual numerical operator. | |
| RationalNumber | operator- () const |
| Usual numerical operator. | |
| RationalNumber | operator * (const RationalNumber &nb) const |
| Usual numerical operator. | |
| RationalNumber | operator/ (const RationalNumber &nb) const |
| Usual numerical operator. | |
| RationalNumber & | operator+= (const RationalNumber &nb) |
| Usual numerical operator. | |
| RationalNumber & | operator-= (const RationalNumber &nb) |
| Usual numerical operator. | |
| RationalNumber & | operator *= (const RationalNumber &nb) |
| Usual numerical operator. | |
| RationalNumber & | operator/= (const RationalNumber &nb) |
| Usual numerical operator. | |
| bool | operator< (const RationalNumber &nb) const |
| Usual numerical operator. | |
| bool | operator> (const RationalNumber &nb) const |
| Usual numerical operator. | |
| bool | operator<= (const RationalNumber &nb) const |
| Usual numerical operator. | |
| bool | operator>= (const RationalNumber &nb) const |
| Usual numerical operator. | |
| bool | operator== (const RationalNumber &nb) const |
| Usual numerical operator. | |
| bool | operator!= (const RationalNumber &nb) const |
| Usual numerical operator. | |
Type conversion methods. | |
| int | to_int () const |
| Provide Explicit cast operator. | |
| double | to_double () const |
| Provide Explicit cast operator. | |
Protected Member Functions | |
Fraction simplification | |
| RationalNumber & | set_result () |
| Simplifies the fraction. | |
| RationalNumber & | set_result (int num, unsigned int denom) |
| Simplifies the fraction. | |
This is a rational numbers implementation.
The way the constructor works ables us to work only on simplified fractions. So, the numerator (num_) and denominator(denom_) are always relatively prime.
Even after operations, the obtained fraction is simplified.
|
|
Generic constructor.
|
|
|
Provide Explicit cast operator.
|
|
|
Provide Explicit cast operator.
|
|
|
Simplifies the fraction.
|
|
||||||||||||
|
Simplifies the fraction.
|
1.3.7