Rings for MAT 685
Mathematical ring implementation to demonstrate templates and inheritance
Public Member Functions | Protected Member Functions | Protected Attributes | Related Functions | List of all members
Rings::Rational< T > Class Template Reference

a type for rational numbers More...

#include <rational.hpp>

Inheritance diagram for Rings::Rational< T >:
Rings::Field_Element Rings::Integral_Domain_Element Rings::Commutative_Ring_Element Rings::Ring_Element

Public Member Functions

 Rational ()
 initializes to 0
 
 Rational (T)
 initializes to the given value
 
 Rational (T, T)
 the first value is for the numerator; the second, for the denominator
 
 Rational (const Integer< T > &)
 initializes numerator to the Integer’s value
 
 Rational (const Rational< T > &)
 copy constructor
 
virtual bool is_one () const override
 should be True iff element is multiplicative identity
 
virtual bool is_zero () const override
 should be True iff element is additive identity
 
virtual Ring_Element & operator+ (const Ring_Element &) const override
 addition: other element should be of same type, use a cast
 
virtual Ring_Element & operator- (const Ring_Element &) const override
 subtraction: other element should be of same type, use a cast
 
virtual Ring_Element & operator* (const Ring_Element &) const override
 multiplicationL other element should be of same type, use a cast
 
virtual Field_Element & operator/ (const Field_Element &) const override
 division: other element should be of same type, use a cast
 
const Rational< T > & operator= (const Ring_Element &)
 assignment operator may be needed
 
virtual bool operator== (const Ring_Element &) const override
 comparison: other element has same value
 
virtual bool operator!= (const Ring_Element &) const override
 comparison: other element has different value
 
virtual Field_Element & inverse () const override
 returns reciprocal
 
T get_numerator () const
 returns the numerator
 
T get_denominator () const
 returns the denominator
 
- Public Member Functions inherited from Rings::Field_Element
virtual bool has_inverse () const override
 fields are integral domains where nonzero elements have inverses
 
- Public Member Functions inherited from Rings::Integral_Domain_Element
virtual bool is_cancellable () const override
 integral domains are commutative rings without zero divisors, so the element should be cancellable (see description of class)
 
- Public Member Functions inherited from Rings::Commutative_Ring_Element
virtual bool is_commutative () const override
 Duh.
 

Protected Member Functions

void simplify ()
 uses the gcd to reduce num and denom (if necessary)
 

Protected Attributes

T num
 the numerator
 
T den
 the denominator
 

Related Functions

(Note that these are not member functions.)

template<typename T >
ostream & operator<< (ostream &os, const Rational< T > &r)
 prints in the form a/b
 

Detailed Description

template<typename T>
class Rings::Rational< T >

a type for rational numbers

The template gives the base type; e.g., Rational<short> or Rational<uint32_t>.

Examples:
test_polynomial.cpp, and test_rings.cpp.

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