미누에요
[C++] 윤성우의 열혈 C++ 11-2 C++ 기반의 데이터 입출력 문제풀이 본문
728x90
반응형
SMALL
문제 1
예제 StablePointPtrArray.cpp 65, 66행을 다음과 같이 구성할 수 있도록 Point 클래스를 대상으로 연산자 오버로딩을 진행해보자.
for(int i=0;i<arr.GetArrLen();i++)
cout<<arr[i];
물론, 실행결과에는 변함이 없도록 연산자를 오버로딩 해야한다.
이 문제의 해법은 출력 연산자 오버로딩을 하나 더 생성하는 것에 있다.
정답
#include<iostream>
using namespace std;
class Point{
private:
int xpos, ypos;
public:
Point(int x=0, int y=0):xpos(x),ypos(y){}
friend ostream &operator<<(ostream &os, const Point &pos);
friend ostream &operator<<(ostream &os, const Point *pos);
};
ostream& operator<<(ostream& os, const Point& pos){
os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
return os;
}
ostream& operator<<(ostream& os, const Point* pos){
os << '[' << pos->xpos << ", " << pos->ypos << ']' << endl;
return os;
}
typedef Point *POINT_PTR;
class BoundCheckPointArray{
private:
POINT_PTR *arr;
int arrlen;
BoundCheckPointArray(const BoundCheckPointArray& arr){}
BoundCheckPointArray& operator=(const BoundCheckPointArray& arr){}
public:
BoundCheckPointArray(int len):arrlen(len){
arr = new POINT_PTR[len];
}
POINT_PTR& operator[](int idx){
if(idx<0 || idx>arrlen){
cout << "Array index out of bound exception" << endl;
exit(2);
}
return arr[idx];
}
int GetArrLen() const { return arrlen; }
~BoundCheckPointArray() { delete[] arr; }
};
int main(void){
BoundCheckPointArray arr(3);
arr[0] = new Point(3, 4);
arr[1] = new Point(5, 6);
arr[2] = new Point(7, 8);
for (int i = 0; i < arr.GetArrLen();i++)
cout << arr[i];
delete arr[0];
delete arr[1];
delete arr[2];
return 0;
}
실행 결과
문제 2
class BoundCheck2DIntArray{ .... }
이 클래스는 BoundCheckArray 클래스의 2차원 배열 배전이다. 따라서 다음과 같이 객체를 생성하면,
Boundcheck2DIntArray arr2d(3,4);
세로와 가로의 길이가 각각 3과 4인, int형 2차원 배열처럼 동작하는 arr2d 객체가 생성되어, 다음의 형태로 데이터를 저장 및 참조할 수 있어야 한다.
for(int n=0;n<3;n++)
for(int m=0;m<4;m++)
arr2d[n][m] = n + m;
for(int n=0;n<3;n++){
for(int m=0;n<4;m++)
cout<<arr2d[n][m]<<' ';
cout<<endl;
}
참고로 두 개의 [] 연산자를 오버로딩하는 것은 허용되지 않기 때문에, 위의 다음 문장은,
arr2d[n][m];
두 번의 [] 연산자 호출을 동반하게끔 구현해야 한다.
즉 , 첫 번째 [ ] 연산에 의해서 위의 문장은 다음과 같이 해석되어야 하며,
(arr2d.operator[](n))[m];
그리고 arr2d.operator[](n) 연산의 반환 값을 이용해서 두 번째 [] 연산은 다음과 같이 해석되어야 한다.
((반환 값).operator[])(m);
정답
#include<iostream>
#include<cstdlib>
using namespace std;
class BoundCheckIntArray{
private:
int *arr;
int arrlen;
BoundCheckIntArray(const BoundCheckIntArray &arr){}
BoundCheckIntArray &operator=(const BoundCheckIntArray &arr){}
public:
BoundCheckIntArray(int len):arrlen(len){
arr = new int[len];
}
int& operator[](int idx) const{
if(idx<0 || idx>=arrlen){
cout << "Array Index out of bound exception" << endl;
exit(1);
}
return arr[idx];
}
int GetArrLen() const { return arrlen; }
~BoundCheckIntArray() { delete[] arr; }
};
typedef BoundCheckIntArray *BoundCheckIntArrayPtr;
class BoundCheck2DIntArray{
private:
BoundCheckIntArray **arr;
int arrlen;
BoundCheck2DIntArray(const BoundCheck2DIntArray &arr){}
BoundCheck2DIntArray &operator=(const BoundCheck2DIntArray &arr){}
public:
BoundCheck2DIntArray(int col, int row):arrlen(col){
arr = new BoundCheckIntArrayPtr[col];
for (int i = 0; i < col;i++)
arr[i] = new BoundCheckIntArray(row);
}
BoundCheckIntArray& operator[] (int idx){
if(idx<0 || idx>arrlen){
cout << "Array index out of bound exception" << endl;
exit(1);
}
return *(arr[idx]);
}
~BoundCheck2DIntArray(){
for (int i = 0; i < arrlen;i++)
delete arr[i];
delete []arr;
}
};
int main(void){
BoundCheck2DIntArray arr2d(3, 4);
for (int i = 0; i < 3;i++)
for (int j = 0; j < 4;j++)
arr2d[i][j] = i + j;
for (int i = 0; i < 3;i++){
for (int j = 0; j < 4;j++)
cout << arr2d[i][j] << ' ';
cout << endl;
}
return 0;
};
실행 결과
728x90
반응형
LIST
'C++' 카테고리의 다른 글
[C++] operator new, operator new[] (1) | 2024.07.23 |
---|---|
[C++] 배열의 인덱스 연산자 오버로딩, operator[] (0) | 2024.07.23 |
[C++] 윤성우의 열혈 C++ 11-1 깊은 복사를 하는 대입 연산자의 정의 문제풀이 (0) | 2024.07.21 |
[C++] << 연산자 오버로딩, >> 연산자 오버로딩 , istream, ostream (1) | 2024.07.19 |
[C++] 연산자 오버로딩에서의 교환법칙 (0) | 2024.07.19 |