Notice
Recent Posts
Recent Comments
Link
«   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
Archives
Today
Total
관리 메뉴

Dev fox

열혈 C++ 프로그래밍 OOP 프로젝트 08 본문

C++

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

나른한여우 2024. 4. 3. 17:19

08에서는 Account에 대한 대입연산자추가와 기존 Account형으로 생성한 배열을

BoundCheckPointPtrArray 배열 클래스를 만들어 대체하고자 함

 

Account.h, Account.cpp에 대입연산자를 추가 변경하고

AccountHandler.h에는 Array * userList[100] 에서 BoundCheckPointPtrArray userList로 변경

추가로 배열클래스를 정의할 AccountArray.h, AccountArray.cpp 파일 추가

 

1. 대입 연산자가 추가된 Account.h

#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__

class Account
{
private:
	int accId;
	int balance;
	char* cusName;
public:
	Account(const int id, int money, const char* name);
	Account(const Account& ac);
	int GetAccId() const;
	virtual void Deposit(int money);
	int WithDraw(int money);
	void ShowAccount() const;
	//08 Account 대입연산자 추가
	Account& operator=(const Account& ac);
	~Account();
};
#endif

 

2. 대입 연산자가 추가된 Account.cpp

#include "Account.h"
#include "BankingCommonDec1.h"

Account::Account(const int id, int money, const char* name)
	:accId(id), balance(money)
{
	cusName = new char[strlen(name) + 1];
	strcpy_s(cusName, strlen(name) + 1, name);
}

Account::Account(const Account& ac)
	:accId(ac.accId), balance(ac.balance)
{
	cusName = new char[strlen(ac.cusName) + 1];
	strcpy_s(cusName, strlen(ac.cusName) + 1, ac.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::ShowAccount() const
{
	cout << "계좌번호: " << accId << endl;
	cout << "고객이름: " << cusName << endl;
	cout << "잔 액: " << balance << endl << endl;
}
//08 Account 대입연산자 추가
Account& Account::operator=(const Account& ac)
{
	accId = ac.accId;
	balance = ac.balance;
	delete[]cusName;
	cusName = new char[strlen(ac.cusName) + 1];
	strcpy_s(cusName, strlen(ac.cusName) + 1, ac.cusName);
	return *this;
}
Account::~Account()
{
	delete[]cusName;
}

 

3. 배열클래스를 정의한 AccountArray.h

#ifndef __ACCOUNT_ARRAY_H__
#define __ACCOUNT_ARRAY_H__

#include "Account.h"
typedef Account* ACCOUNT_PTR;

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

#endif

 

4. 배열 클래스를 추가한 AccountArray.cpp

#include "Account.h"
#include "AccountArray.h"
#include "BankingCommonDec1.h"

//생성자
BoundCheckPointPtrArray::BoundCheckPointPtrArray(int len) : arrlen(len)
{
	arr = new ACCOUNT_PTR[len];
}

//배열의 인덱스 범위 초과 방지
ACCOUNT_PTR& BoundCheckPointPtrArray::operator[](int idx)
{
	if (idx < 0 || idx >= arrlen)
	{
		cout << "Array index out of Bound exception" << endl;
		exit(1);
	}
	return arr[idx];
}

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

int BoundCheckPointPtrArray::GetArrLen() const
{
	return arrlen;
}

BoundCheckPointPtrArray::~BoundCheckPointPtrArray()
{
	delete[]arr;
}

 

5. 변경된 userList를 적용한 AccountHandler.h

#ifndef __ACCOUNTHENDLER_H__
#define __ACCOUNTHENDLER_H__

#include "Account.h"
#include "AccountArray.h"

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

 

기존 Account형 배열에서 배열클래스로 변경한 이유는

[ ] 연산자 오버로딩을 통해 배열접근의 안정성을 보장받을수있고

AccountArray.h에 정의한 복사 생성자와 대입 연산자를 빈 상태로

private에 정의해서 복사와 대입을 원척적으로 막을수있음

 

배열은 저장소의 일종이고, 저장소에 저장된 데이터는 '유일성'을 보장받아야하기때문

 

추가로 new, delete 연산자의 경우 오버로딩을 할수있지만 메모리 공간 할당에대한 부분을

어떻게 효율적으로 풀어나갈지는 차차 이해해나가기로 하자.

( &, 스마트 포인터도 역시 그렇다 )