-
Notifications
You must be signed in to change notification settings - Fork 2
/
integer_class.cpp
83 lines (67 loc) · 1.24 KB
/
integer_class.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
#include <iostream>
using namespace std;
class IntArray{
private:
int* m_data;
int m_length;
public:
IntArray(int length): m_length{length}{
m_data = new int[m_length];
};
~IntArray(){
delete[] m_data;
};
IntArray(const IntArray& array){
cout << "Inside copy constructor\n";
m_length = array.m_length;
m_data = new int[m_length];
for(int i=0; i<m_length; ++i){
m_data[i] = array.m_data[i];
}
}
int& operator[](int index){
return m_data[index];
}
IntArray& operator=(IntArray& array){
if(this == &array){
return *this;
}
delete[] m_data;
m_length = array.m_length;
m_data = new int[m_length];
for(int i=0; i<m_length; ++i){
m_data[i] = array.m_data[i];
}
return *this;
}
friend ostream& operator<<(ostream& out, IntArray& array){
for(int i=0; i<array.m_length; ++i){
out << array[i] << " ";
}
return out;
}
};
IntArray fillArray(void){
IntArray a(5);
a[0] = 5;
a[1] = 8;
a[2] = 2;
a[3] = 3;
a[4] = 6;
return a;
}
int main(int argc, char const *argv[]){
IntArray array1(5);
array1[0] = 5;
array1[1] = 8;
array1[2] = 2;
array1[3] = 3;
array1[4] = 6;
IntArray a = array1;
std::cout << a << '\n';
IntArray b(1);
a = a;
b = a;
std::cout << b << '\n';
return 0;
}