번개애비의 라이프스톼일

c++ espresso ch10 [1] - (1), (2) 본문

IT

c++ espresso ch10 [1] - (1), (2)

번개애비 2016. 11. 3. 16:43

c++ espresso

chapter 10


1. 정수형 배열을 나타내는 Array를 구현하여 보자. 생성자는 배열의 크기만을 받거나 포인터 기반의 배열을 매개 변수로 받는다. 멤버로는 현재 크기를 나타내는 size와 데이터가 저장된 위치를 가리키는 포인터인 ptr이 있다.


(1) 입력 연산자인 >> 을 중복 정의한다.


(2) ==와 != 연산자를 중복 정의하여 두 개의 배열을 비교할 수 있도록 한다.




#include <iostream>

#include <assert.h>

using namespace std;


// 향상된 배열을 나타낸다. 

class MyArray {

friend ostream& operator<<(ostream &, const MyArray &); // 출력 연산자 <<

friend istream& operator>>(istream &input, const MyArray &a);

friend bool operator==(const MyArray &v1, const MyArray &v2);

friend bool operator!=(const MyArray &v1, const MyArray &v2);

private:

int *data; // 배열의 데이터

int size; // 배열의 크기


public:

MyArray(int size = 10); // 디폴트 생성자

~MyArray(); // 소멸자


int getSize() const; // 배열의 크기를 반환

MyArray& operator=(const MyArray &a); // = 연산자 중복 정의

int& operator[](int i); // [] 연산자 중복: 설정자

};


MyArray::MyArray(int s) {

size = (s > 0 ? s : 10);    // 디폴트 크기를 10으로 한다.

data = new int[size];      // 동적 메모리 할당


for (int i = 0; i < size; i++)

data[i] = 0;           // 요소들의 초기화 

}


MyArray::~MyArray() {

delete[] data;                       // 동적 메모리 반납

data = NULL;

}


MyArray& MyArray::operator=(const MyArray& a) {

if (&a != this) { // 자기 자신인지를 체크

delete[] data; // 동적 메모리 반납

size = a.size; // 새로운 크기를 설정

data = new int[size]; // 새로운 동적 메모리 할당 


for (int i = 0; i < size; i++)

data[i] = a.data[i]; // 데이터 복사

}

return *this; // a = b = c와 같은 경우를 대비

}


int MyArray::getSize() const

{

return size;

}


int& MyArray::operator[](int index) {

assert(0 <= index && index < size); // 인데스가 범위에 있지 않으면 중지

return data[index];

}


// 프렌드 함수 정의

ostream& operator<<(ostream &output, const MyArray &a) {

int i;

for (i = 0; i < a.size; i++) {

output << a.data[i] << ' ';

}

output << endl;

return output; // cout << a1 << a2 << a3와 같은 경우 대비

}


istream& operator>>(istream &input, const MyArray &a) {

int i;

for (i = 0; i < a.size; i++) {

input >> a.data[i];

}

return input; // cout << a1 << a2 << a3와 같은 경우 대비

}

bool operator==(const MyArray &v1, const MyArray &v2)

{

int i;

if (v1.getSize() == v2.getSize())

for (i = 0; i < v1.getSize(); i++) {

if (v1.data[i] != v2.data[i]) return false;

}

return true;

}

bool operator!=(const MyArray &v1, const MyArray &v2)

{

return !(v1 == v2); // 중복된 == 연산자를 이용

}


int main()

{

MyArray a1(5);


a1[0] = 1;

a1[1] = 2;

a1[2] = 3;

a1[3] = 4;

a1[4] = 5;

cout << a1;

MyArray a2(5);

cin >> a2;

cout << a2;

cout.setf(ios::boolalpha);

cout << (a1 == a2) << endl;

return 0;

}



Comments