Template class Komplex with concepts
komplex2.h
Go to the documentation of this file.
1 #pragma once
2 #include <cassert> // static_assert
3 #include <cmath> // sqrt()
4 #include <concepts>
5 #include <iostream>
6 //#include <type_traits> // std::is_floating_point<T>()
7 
13 // Concepts: See [Grimm: C++20 Get the details, p.10 and ยง4.1]
14 // see also: https://akrzemi1.wordpress.com/2020/01/29/requires-expression/
15 template<typename T> // C++20: concepts
16 concept FloatDouble = sizeof(T)>=sizeof(float) && std::floating_point<T>; // float32 and larger
17 //concept FloatDouble = sizeof(T)>=sizeof(double) && std::floating_point<T>; // float64 and larger
18 
19 
20 template<FloatDouble T>
21 class Komplex
22 {
23 public:
25  Komplex() : Komplex(T(0),T(0)) // constructor forwarding
26  { }
27  // |-- Standardwert
32  Komplex(T re, T im=0.0) // Parameterkonstruktor mit ein oder zwei Argumenten
33  : _re(re), _im(im)
34  { }
35  // See rule of five: https://en.cppreference.com/w/cpp/language/rule_of_three
36  //
37  Komplex(const Komplex<T>& org) = default; // Copykonstruktor
38  Komplex( Komplex<T>&& org) = default; // Movekonstruktor
39  Komplex<T>& operator=(const Komplex<T>& rhs) = default; // Copy-Zuweisungsoperator
40  Komplex<T>& operator=( Komplex<T>&& rhs) = default; // Move-Zuweisungsoperator
41  ~Komplex() = default; // Destruktor
42 
46  // |-- Member der Instanz werden durch die Methode nicht veraendert!
47  T Get_re() const
48  {
49  return _re;
50  }
54  void Set_re(T val)
55  {
56  _re = val;
57  }
61  // |-- Member der Instanz werden durch die Methode nicht veraendert!
62  T Get_im() const
63  {
64  return _im;
65  }
69  void Set_im(T val)
70  {
71  _im = val;
72  }
73 
79 
84  // |-- Member der Instanz werden durch die Methode nicht veraendert!
85  Komplex<T> operator+(const Komplex<T>& rhs) const;
86 
87  bool operator<(const Komplex<T>& rhs) const
88  {
89  return _re < rhs._re || ( _re == rhs._re && _im < rhs._im );
90  }
91 
92  bool operator==(const Komplex<T>& rhs) const
93  {
94  return _re == rhs._re && _im == rhs._im ;
95  }
96 
97  bool operator>(const Komplex<T>& rhs) const
98  {
99  return !( *this < rhs || *this== rhs ) ;
100  }
101 
106  template <class S>
107  friend std::ostream& operator<<(std::ostream& s, const Komplex<S>& rhs);
108 
109 protected:
110 private:
111  T _re;
112  T _im;
113 };
114 
120 template <FloatDouble T>
121 Komplex<T> operator+(T lhs, const Komplex<T>& rhs);
122 
123 
124 template <FloatDouble T>
125 T abs(const Komplex<T>& rhs)
126 {
127  return std::sqrt(rhs.Get_re()*rhs.Get_re()+rhs.Get_im()*rhs.Get_im());
128 }
129 
130 
131 #include "komplex2.tcc"
132 
bool operator<(const Komplex< T > &rhs) const
Definition: komplex2.h:87
Komplex(T re, T im=0.0)
Definition: komplex2.h:32
bool operator==(const Komplex< T > &rhs) const
Definition: komplex2.h:92
T Get_im() const
Definition: komplex2.h:62
Komplex< T > & operator+=(const Komplex< T > &rhs)
friend std::ostream & operator<<(std::ostream &s, const Komplex< S > &rhs)
Komplex< T > & operator=(Komplex< T > &&rhs)=default
T Get_re() const
Definition: komplex2.h:47
Komplex(const Komplex< T > &org)=default
Komplex< T > operator+(const Komplex< T > &rhs) const
void Set_re(T val)
Definition: komplex2.h:54
void Set_im(T val)
Definition: komplex2.h:69
Komplex(Komplex< T > &&org)=default
bool operator>(const Komplex< T > &rhs) const
Definition: komplex2.h:97
Komplex< T > & operator=(const Komplex< T > &rhs)=default
Komplex()
Definition: komplex2.h:25
~Komplex()=default
Komplex< T > operator+(T lhs, const Komplex< T > &rhs)
concept FloatDouble
Definition: komplex2.h:16
T abs(const Komplex< T > &rhs)
Definition: komplex2.h:125