-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.cpp
69 lines (66 loc) · 1.34 KB
/
array.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 "Array.h"
#include <cstdlib>
#include<iostream>
using namespace std;
Array::Array(int size)
{
//사이즈를 확인하고 양수이면 new를 사용하여 배열 data를 할당, len값 초기화
if(size<0)
{
cout << "Input Error!" << endl;
exit(-1);
}
else
{
data = new int[size];
len = size;
}
}
Array::~Array()
{
// 소멸자; 할당된 메모리 해제
delete[]data;
}
int Array::length() const
{
// 배열의 크기 리턴
return len;
}
// 배열에 원소를 대입하거나 값을 반환하는 부분으로 []연산자의 오버로딩이다
int& Array::operator[](int i) // 배열에 원소 삽입
{
static int tmp;
// 배열의 인덱스가 범위 내에 있으면 값 리턴, 그렇지 않으면 에러메세지 출력하고 tmp리턴
if(i >= 0 && i < len)
{
return data[i];
}
else
{
cout << "Over Boundary!!" << endl;
return tmp;
}
}
int Array::operator[](int i) const // 배열의 원소값 반환
{
//배열의 인덱스가 범위 내에 잇으면 값을 리턴, 그렇지 않으면 에러메세지 출력하고 0을 리턴
if(i >= 0 && i<len)
{
return data[i];
}
else
{
cout << "Over Boundary!!" << endl;
return 0;
}
}
void Array::print() //배열의 모든 내용을 출력해주는 함수
{
int i;
cout<<"[";
for (i = 0; i < len; i++) {
cout << data[i] << " " ;
}
cout << "]";
cout<<endl;
}