이번 단계에는 멤버 함수를 상수화 시키는 방법을 설명하고 있습니다.
4단계의 프로그램에서 일부 함수를 상수화 (const) 시킬 수 있습니다.
Data를 읽어 오는 부분은 내부 변수를 변경시키지 않으므로 상수화가 가능합니다.
계좌번호, 이름, 잔액을 가져오는 함수가 이에 해당됩니다.
/*
MyBank v2.2
Coding by gurumaru
*/
#include <iostream>
using namespace::std;
const int MAX_ACCOUNT = 100;
typedef unsigned int UINT;
UINT unTotalAccount = 0;
class Account
{
char *m_strName;
UINT m_unAccountNumber;
UINT m_unBalance;
public:
Account(char *Name, UINT AccountNum, UINT Bal);
Account(const Account &acc);
~Account();
void Add(UINT unMoney);
void Min(UINT unMoney);
UINT GetBalance() const;
UINT GetAccountNumber() const;
char* GetName() const;
void ShowAllInfo();
};
Account::Account(char *Name, UINT AccountNum, UINT Bal)
{
m_strName = new char[strlen(Name)+1];
strcpy_s(m_strName, (strlen(Name)+1), Name);
m_unAccountNumber = AccountNum;
m_unBalance = Bal;
};
Account::Account(const Account &acc)
{
m_strName = new char[strlen(acc.m_strName)+1];
strcpy_s(m_strName, (strlen(acc.m_strName)+1), acc.m_strName);
m_unAccountNumber = acc.m_unAccountNumber;
m_unBalance = acc.m_unBalance;
};
Account::~Account()
{
if (m_strName != NULL)
{
delete [] m_strName;
m_strName = NULL;
}
};
void Account::Add(UINT unMoney)
{
m_unBalance += unMoney;
}
void Account::Min(UINT unMoney)
{
m_unBalance -= unMoney;
}
UINT Account::GetBalance() const
{
return m_unBalance;
}
UINT Account::GetAccountNumber() const
{
return m_unAccountNumber;
}
char* Account::GetName() const
{
return m_strName;
}
//Account pAccount[MAX_ACCOUNT];
Account *pAccount[MAX_ACCOUNT];
void HelpMenu();
void DepositToAccount();
void WithdrawFromAccount();
void ShowAllAcountRemain();
void MakeAccount();
int FindAccountNumber();
void HelpMenu()
{
cout<<"############### 메뉴를 고르세요 #############"<<endl;
cout<<"# (1) 신규 계좌 개설 #"<<endl;
cout<<"# (2) 입금 #"<<endl;
cout<<"# (3) 출금 #"<<endl;
cout<<"# (4) 전체 계좌 잔고 조회 #"<<endl;
cout<<"# (5) 종료 #"<<endl;
cout<<"#############################################"<<endl;
cout<<"@ 고객님의 선택은? : ";
}
void main()
{
int nInput;
do
{
HelpMenu();
cin >> nInput;
switch (nInput)
{
case 1:
MakeAccount();
break;
case 2:
DepositToAccount();
break;
case 3:
WithdrawFromAccount();
break;
case 4:
ShowAllAcountRemain();
break;
case 5:
break;
default:
cout<<"잘못된 입력입니다."<<endl<<endl;
break;
}
}while (nInput != 5);
}
int FindAccountNumber()
{
bool bFindAccount = false;
UINT unAccountNumber;
UINT unIndex = 0;
cout << "@ 계좌 번호를 입력하세요. ";
cin >> unAccountNumber;
for (UINT i = 0; i < unTotalAccount; i++)
{
if (unAccountNumber == pAccount[i]->GetAccountNumber())
{
bFindAccount = true;
return i;
}
}
if (!bFindAccount)
{
cout << "계좌번호 오류. 계좌번호를 확인해 주세요."<<endl <<endl;
return -1;
}
return 0;
}
void DepositToAccount()
{
UINT unMoney;
int nIndex;
nIndex = FindAccountNumber();
if (nIndex >= 0)
{
cout << "@ 얼마를 입금할까요? " ;
cin >> unMoney;
pAccount[nIndex]->Add(unMoney);
cout << "@ 입금 완료! 잔액:" << pAccount[nIndex]->GetBalance()<< endl << endl;
}
}
void WithdrawFromAccount()
{
int nIndex;
UINT unMoney;
nIndex = FindAccountNumber();
if (nIndex >= 0)
{
cout << "@ 얼마를 출금할까요? ";
cin >> unMoney;
if (pAccount[nIndex]->GetBalance() >= unMoney)
{
pAccount[nIndex]->Min(unMoney);
cout << "@ 출금 완료! 잔액 :"<< pAccount[nIndex]->GetBalance()<< endl<<endl;
}
else
cout << "@ 잔고 부족! 잔액 :"<< pAccount[nIndex]->GetBalance()<< endl<<endl;
}
}
void ShowAllAcountRemain()
{
for (UINT i = 0; i < unTotalAccount; i++)
{
cout<<"##########################"<<endl;
cout<<"계좌번호 : "<<pAccount[i]->GetAccountNumber()<<endl;
cout<<"소유주 : "<<pAccount[i]->GetName()<<endl;
cout<<"잔액 : "<<pAccount[i]->GetBalance()<<endl;
}
}
void MakeAccount()
{
UINT unAccountNumber;
UINT unBal;
char chName[80];
cout << "@ 신규 계좌 개설 "<<endl;
cout << "@ 계좌 번호 : ";
cin >> unAccountNumber;
cout << "@ 소유주 : ";
cin >> chName;
cout << "@ 금액 : ";
cin >> unBal;
cout << endl;
pAccount[unTotalAccount] = new Account(chName, unAccountNumber, unBal);
unTotalAccount++;
}