Kselax.ru

Hacker Kselax — the best hacker in the world

Menu
  • Блог
  • Контакты
  • wp plugin генератор
  • Русский
    • Русский
    • English
Menu

Шаблон Vectro с исключениями Range и Size.

Posted on 14 апреля, 201315 апреля, 2013 by admin

Напишите полный шаблон Vector с исключениями типа Range и Size.

Сделал я шаблон класса Vector, то есть я его доделал, сам класс как бы был уже сделан, конечно там есть и ошибки, я не стал их исправлять так, что так, да лень мне их исправлять, там просто ошибки по неоднозначному вызову конструкторов, а так все вроде норм работает. От файл Vector.h:

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
//ob69vlenie klacca Vector
#ifndef VECTOR_H
#define VECTOR_H
#include <vector>
using std::vector;
 
template<class T>
class Vector
{
vector<T> vec;//predctavlenie
int SIZE;//razmer
public:
class Range{};
class Size{};
//konctryktor po ymolchaniyu
Vector();
//konctryktor kopii
Vector(const Vector& a);
//konctryktor preobrazovani9
Vector(T a);
//konctryktor preobrazovani9 mojno ykazat6 razmer
Vector(int a,int=0);
//dl9 ne const
T& operator[](int i);
//dl9 const ob6ektov
T operator[](int i) const;
Vector& operator=(const Vector& a);
//pechat6
void print();
Vector& operator+=(const Vector& a);
Vector& operator-=(const Vector& a);
Vector& operator*=(const Vector& a);
Vector& operator/=(const Vector& a);
template<class R>
friend Vector<R> operator+(const Vector<R> a, const Vector<R> b);
template<class R>
friend Vector<R> operator-(const Vector<R> a, const Vector<R> b);
template<class R>
friend Vector<R> operator*(const Vector<R> a, const Vector<R> b);
template<class R>
friend Vector<R> operator/(const Vector<R> a, const Vector<R> b);
 
template<class R>
friend Vector<R> operator+(const Vector<R> a, const R b);
template<class R>
friend Vector<R> operator-(const Vector<R> a, const R b);
template<class R>
friend Vector<R> operator*(const Vector<R> a, const R b);
template<class R>
friend Vector<R> operator/(const Vector<R> a, const R b);
};
 
#endif

 

Файл Vector.cpp:

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//opredelenie klacca Vector
#include <iostream>
using std::cout;
using std::endl;
 
#include "Vector.h"
//konctryktor po ymolchaniyu
template<class T>
Vector<T>::Vector()
:SIZE(4)
{
for(int i=0;i<SIZE;i++)
vec.push_back(0);
}
 
//konctryktor dl9 konctantnux kopii
template<class T>
Vector<T>::Vector(const Vector<T>& a)
:SIZE(a.SIZE)
{
for(int i=0;i<SIZE;i++)
vec.push_back(a.vec[i]);
}
 
//konctryktor preobrazovani9
template<class T>
Vector<T>::Vector(T a)
:SIZE(4)
{
for(int i=0;i<SIZE;i++)
vec.push_back(a);
}
 
//konctryktor preobrazovani9
template<class T>
Vector<T>::Vector(int a,int c)
:SIZE(a)
{
for(int i=0;i<SIZE;i++)
vec.push_back(0);
}
 
//dl9 ne const
template<class T>
T& Vector<T>::operator[](int i)
{
if(i>=0&&i<SIZE)
return vec[i];
else
throw Range();
}
 
//dl9 const ob6ektov
template<class T>
T Vector<T>::operator[](int i) const
{
if(i>=0&&i<SIZE)
return vec[i];
else
throw Range();
}
 
template<class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& a)
{
for(int i=0;i<SIZE;i++)
{
vec[i]=a.vec[i];
}
return *this;
}
 
template<class T>
void Vector<T>::print()
{
for(int i=0;i<SIZE;i++)
{
cout <<vec[i]<<' ';
}
cout <<endl;
}
 
template<class T>
Vector<T>& Vector<T>::operator+=(const Vector<T>& a)
{
for(int i=0;i<SIZE;i++)
{
vec[i]+=a.vec[i];
}
return *this;
}
 
template<class T>
Vector<T>& Vector<T>::operator-=(const Vector<T>& a)
{
for(int i=0;i<SIZE;i++)
{
vec[i]-=a.vec[i];
}
return *this;
}
 
template<class T>
Vector<T>& Vector<T>::operator*=(const Vector<T>& a)
{
for(int i=0;i<SIZE;i++)
{
vec[i]*=a.vec[i];
}
return *this;
}
 
template<class T>
Vector<T>& Vector<T>::operator/=(const Vector<T>& a)
{
for(int i=0;i<SIZE;i++)
{
vec[i]/=a.vec[i];
}
return *this;
}
 
template<class T>
Vector<T> operator+(const Vector<T> a, const Vector<T> b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]+b[i];
}
return c;
}
 
template<class T>
Vector<T> operator+(const Vector<T> a, const T b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]+b;
}
return c;
}
 
template<class T>
Vector<T> operator-(const Vector<T> a, const Vector<T> b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]-b[i];
}
}
 
template<class T>
Vector<T> operator-(const Vector<T> a, const T b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]-b;
}
}
 
template<class T>
Vector<T> operator*(const Vector<T> a, const Vector<T> b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]*b[i];
}
}
 
template<class T>
Vector<T> operator*(const Vector<T> a, const T b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]*b;
}
}
 
template<class T>
Vector<T> operator/(const Vector<T> a, const Vector<T> b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]/b[i];
}
}
 
template<class T>
Vector<T> operator/(const Vector<T> a, const T b)
{
Vector<T> c;
for(int i=0;i<a.SIZE;i++)
{
c[i]=a[i]/b;
}
}

 

И сам файл main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//polnui wablon Vector
#include <iostream>
using std::cout;
using std::endl;
 
#include "Vector.cpp"
 
int main()
{
try
{
Vector<float> a;
a.print();
Vector<float> b(6);
b.print();
b[7];//gen icklyucheni9
}
catch(Vector<float>::Range& a)
{
cout <<"Vuxod za predelu "<<endl;
}
return 0;
}

За исключения чуточку скажу я как бы создал два вложенных класса Size и Range. Использую токо Range для генерации исключения в операции взятия индекса [], или как ее правильно называют не помню, вообщем в operator[](), опять же ловим исключение в int main(). Да и все пожалуй.

[youtube]http://www.youtube.com/watch?v=199SbZCxS04[/youtube]

Добавить комментарий Отменить ответ

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Рубрики

  • C++ (293)
  • JavaScript (1)
  • linux (1)
  • MFC (39)
  • node.js (2)
  • React (3)
  • uncategorized (3)
  • vBulletin (5)
  • Visual Studio (9)
  • wordpress (18)
  • Разное (77)

Метки

Ajax bootstrap CentOS CLI expressjs FormData GDlib google Invisible reCAPTCHA JWT media MFC php react-router-dom redux repository wordpress RTTI STL vBulletin vector Visual Studio WINAPI wordpress wp-plugins XMLHttpRequest Двоичное дерево Задачи С++ Игры С++ Исключения С++ О-большое Операторы_С++ Перегрузка операторов С++ Поиск С++ Потоки Проектирование_С++ С++ Типы_С++ Типы С++ Шаблоны С++ библиотеки локализация макросы С++ сортировка С++

Свежие комментарии

  • RA3PKJ к записи visual C++, создание диалоговых окон.
  • JasonReant к записи Создание и использование статических библиотек .lib в visual studio.
  • MyWin2020 к записи Программка для заполнения форума на vBulletin 3.8.7
  • ScottJip к записи Создание и использование статических библиотек .lib в visual studio.
  • ArnoldKig к записи Создание и использование статических библиотек .lib в visual studio.
©2021 Kselax.ru Theme by ThemeGiant