C++

열혈 C++ 프로그래밍 OOP 프로젝트 10

나른한여우 2024. 4. 5. 18:06

10에서는 저번에 만들었던 AccountArray를 대체할 클레스 템플릿을 정의해

다양한 데이터를 저장할 수 있도록 일반화하는 것이 목표다.

BoundCheckArray 이름의 헤더파일을 만들고 같은 이름으로 클래스 템플릿 정의

 

1. BoundCheckArray.h

#ifndef __BOUNDCHECKARRAY_H__
#define __BOUNDCHECKARRAY_H__

template <class T>
class BoundCheckArray
{
private:
	T* arr;
	int arrlen;
	//배열에 대한 복사 및 대입방지
	BoundCheckArray(const BoundCheckArray& arr) {}
	BoundCheckArray& operator=(const BoundCheckArray& arr) {}
public:
	BoundCheckArray(int len = 100);
	T& operator[] (int idx);
	T operator[] (int idx) const;
	int GetArrLen() const;
	~BoundCheckArray();
};

template <class T>
BoundCheckArray<T>::BoundCheckArray(int len) : arrlen(len)
{
	arr = new T[len];
}

template <class T>
T& BoundCheckArray<T>::operator[](int idx)
{
	if (idx < 0 || idx >= arrlen)
	{
		cout << "Array index out of Bound exception" << endl;
		exit(1);
	}
	return arr[idx];
}

template <class T>
T BoundCheckArray<T>::operator[](int idx) const
{
	if (idx < 0 || idx >= arrlen)
	{
		cout << "Array index out of Bound exception" << endl;
		exit(1);
	}
	return arr[idx];
}

template <class T>
int BoundCheckArray<T>::GetArrLen() const
{
	return arrlen;
}

template <class T>
BoundCheckArray<T>::~BoundCheckArray()
{
	delete[]arr;
}
#endif

 

2. AccountHandler.h

기존 AccountArray.h를 포함하는걸 BoundCheckArray.h로 변경하고

클래스탬플릿이기때문에 BoundCheckArray뒤에 <Account*>로 타입을 명시해주어야함

#ifndef __ACCOUNTHENDLER_H__
#define __ACCOUNTHENDLER_H__

#include "Account.h"
//#include "AccountArray.h"
#include "BoundCheckArray.h"

class AccountHandler
{
private:
	BoundCheckArray<Account*> userList;
	int userIndex;
public:
	AccountHandler();
	void ShowMenu();
	void MakeAccount();
	void MakeNomalAccount();
	void MakeCreditAccount();
	void DepositMoney();
	void WithdrawMoney();
	void ShowAllAccount();
	~AccountHandler();
};
#endif