Rings for MAT 685
Mathematical ring implementation to demonstrate templates and inheritance
integer.hpp
1 #ifndef __INTEGER_HPP_
2 #define __INTEGER_HPP_
3 
4 #include "rings.hpp"
5 
6 namespace Rings {
7 
16 template<typename T>
17 class Integer : virtual public Integral_Domain_Element {
18 
19 private:
20 
21  T value;
22 
23 public:
24 
26  Integer<T>();
28  Integer<T>(T);
30  Integer<T>(const Integer<T> & other);
31 
33  const Integer<T> & operator = (const Ring_Element &);
34 
35  virtual bool is_one() const override;
36  virtual bool is_zero() const override;
37  virtual bool has_inverse() const override;
38 
39  virtual bool operator == (const Ring_Element &) const override;
40  virtual bool operator != (const Ring_Element &) const override;
41 
42  virtual Ring_Element & operator + (const Ring_Element &) const override;
43  virtual Ring_Element & operator - (const Ring_Element &) const override;
44  virtual Ring_Element & operator * (const Ring_Element &) const override;
45 
47  T get_value() const;
48 
49 };
50 
51 template<typename T>
52 Integer<T>::Integer() : value(0) { }
53 
54 template<typename T>
55 Integer<T>::Integer(T v) : value(v) { }
56 
57 template<typename T>
58 Integer<T>::Integer(const Integer<T> & other) : value(other.value) { }
59 
60 template<typename T>
61 bool Integer<T>::is_one() const { return value == 1; }
62 
63 template<typename T>
64 bool Integer<T>::is_zero() const { return value == 0; }
65 
66 template<typename T>
67 bool Integer<T>::has_inverse() const { return value == 1 or value == -1; }
68 
69 template<typename T>
71  static Integer<T> result;
72  auto o = dynamic_cast<const Integer<T> &>(other);
73  result.value = value + o.value;
74  return result;
75 }
76 
77 template<typename T>
79  static Integer<T> result;
80  auto o = dynamic_cast<const Integer<T> &>(other);
81  result.value = value - o.value;
82  return result;
83 }
84 
85 template<typename T>
87  static Integer<T> result;
88  auto & o = dynamic_cast<const Integer<T> &>(other);
89  result.value = value * o.value;
90  return result;
91 }
92 
93 template<typename T>
95  if (*this != other) {
96  auto & o = dynamic_cast<const Integer<T> &>(other);
97  value = o.value;
98  }
99  return *this;
100 }
101 
102 template<typename T>
103 bool Integer<T>::operator == (const Ring_Element & other) const {
104  auto & o = dynamic_cast<const Integer<T> &>(other);
105  return value == o.value;
106 }
107 
108 template<typename T>
109 bool Integer<T>::operator != (const Ring_Element & other) const {
110  auto & o = dynamic_cast<const Integer<T> &>(other);
111  return value != o.value;
112 }
113 
114 template<typename T>
115 T Integer<T>::get_value() const { return value; }
116 
121 template <typename T>
122 ostream & operator << (ostream & os, Integer<T> i) {
123  os << i.get_value();
124  return os;
125 }
126 
127 }
128 
129 #endif
const Integer< T > & operator=(const Ring_Element &)
assignment operator may be needed
Definition: integer.hpp:94
T get_value() const
returns the value of this
Definition: integer.hpp:115
Integer()
initializes to zero
Definition: integer.hpp:52
elements of this type should be cancellable; there should be no zero divisors
Definition: rings.hpp:53
virtual Ring_Element & operator*(const Ring_Element &) const override
multiplicationL other element should be of same type, use a cast
Definition: integer.hpp:86
Definition: integer.hpp:6
encapsulates integers under the Ring_Element rubric
Definition: integer.hpp:17
virtual bool operator!=(const Ring_Element &) const override
comparison: other element has different value
Definition: integer.hpp:109
virtual bool is_one() const override
should be True iff element is multiplicative identity
Definition: integer.hpp:61
virtual bool has_inverse() const override
should be True iff element has a multiplicative inverse
Definition: integer.hpp:67
virtual bool is_zero() const override
should be True iff element is additive identity
Definition: integer.hpp:64
virtual bool operator==(const Ring_Element &) const override
comparison: other element has same value
Definition: integer.hpp:103
a class for elements with the capabilities of ring arithmetic
Definition: rings.hpp:9
virtual Ring_Element & operator-(const Ring_Element &) const override
subtraction: other element should be of same type, use a cast
Definition: integer.hpp:78
virtual Ring_Element & operator+(const Ring_Element &) const override
addition: other element should be of same type, use a cast
Definition: integer.hpp:70