Template class Komplex with type_traits
komplex.h
Go to the documentation of this file.
1 #pragma once
2 #include <cassert> // static_assert
3 #include <cmath> // sqrt()
4 #include <iostream>
5 #include <type_traits> // std::is_floating_point<T>()
6 
7 //using namespace std; // don't use in header
8 
15 template <class T>
16 class Komplex
17 {
18  // check type of template at compile time
19  static_assert(std::is_floating_point<T>(),"Vector elements have to be floating point numbers."); // C++11
20 public:
22  Komplex() : Komplex(T(0),T(0)) // constructor forwarding
23  { }
24  // |-- Standardwert
29  Komplex(T re, T im=0.0) // Parameterkonstruktor mit ein oder zwei Argumenten
30  : _re(re), _im(im)
31  { }
32  // See rule of five: https://en.cppreference.com/w/cpp/language/rule_of_three
33  //
34  Komplex(const Komplex<T>& org) = default; // Copykonstruktor
35  Komplex( Komplex<T>&& org) = default; // Movekonstruktor
36  Komplex<T>& operator=(const Komplex<T>& rhs) = default; // Copy-Zuweisungsoperator
37  Komplex<T>& operator=( Komplex<T>&& rhs) = default; // Move-Zuweisungsoperator
38  ~Komplex() = default; // Destruktor
39 
43  // |-- Member der Instanz werden durch die Methode nicht veraendert!
44  T Get_re() const
45  {
46  return _re;
47  }
51  void Set_re(T val)
52  {
53  _re = val;
54  }
58  // |-- Member der Instanz werden durch die Methode nicht veraendert!
59  T Get_im() const
60  {
61  return _im;
62  }
66  void Set_im(T val)
67  {
68  _im = val;
69  }
70 
76 
81  // |-- Member der Instanz werden durch die Methode nicht veraendert!
82  Komplex<T> operator+(const Komplex<T>& rhs) const;
83 
84  bool operator<(const Komplex<T>& rhs) const
85  {
86  return _re < rhs._re || ( _re == rhs._re && _im < rhs._im );
87  }
88 
89  bool operator==(const Komplex<T>& rhs) const
90  {
91  return _re == rhs._re && _im == rhs._im ;
92  }
93 
94  bool operator>(const Komplex<T>& rhs) const
95  {
96  return !( *this < rhs || *this== rhs ) ;
97  }
98 
99 
100 protected:
101 private:
102  T _re;
103  T _im;
104 };
105 
110 template <class T>
111 std::ostream& operator<<(std::ostream& s, const Komplex<T>& rhs);
112 
113 
119 template <class T>
120 Komplex<T> operator+(T lhs, const Komplex<T>& rhs);
121 
122 
123 template <class T>
124 T abs(const Komplex<T>& rhs)
125 {
126  return std::sqrt(rhs.Get_re()*rhs.Get_re()+rhs.Get_im()*rhs.Get_im());
127 }
128 
129 
130 #include "komplex.tcc"
131 
bool operator<(const Komplex< T > &rhs) const
Definition: komplex.h:84
Komplex(T re, T im=0.0)
Definition: komplex.h:29
bool operator==(const Komplex< T > &rhs) const
Definition: komplex.h:89
T Get_im() const
Definition: komplex.h:59
Komplex< T > & operator+=(const Komplex< T > &rhs)
Komplex< T > & operator=(Komplex< T > &&rhs)=default
T Get_re() const
Definition: komplex.h:44
Komplex(const Komplex< T > &org)=default
Komplex< T > operator+(const Komplex< T > &rhs) const
void Set_re(T val)
Definition: komplex.h:51
void Set_im(T val)
Definition: komplex.h:66
Komplex(Komplex< T > &&org)=default
bool operator>(const Komplex< T > &rhs) const
Definition: komplex.h:94
Komplex< T > & operator=(const Komplex< T > &rhs)=default
Komplex()
Definition: komplex.h:22
~Komplex()=default
Komplex< T > operator+(T lhs, const Komplex< T > &rhs)
std::ostream & operator<<(std::ostream &s, const Komplex< T > &rhs)
T abs(const Komplex< T > &rhs)
Definition: komplex.h:124