Calculator Step 6

- Main Page
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Private Member Functions | Private Attributes | List of all members
number_rational Class Reference

`#include <number_impl.hpp>`

Inheritance diagram for number_rational:

## Public Member Functions
| number_rational (rational< long long > value)
| number_rational (long long num)
| number_rational (long long num, long long den)
rational< long long > | value () const
virtual std::shared_ptr < number_impl > | promote_to_void ()
virtual std::shared_ptr < number_impl > | promote_to_double ()
Public Member Functions inherited from number_impl
virtual | ~number_impl ()
void | print (std::ostream &stream) const
std::string | to_string () const
void | save (std::ostream &stream) const
bool | equals (number_impl const &rhs) const
bool | less (number_impl const &rhs) const
std::shared_ptr< number_impl > | add (number_impl const &rhs)
std::shared_ptr< number_impl > | subtract (number_impl const &rhs)
std::shared_ptr< number_impl > | multiply (number_impl const &rhs)
std::shared_ptr< number_impl > | divide (number_impl const &rhs)
void | add_ref ()
void | del_ref ()
std::shared_ptr< number_impl > | promote (number_impl &rhs) const
virtual std::shared_ptr < number_impl > | promote_to_long ()
virtual std::shared_ptr < number_impl > | promote_to_rational ()

## Private Member Functions
virtual std::string | do_to_string () const
virtual void | do_save (std::ostream &stream) const
virtual bool | do_equals (number_impl const &rhs) const
virtual bool | do_less (number_impl const &rhs) const
virtual std::shared_ptr < number_impl > | do_add (number_impl const &rhs)
virtual std::shared_ptr < number_impl > | do_subtract (number_impl const &rhs)
virtual std::shared_ptr < number_impl > | do_multiply (number_impl const &rhs)
virtual std::shared_ptr < number_impl > | do_divide (number_impl const &rhs)
virtual std::shared_ptr < number_impl > | do_promote (number_impl &rhs) const

## Private Attributes
rational< long long > | value_

## Additional Inherited Members
Static Public Member Functions inherited from number_impl
static std::shared_ptr < number_impl > | read_library (std::istream &stream)
Protected Member Functions inherited from number_impl
| number_impl ()

## Detailed Description

Represent a rational number. For the greatest range, always use `long` to represent the numerator and denominator.

Definition at line 122 of file number_impl.hpp.

## Constructor & Destructor Documentation

number_rational::number_rational | ( | rational< long long > | value | ) |
| inline

Definition at line 124 of file number_impl.hpp.

124 : number_impl(), value_(value) {}
number_impl::number_impl
number_impl()
Definition: number_impl.hpp:54
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

number_rational::number_rational | ( | long long | num | ) |
| inline

Definition at line 125 of file number_impl.hpp.

125 : number_impl(), value_(num) {}
number_impl::number_impl
number_impl()
Definition: number_impl.hpp:54
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

number_rational::number_rational | ( | long long | num,
|  | long long | den
| ) |  |
| inline

Definition at line 126 of file number_impl.hpp.

126 : number_impl(), value_(num, den) {}
number_impl::number_impl
number_impl()
Definition: number_impl.hpp:54
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

## Member Function Documentation

std::shared_ptr< number_impl > number_rational::do_add | ( | number_impl const & | rhs | ) |
| private virtual

Implements number_impl.

Definition at line 271 of file number_impl.cpp.

References value(), and value_.

272 {
273 rational<long long> result{ value_ + dynamic_cast<number_rational const&>(rhs).value() };
274 if (result.denominator() == 1)
275 return std::make_shared<number_long>(result.numerator());
276 else
277 return std::make_shared<number_rational>(result);
278 }
number_rational
Definition: number_impl.hpp:122
rational< long long >
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

std::shared_ptr< number_impl > number_rational::do_divide | ( | number_impl const & | rhs | ) |
| private virtual

Implements number_impl.

Definition at line 298 of file number_impl.cpp.

References rational< T >::denominator(), rational< T >::numerator(), value(), and value_.

299 {
300 rational<long long> right{ dynamic_cast<number_rational const&>(rhs).value() };
301 if (right.numerator() == 0)
302 throw calc_error{"division by zero"};
303 rational<long long> result = value_ / right;
304 if (result.denominator() == 1)
305 return std::make_shared<number_long>(result.numerator());
306 else
307 return std::make_shared<number_rational>(result);
308 }
calc_error
Definition: calc_error.hpp:11
rational::denominator
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
rational::numerator
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
number_rational
Definition: number_impl.hpp:122
rational< long long >
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

bool number_rational::do_equals | ( | number_impl const & | rhs | ) | const
| private virtual

Implements number_impl.

Definition at line 259 of file number_impl.cpp.

References value().

261 {
262 return value() == dynamic_cast<number_rational const&>(rhs).value();
263 }
number_rational
Definition: number_impl.hpp:122
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127

bool number_rational::do_less | ( | number_impl const & | rhs | ) | const
| private virtual

Implements number_impl.

Definition at line 265 of file number_impl.cpp.

References value().

267 {
268 return value() < dynamic_cast<number_rational const&>(rhs).value();
269 }
number_rational
Definition: number_impl.hpp:122
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127

std::shared_ptr< number_impl > number_rational::do_multiply | ( | number_impl const & | rhs | ) |
| private virtual

Implements number_impl.

Definition at line 289 of file number_impl.cpp.

References value(), and value_.

290 {
291 rational<long long> result{ value_ * dynamic_cast<number_rational const&>(rhs).value() };
292 if (result.denominator() == 1)
293 return std::make_shared<number_long>(result.numerator());
294 else
295 return std::make_shared<number_rational>(result);
296 }
number_rational
Definition: number_impl.hpp:122
rational< long long >
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

std::shared_ptr< number_impl > number_rational::do_promote | ( | number_impl & | rhs | ) | const
| private virtual

Implements number_impl.

Definition at line 310 of file number_impl.cpp.

References number_impl::promote_to_rational().

312 {
313 return rhs.promote_to_rational();
314 }
number_impl::promote_to_rational
virtual std::shared_ptr< number_impl > promote_to_rational()
Definition: number_impl.cpp:107

void number_rational::do_save | ( | std::ostream & | stream | ) | const
| private virtual

Implements number_impl.

Definition at line 253 of file number_impl.cpp.

References value().

255 {
256 stream << "rational " << value() << ' ';
257 }
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127

std::shared_ptr< number_impl > number_rational::do_subtract | ( | number_impl const & | rhs | ) |
| private virtual

Implements number_impl.

Definition at line 280 of file number_impl.cpp.

References value(), and value_.

281 {
282 rational<long long> result{ value_ - dynamic_cast<number_rational const&>(rhs).value() };
283 if (result.denominator() == 1)
284 return std::make_shared<number_long>(result.numerator());
285 else
286 return std::make_shared<number_rational>(result);
287 }
number_rational
Definition: number_impl.hpp:122
rational< long long >
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

std::string number_rational::do_to_string | ( |  | ) | const
| private virtual

Implements number_impl.

Definition at line 245 of file number_impl.cpp.

References value().

247 {
248 std::ostringstream stream{};
249 stream << value();
250 return stream.str();
251 }
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127

std::shared_ptr< number_impl > number_rational::promote_to_double | ( |  | ) |
| virtual

Reimplemented from number_impl.

Definition at line 321 of file number_impl.cpp.

References value().

322 {
323 return std::make_shared<number_double>(static_cast<double>(value()));
324 }
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127

std::shared_ptr< number_impl > number_rational::promote_to_void | ( |  | ) |
| virtual

Reimplemented from number_impl.

Definition at line 316 of file number_impl.cpp.

317 {
318 return std::make_shared<number_void>();
319 }

rational<long long> number_rational::value | ( |  | ) | const
| inline

Definition at line 127 of file number_impl.hpp.

References value_.

Referenced by do_add(), do_divide(), do_equals(), do_less(), do_multiply(), do_save(), do_subtract(), do_to_string(), and promote_to_double().

127 { return value_; }
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141

## Member Data Documentation

rational<long long> number_rational::value_
| private

Definition at line 141 of file number_impl.hpp.

Referenced by do_add(), do_divide(), do_multiply(), do_subtract(), and value().

The documentation for this class was generated from the following files:

- number_impl.hpp
- number_impl.cpp