단계별 프로젝트를 진행하기에 앞서 하나의 파일을 여러 개로 나눠서 내용을 정리할 필요가 있습니다. 하나의 파일 안에 모든 것이 다들어있어서 내용파악이 쉽지 않기 때문입니다. 각각의 클래스마다 선언은 헤더파일에 정의는 시플플파일에 저장하는 것이 좋습니다. 이렇게 하면 프로그램을 관리하기도 좋고, 또 클래스의 구성이 한눈에 들어오기 때문에 프로그램의 내용을 파악하기도 수월해지기 때문입니다. 짧은 프로그램이라 할지라도 하나의 파일에 필요한 모든 요소를 담는 것은 바람직하지 못합니다.
NormalAccount 클래스와 HighCreditAccount 클래스는 크기가 작은 관계로 멤버함수의 정의를 별도의 파일에 분리하지 않고 헤더파일에 모두 삽입하는 형태로 파일을 분합합니다.
#pragma warning(disable:4996) #include "BankingCommonDecl.h" #include "AccountHandler.h" int main(void) { AccountHandler manager; int choice; while (1) { manager.ShowMenu(); cout << "select: "; cin >> choice; cout << endl; switch (choice) { case MAKE: manager.MakeAccount(); break; case DEPOSIT: manager.DepositMoney(); break; case WITHDRAW: manager.WithdrawMoney(); break; case INQUIRE: manager.ShowAllAccInfo(); break; case EXIT: return 0; default: cout << "Illegal selection." << endl; } } return 0; } |
#pragma once #ifndef __BANKING_COMMON_H__ #define __BANKING_COMMON_H__ #include <iostream> #include <cstring> using namespace std; const int NAME_LEN = 20; enum {MAKE=1, DEPOSIT, WITHDRAW, INQUIRE, EXIT}; enum {LEVEL_A=7, LEVEL_B=4, LEVEL_C=2}; enum {NORMAL=1, CREDIT=2}; #endif; |
#ifndef __ACCOUNT_HANDLER_H__ #define __AACOUNT_HANDLER_H__ #include "Account.h" class AccountHandler { private: Account * accArr[100]; int accNum; public: AccountHandler(); void ShowMenu(void) const; void MakeAccount(void); void DepositMoney(void); void WithdrawMoney(void); void ShowAllAccInfo(void) const; ~AccountHandler(); protected: void MakeNormalAccount(void); void MakeCreditAccount(void); }; #endif
|
#ifndef __ACCOUNT_H__ #define __ACCOUNT_H__ class Account { private: int accID; int balance; char * cusName; public: Account(int ID, int money, char * name); Account(const Account & ref); int GetAccID() const; virtual void Deposit(int money); int Withdraw(int money); void ShowAccInfo() const; ~Account(); }; #endif |
#ifndef __HIGHCREDIT_ACCOUNT_H__ #define __HIGHREDIT_ACCOUNT_H__ #include "NormalAccount.h" class HighCreditAccount : public NormalAccount { private: int specialRate; public: HighCreditAccount(int ID, int money, char * name, int rate, int special) :NormalAccount(ID, money, name, rate), specialRate(special) { } virtual void Deposit(int money) { NormalAccount::Deposit(money); Account::Deposit(money*(specialRate / 100.0)); } }; #endif
|
#ifndef __NORMAL_ACCOUNT_H__ #define __NORMAL_ACCOUNT_H__ #include "Account.h" class NormalAccount : public Account { private: int interRate; public: NormalAccount(int ID, int money, char * name, int rate) :Account(ID, money, name), interRate(rate) { } virtual void Deposit(int money) { Account::Deposit(money); Account::Deposit(money*(interRate / 100.0)); } }; #endif
|
#pragma warning(disable:4996) #include "BankingCommonDecl.h" #include "Account.h" Account::Account(int ID, int money, char * name) :accID(ID), balance(money) { cusName = new char[strlen(name) + 1]; strcpy(cusName, name); } Account::Account(const Account & ref) : accID(ref.accID), balance(ref.balance) { cusName = new char[strlen(ref.cusName) + 1]; strcpy(cusName, ref.cusName); } int Account::GetAccID() const { return accID; } void Account::Deposit(int money) { balance += money; } int Account::Withdraw(int money) { if (balance < money) return 0; balance -= money; return money; } void Account::ShowAccInfo() const { cout << "Acc ID: " << accID << endl; cout << "name: " << cusName << endl; cout << "balance: " << balance << endl; } Account::~Account() { delete[]cusName; } |
#pragma warning(disable:4996) #include "BankingCommonDecl.h" #include "AccountHandler.h" #include "Account.h" #include "NormalAccount.h" #include "HighCreditAccount.h" void AccountHandler::ShowMenu(void) const { cout << "menu" << endl; cout << "1. Acc making" << endl; cout << "2. Input" << endl; cout << "3. Output" << endl; cout << "4. Acc info All print" << endl; cout << "5. Program ending" << endl; } void AccountHandler::MakeAccount(void) { int sel; cout << "select" << endl; cout << "1. normal acc"; cout << "2. trust acc" << endl; cout << "select: "; cin >> sel; if (sel == NORMAL) MakeNormalAccount(); else MakeCreditAccount(); } void AccountHandler::MakeNormalAccount(void) { int id; char name[NAME_LEN]; int balance; int interRate; cout << "Making" << endl; cout << "acc ID "; cin >> id; cout << "name: "; cin >> name; cout << "balance: "; cin >> balance; cout << "interRate: "; cin >> interRate; cout << endl; accArr[accNum++] = new NormalAccount(id, balance, name, interRate); } void AccountHandler::MakeCreditAccount(void) { int id; char name[NAME_LEN]; int balance; int interRate; int creditLevel; cout << "making" << endl; cout << "acc ID: "; cin >> id; cout << "name: "; cin >> name; cout << "balance: "; cin >> balance; cout << "interRate: "; cin >> interRate; cout << "level(1toA, 2toB, 3toC): "; cin >> creditLevel; cout << endl; switch (creditLevel) { case 1: accArr[accNum++] = new HighCreditAccount(id, balance, name, interRate, LEVEL_A); break; case 2: accArr[accNum++] = new HighCreditAccount(id, balance, name, interRate, LEVEL_B); break; case 3: accArr[accNum++] = new HighCreditAccount(id, balance, name, interRate, LEVEL_C); } } void AccountHandler::DepositMoney(void) { int money; int id; cout << "input" << endl; cout << "acc ID: "; cin >> id; cout << "acc moeny: "; cin >> money; for (int i = 0; i < accNum; i++) { if (accArr[i]->GetAccID() == id) { accArr[i]->Deposit(money); cout << "Input complete" << endl; return; } } cout << "invalid ID." << endl << endl; } void AccountHandler::WithdrawMoney(void) { int money; int id; cout << "Output" << endl; cout << "accID"; cin >> id; cout << "output money"; cin >> money; for (int i = 0; i < accNum; i++) { if (accArr[i]->GetAccID() == id) { if (accArr[i]->Withdraw(money) == 0) { cout << "short" << endl << endl; return; } cout << "complete" << endl << endl; return; } } } AccountHandler::AccountHandler() : accNum(0) { } void AccountHandler::ShowAllAccInfo(void) const { for (int i = 0; i < accNum; i++) { accArr[i]->ShowAccInfo(); cout << endl; } } AccountHandler::~AccountHandler() { for (int i = 0; i < accNum; i++) delete accArr[i]; } |