티스토리 뷰
기존에 AccountHandler 클래스에서 Account * 형으로 계정 정보를 저장하는 방식에서
Account** 형을 저장해서 bound check까지 수행하는 방식으로 변경.
AccountArray.h
#ifndef __ACCOUNTARRAY__
#define __ACCOUNTARRAY__
#include "Account.h"
typedef Account* ACCOUNT_PTR;
class BoundCheckAccountPtrArray
{
private:
ACCOUNT_PTR * arr;
int arrlen;
// 복사 생성자, 대입 연산자 오버라이딩 호출 금지
BoundCheckAccountPtrArray(const BoundCheckAccountPtrArray &arr);
BoundCheckAccountPtrArray& operator= (const BoundCheckAccountPtrArray &arr);
public:
BoundCheckAccountPtrArray(int len=100); // default size 100
ACCOUNT_PTR& operator[] (int idx);
ACCOUNT_PTR operator[] (int idx) const;
int GetArrLen() const;
~BoundCheckAccountPtrArray();
};
#endif
AccountArray.cpp
#include "BankingCommon.h"
#include "AccountArray.h"
BoundCheckAccountPtrArray::BoundCheckAccountPtrArray(int len)
: arrlen(len)
{ arr = new ACCOUNT_PTR[len]; }
ACCOUNT_PTR& BoundCheckAccountPtrArray::operator[] (int idx)
{
if(idx < 0 || idx >= arrlen)
{
cout << "Array idx out of bound exception" <<endl;
exit(1);
}
return arr[idx];
}
ACCOUNT_PTR BoundCheckAccountPtrArray::operator[] (int idx) const
{
if(idx < 0 || idx >= arrlen)
{
cout << "Array idx out of bound exception" <<endl;
exit(1);
}
return arr[idx];
}
int BoundCheckAccountPtrArray::GetArrLen() const { return arrlen; }
BoundCheckAccountPtrArray::~BoundCheckAccountPtrArray() { delete []arr; }
Account.h
// Account 클래스 깊은 복사 진행하는 대입 연산자 오버로딩 추가
#ifndef __ACCOUNT__H__
#define __ACCOUNT__H__
class Account
{
private:
int accID;
int balance;
char * cusName;
public:
Account(int _accID, int _balance, char * _cusName);
Account(const Account ©);
Account& operator= (const Account& ref);
int Get_accID() const;
void ShowAccInfo() const;
virtual void DepositMoney(int amount);
int WithDrawMoney(int amount);
~Account();
};
#endif // __ACCOUNT__H__
Account.cpp
// Account 클래스 깊은 복사 진행하는 대입 연산자 오버로딩 추가
#include "BankingCommon.h"
#include "Account.h"
Account::Account(int _accID, int _balance, char * _cusName)
: accID(_accID), balance(_balance)
{
cusName = new char[strlen(_cusName)+1];
strcpy(cusName, _cusName);
}
// 깊은 복사 진행하는 복사 생성자
Account::Account(const Account ©) : accID(copy.accID), balance(copy.balance)
{
// 새롭게 동적 할당해서 깊은 복사 진행
cusName = new char[strlen(copy.cusName) + 1];
strcpy(cusName, copy.cusName);
}
// 깊은 복사 진행하는 대입 연산자 오버로딩
Account& Account::operator= (const Account& ref)
{
accID = ref.accID;
balance = ref.balance;
delete []cusName;
cusName = new char[strlen(ref.cusName) + 1];
strcpy(cusName, ref.cusName);
return *this;
}
int Account::Get_accID() const { return accID; }
void Account::ShowAccInfo() const
{
cout << "계좌ID: " << accID << endl;
cout << "이 름: " << cusName << endl;
cout << "잔 액: " << balance << endl << endl;
}
void Account::DepositMoney(int amount) // virtual
{
balance += amount;
}
int Account::WithDrawMoney(int amount)
{
if(balance < amount)
{
return -1;
}
balance -= amount;
return balance;
}
Account::~Account()
{
delete cusName;
}
AccountHandler.h
/*
* BoundCheckAccountPtrArray 적용
*/
#ifndef __ACCOUNT_HANDLER__
#define __ACCOUNT_HANDLER__
#include "Account.h"
#include "AccountArray.h"
class AccountHandler
{
private:
// 기존의 Account* 형에서 변경
BoundCheckAccountPtrArray accounts;
int accNum;
public:
AccountHandler();
int PrintMenu();
void MakeAccount();
void MakeNormalAccount();
void MakeHighCreditAccount();
void Deposit();
void WithDraw();
void PrintAllAccountsInfo();
~AccountHandler();
};
#endif // __ACCOUNT_HANDLER__
'윤성우의 열헐 C++' 카테고리의 다른 글
윤성우의 열혈 c++) Chapter 13. 템플릿 (Template) 1 (0) | 2022.03.24 |
---|---|
윤성우의 열혈 c++) OOP 단계별 프로젝트 09단계 (0) | 2022.03.24 |
윤성우의 열혈 c++) Chapter 12. String 클래스의 디자인 (0) | 2022.03.23 |
윤성우의 열혈 c++) Chapter 11. 연산자 오버로딩2 (2) (0) | 2022.03.22 |
윤성우의 열혈 c++) Chapter 11. 연산자 오버로딩2 (0) | 2022.03.22 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- db
- back tracking
- BFS
- Kruskal
- DP
- two pointer
- dfs
- permutation
- C++
- MVC
- Implementation
- graph
- priority queue
- binary search
- 이분탐색
- 조합
- Spring
- greedy
- Unity
- 자료구조
- C
- Dijkstra
- floyd warshall
- Python
- Stack
- CSS
- Brute Force
- 재귀
- Tree
- recursion
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함