-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp07ex02Array.hpp
More file actions
86 lines (73 loc) · 1.58 KB
/
cpp07ex02Array.hpp
File metadata and controls
86 lines (73 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef ARRAY_HPP
#define ARRAY_HPP
#include <string>
#include <iostream>
#include <exception>
//array.tpp dosyasında tanımla bunları....
template <class T>
class Array
{
private:
T *_arr;
unsigned int _size;
public:
Array(void) : _size(0)
{
_arr = new T[_size]; //doğru mu? kontrolllll
};
Array(unsigned int n)
{
_size = n;
_arr = new T[n];
};
Array(const Array& other)
{
_size = other._size;
//_arr != NULL; kontrolü gibi bir şey olmamalı mı?
_arr = new T[_size];
for (unsigned int i(0); i < _size; i++){
_arr[i] = other._arr[i];
}
};
Array& operator=(const Array& other) //doğru mu?
{
if (this != &other)
{
delete[] _arr;
_size = other._size;
_arr = new T[_size];
for (unsigned int i = 0; i < _size; i++){
_arr[i] = other._arr[i];
}
}
return *this;
}
T& operator[](unsigned int i) const
{
if ( i >= _size ) // && i >= 0 kontrolü bu kontrlü yapmadan 0bir değer ver ama...
throw OutOfBoundsException();
return _arr[i];
}
unsigned int size(void) const
{
return _size;
}
~Array(void)
{
delete[] _arr;
}
class OutOfBoundsException : public std::exception {
public:
virtual const char* what() const throw(){
return "Index is out of bounds";
}
};
};
template < typename T >
std::ostream& operator<<(std::ostream& out, const Array<T>& arr)
{
for (unsigned int i(0); i < arr.size(); i++)
out << arr[i] << " ";
return out;
};
#endif