티스토리 뷰

직접 만든 char * 를 대체하는 String 클래스의 도입. 

기존의 Account 클래스는 char * 형에 고객 정보를 저장했기 때문에 Account 클래스 내에서 동적할당을 해야하고,

동적할당을 하기 때문에 복사생성자, 대입연산자에서 깊은 복사를 해주고

소멸자에서 동적할당 한 메모리를 해제해줘야 했다.

고객 이름 정보를 String 클래스로 저장하면 이 모든것을 String 클래스 내부에서 해결하기 때문에 Account 클래스에서 복사생성자, 대입연산자, 소멸자를 지울수 있다.

 

String.h 


#ifndef __STRING_H__
#define __STRING_H__

#include "BankingCommon.h"

class String
{
private:
    char *str;
public:
    String();
    String(const char *_str);
    String(const String &ref);
    String& operator= (const String& ref);
    String operator+ (const String& ref);
    String& operator+= (const String& ref);
    bool operator== (const String& ref);
    ~String();

    friend ostream& operator<< (ostream& os, const String& ref);
    friend istream& operator>> (istream& is, String& ref);
};

#endif // __STRING_H__

 

String.cpp

#include "String.h"

String::String()
{
    str = NULL;
}
// 생성자
String::String(const char *_str)
{
    str = new char[strlen(_str) + 1];
    strcpy(str, _str);
}
// 복사 생성자
String::String(const String& ref)
{
    str = new char[strlen(ref.str)];
    strcpy(str, ref.str);
}
// = 연산자 오버로딩
String& String::operator= (const String& ref)
{
    if(str != NULL) delete []str;
    str = new char[strlen(ref.str)];
    strcpy(str, ref.str);
    return *this;
}

// + 연산자 오버로딩
String String::operator+ (const String& ref)
{
    char * tmp = new char[strlen(str) + strlen(ref.str) - 1];
    strcpy(tmp, str);
    strcat(tmp, ref.str);
    String tmpStr(tmp);
    delete []tmp;
    return tmpStr;
}

// += 연산자 오버로딩
String& String::operator+= (const String& ref)
{
    char * tmp = new char[strlen(str) + strlen(ref.str) - 1];
    strcpy(tmp, str);
    strcat(tmp, ref.str);
    if(str != NULL) delete []str;
    str = tmp;
    return *this;
}

// == 연산자 오버로딩
bool String::operator== (const String& ref)
{
    if(strlen(str) != strlen(ref.str)) return false;
    for(int i = 0; i < strlen(str); i++)
    {
        if(str[i] != ref.str[i]) return false;
    }
    return true;
}

String::~String()
{
    if(str != NULL)
        delete []str;

}

// 전역
// << 연산자 오버로딩
ostream& operator<< (ostream& os, const String& ref)
{
    os << ref.str;
    return os;
}

// >> 연산자 오버로딩
istream& operator>> (istream& is, String& ref)
{
    char str[100];
    is >> str;
    ref = String(str);
    return is;
}

 

Account.h 

// 고객이름 저장 방식을 char * 동적할당에서 String 클래스로 대체
// => String 클래스에서 정의되어 있으므로, Account 클래스에서의 소멸자, 복사 생성자, 대입 연산자 모두 제거

#ifndef __ACCOUNT__H__
#define __ACCOUNT__H__

#include "String.h"

class Account
{
private:
    int accID;
    int balance;
//    char * cusName;
    String cusName;

public:
    Account(int _accID, int _balance, String _cusName);

    int Get_accID() const;
    void ShowAccInfo() const;
    virtual void DepositMoney(int amount);
    int WithDrawMoney(int amount);
};


#endif // __ACCOUNT__H__

Account.cpp


#include "BankingCommon.h"
#include "Account.h"

Account::Account(int _accID, int _balance, String _cusName)
        : accID(_accID), balance(_balance)
{
    cusName = _cusName;
}

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;
}



NormalAccount.h

#ifndef __NORMAL_ACCOUNT_H__
#define  __NORMAL_ACCOUNT_H__

#include "Account.h"

class NormalAccount : public Account
{
private:
    int interest;
public:
    NormalAccount(int _accID, int _balance, String _cusName, int _interest)
            : Account(_accID, _balance, _cusName), interest(_interest) {}

    virtual void DepositMoney(int amount)
    {
        Account::DepositMoney(amount + (int)(amount * (interest/100.0)));
    }
};


#endif //  __NORMAL_ACCOUNT_H__

HighCreditAccount.h


#ifndef __HIGH_CREDIT_ACCOUNT_H__
#define __HIGH_CREDIT_ACCOUNT_H__

#include "NormalAccount.h"

class HighCreditAccount : public NormalAccount
{
private:
    int rating; // 신용등급
public:
    HighCreditAccount(int _accID, int _balance, String _cusName, int _interest, int _rating)
            : NormalAccount(_accID, _balance, _cusName, _interest), rating(_rating) {}

    virtual void DepositMoney(int amount)
    {
        int additional_interest;
        switch(this->rating)
        {
            case A_RATING:
                additional_interest = (int)(amount * (7/100.0)); break;
            case B_RATING:
                additional_interest = (int)(amount * (4/100.0)); break;
            case C_RATING:
                additional_interest = (int)(amount * (2/100.0)); break;
        }
        NormalAccount::DepositMoney(amount); // 원금 + 이자
        Account::DepositMoney(additional_interest);
    }
};


#endif // __HIGH_CREDIT_ACCOUNT_H__

AccountHandler.cpp

#include "AccountHandler.h"
#include "BankingCommon.h"
#include "NormalAccount.h"
#include "HighCreditAccount.h"

AccountHandler::AccountHandler() : accNum(0) {}

int AccountHandler::PrintMenu()
{
    cout << "----- MENU -----" << endl;
    cout << "1. 계좌개설" << endl;
    cout << "2. 입 금" << endl;
    cout << "3. 출 금" << endl;
    cout << "4. 계좌정보 전체 출력" << endl;
    cout << "5. 프로그램 종료" << endl;
    cout << "선택: ";
    int chose; cin >> chose;
    cout << endl;
    return chose;
}

void AccountHandler::MakeAccount()
{
    int choice;
    cout << "[계좌종류선택]" << endl;
    cout << "1.보통예금계좌 2.신용신뢰계좌" << endl;
    cout << "선택: "; cin >> choice;

    if(choice == NORMAL) MakeNormalAccount();
    else MakeHighCreditAccount();
}

void AccountHandler::MakeNormalAccount()
{
    int accID, balance, interest;
//    char cusName[NAME_LEN];
    String cusName;

    cout << "[보통예금계좌 개설]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "이 름: "; cin >> cusName;
    cout << "입금액: "; cin >> balance;
    cout << "이자율: "; cin >> interest;

    // 동적으로 객체 생성
    accounts[accNum++] = new NormalAccount(accID, balance, cusName, interest);
}

void AccountHandler::MakeHighCreditAccount()
{
    int accID, balance, interest, rating;
//    char cusName[NAME_LEN];
    String cusName;

    cout << "[신용신뢰계좌 개설]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "이 름: "; cin >> cusName;
    cout << "입금액: "; cin >> balance;
    cout << "이자율: "; cin >> interest;
    cout << "신용등급(1toA, 2toB, 3toC): "; cin >> rating;

    accounts[accNum++] = new HighCreditAccount(accID, balance, cusName, interest, rating);
}

void AccountHandler::Deposit()
{
    int accID, balance;
    cout << "[입   금]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "입금액: "; cin >> balance;
    cout << "입금완료" << endl;

    for(int i = 0; i < accNum; i++)
    {
        if(accounts[i]->Get_accID() == accID)
        {
            accounts[i]->DepositMoney(balance);
            return;
        }
    }
    cout << "존재하지 않는 계좌ID 입니다 " << endl;
}

void AccountHandler::WithDraw()
{
    int accID, balance;
    cout << "[출   금]" << endl;
    cout << "계좌ID: "; cin >> accID;
    cout << "출금액: "; cin >> balance;
    cout << "출금완료" << endl;

    for(int i = 0; i < accNum; i++)
    {
        if(accounts[i]->Get_accID() == accID)
        {
            if(accounts[i]->WithDrawMoney(balance) == -1)
                cout << "잔액이 부족합니다" << endl;
            else cout << "출금 완료" << endl;
            return;
        }
    }
    cout << "존재하지 않는 계좌ID 입니다 " << endl;
}

void AccountHandler::PrintAllAccountsInfo()
{
    for(int i = 0; i < accNum; i++)
    {
        accounts[i]->ShowAccInfo();
    }
}

AccountHandler::~AccountHandler()
{
    for(int i = 0; i < accNum; i++) delete accounts[i];
}




댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
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
글 보관함