-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy path06_homework_05_answer.cpp
69 lines (51 loc) · 1.06 KB
/
06_homework_05_answer.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
#include <bits/stdc++.h>
using namespace std;
class MyVector {
private:
int *arr;
int len = 100;
public:
MyVector(int len, int default_value = 0) {
this->len = len;
this->arr = new int[len];
for (int i = 0; i < len; ++i) {
this->arr[i] = default_value;
}
}
MyVector(const MyVector & another) {
len = another.len;
this->arr = new int[len];
for (int i = 0; i < len; ++i)
arr[i] = another.arr[i];
}
~MyVector() {
delete[] this->arr;
}
int Get(int pos) {
if (pos < len)
return this->arr[pos];
else {
cout<<"Invalid access\n";
return -1;
}
}
void Set(int pos, int val = 0) {
if (pos < len)
this->arr[pos] = val;
else
cout<<"Invalid access\n";
}
// Breaks Data-Hiding concept. User has access to private data and can corrupt the system
int& GetLen() {
return len;
}
};
int main() {
MyVector v(10, 12345);
cout<<v.Get(4)<<"\n";
// User access array length and set to zero!
int &l = v.GetLen();
l = 0;
cout<<v.Get(4)<<"\n";
return 0;
}