MyVector
myvector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "myexceptions.h" // Exeption handlers and classes
4 #include <iostream>
5 
10 class MyVector
11 {
12 public:
18 
19  MyVector(const long long int n = 0); // Standard-, Parameterkonstruktor
20 
21 #ifndef WRONG_CODE
27  MyVector(const MyVector& orig); // Copykonstruktor
28 #endif
31  virtual ~MyVector(); // Destruktor
32 
33 #ifndef WRONG_CODE
39  MyVector& operator=(MyVector const &orig); // Zuweisungsoperator
40 #endif
47  friend std::ostream & operator<<(std::ostream & s, MyVector const & orig);
48 
53  const long long int& size() const
54  {
55  return length_;
56  }
57 
64  double& operator[](const long long int i)
65  {
66 #ifndef NDEBUG
67 #pragma message(" ########## Debug mode ###############")
68  IndexCheck(i);
69 #endif
70  return val_[i];
71  }
72 
79  const double& operator[](const long long int i) const // const notw. fuer const. Instanzen von MyVektor
80  {
81 #ifndef NDEBUG
82 #pragma message(" ########## Debug mode ###############")
83  IndexCheck(i);
84 #endif
85  return val_[i];
86  }
87 
88 
89 private:
95  void IndexCheck(const long long int &i) const
96  {
97  if ( i<0 || i>=size() )
98  {throw OutOfRange(i,size()-1);} // Indexfehler
99  }
100 
101 protected: // private Members von MyVector duerfen in abgeleiteter Klasse benutzt werden
102  long long int length_;
103  double * val_;
104 };
105 
106 
107 
108 
My own vector class.
Definition: myvector.h:11
MyVector(const long long int n=0)
Allocates a vector with n elements on the heap.
Definition: myvector.cpp:8
const double & operator[](const long long int i) const
Returns the value of element i.
Definition: myvector.h:79
const long long int & size() const
Returns the number of elements.
Definition: myvector.h:53
friend std::ostream & operator<<(std::ostream &s, MyVector const &orig)
Output of the vector orig.
double & operator[](const long long int i)
Returns the value of element i.
Definition: myvector.h:64
long long int length_
number of elements
Definition: myvector.h:102
virtual ~MyVector()
Deallocates the heap memory.
Definition: myvector.cpp:38
MyVector & operator=(MyVector const &orig)
Reallocates the vector with the same elements as vector orig and copies its elements.
Definition: myvector.cpp:45
double * val_
pointer to allocated memory
Definition: myvector.h:103