-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorUserDefinedSizeNewOp.cpp
More file actions
79 lines (56 loc) · 1.2 KB
/
vectorUserDefinedSizeNewOp.cpp
File metadata and controls
79 lines (56 loc) · 1.2 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
//A function or Template that creates a vector of user given size N using new operator
#include <iostream>
template <class T>
T* createArray(size_t N) // template function that returns the pointer
{ // to the array
return new T[N];
}
//This is a generic array template class that handles a dynamic array
template <class T>
class array
{
public:
//Constructor
array(size_t n) : N(n){
data = new T[N];
}
//Destructor
~array(){
if (data)
{
delete[] data;
std::cout << "\nDeleted\n";
}
}
T& operator[](size_t index){
return data[index];
}
void operator= (T val) {
for (size_t i = 0; i < N; i++)
data[i] = val;
}
private:
T *data;
size_t N;
};
int main()
{
int n = 4;
std::cin >> n;
//int *a = nullptr;// = new int[n];
//a = createArray<int>(static_cast<size_t>(n));
////Lets assign some values to some of them only
//a[0] = 1;
//a[1] = 2;
//a[2] = 3;
//a[3] = 4;
//for (int i = 0; i < 4; i++)
// std::cout << "\nVal " << i << " = " << a[i];
array<int> b(n);
b = 1;//initialize the contents with the value of 1
b[0] = 5;
for (int i = 0; i < n; i++)
std::cout << "\nVal " << i << " = " << b[i];
system("pause");
return 0;
}