[C++] 윤성우 열혈 - OOP 단계별 프로젝트 08단계 (상속과 다향성)

Kim Dongil·2022년 12월 14일
0

C++

목록 보기
11/23

버전 0.7 에서는 파일 분할만 했으므로 생략.
버전 0.7 에서 만들었던 은행계좌 관리 프로그램을 버전 0.8 로 업그레이드 시켜보자.

프로젝트 08단계의 도입

Account 클래스는 깊은 복사를 진행하도록 복사 생성자가 정의되어 있다. 따라서 대입 연산자도 깊은 복사가 진행되도록 정의하는 것이 좋다.
그리고 AccountHandler 클래스에는 배열이 멤버로 선언되어서 객체의 저장을 주도 하는데, 이를 이번 Chapter에서 정의한 BoundCheckPointPtrArray 배열 클래스로 대체하고자 한다.
물론, 이를 위해서는 약간의 수정이 필요하며, 클래스의 이름도 적당히 변경할 필요가 있다.

프로그램 설명

실제 변경이 발생하는 헤더파일과 소스파일은 다음과 같다.

  • Account.h, Account.cpp -> 대입 연산자의 정의
  • AccountHandler.h -> BoundCheckPointPtrArray 클래스의 적용

따라서 이들 파일에 대한 버전정보를 갱신하기 바란다. 또한 배열 클래스의 추가를 위해서 다음의 소스파일과 헤더파일을 추가하기로 하자.

  • AccountArray.h, AccountArray.cpp -> 배열 클래스의 선언과 적용

혹, 위에서 언급한 파일과 BangkingCommonDecl.h 를 제외한 다른 파일에서 수정이 발생했다면, 무엇인가 잘못되었거나 불필요한 수정이 가해졌을 수도 있으니, 다시 한번 확인하기 바란다.

Account.h

#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__

class Account {
	int accID;
	int balance;
	char* cusName;

public:
	Account(int id, int money, char* name);
	Account(const Account& ref);
	Account& operator=(const Account& ref);  ////////////// 추가

	int GetAccID() const;
	virtual void Deposit(int money);
	int Withdraw(int money);
	void ShowAccInfo() const;
	~Account();
};
#endif

Account.cpp

#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);
}
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::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 << "계좌ID: " << accID << endl;
	cout << "이 름: " << cusName << endl;
	cout << "잔 액: " << balance << endl;
}

Account::~Account() {
	delete[]cusName;
}

AccountHandler.h

#ifndef __ACCOUN_HANDLER_H__
#define __ACCOUN_HANDLER_H__

#include "Account.h"
#include "AccountArray.h" /////// 추가

class AccountHandler {
	BoundCheckAccountPtrArray accArr;  /////// 변경 
	int accNum;

public:
	AccountHandler();
	void ShowMenu() const;
	void MakeAccount();
	void DepositMoney();
	void WithdrawMoney();
	void ShowAllAccInfo() const;
	~AccountHandler();

protected:
	void MakeNormalAccount();
	void MakeCreditAccount();
};
#endif

AccountArray.h (추가)

#ifndef __ACCOUNT_ARRAY_H__
#define __ACCOUNT_ARRAY_H__
#include "Account.h"

typedef Account* Account_Ptr;

class BoundCheckAccountPtrArray {
	Account_Ptr* arr;
	int arrlen;

	BoundCheckAccountPtrArray(const BoundCheckAccountPtrArray& arr) {}
	BoundCheckAccountPtrArray& operator=(const BoundCheckAccountPtrArray& arr) {}

public:
	BoundCheckAccountPtrArray(int len = 100);
	Account_Ptr& operator[] (int idx);
	Account_Ptr& operator[] (int idx) const;
	int GetArrlen() const;
	~BoundCheckAccountPtrArray();
};
#endif

AccountArray.cpp (추가)

#include "AccountArray.h"
#include "BankingCommonDecl.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 index out of bound exception" << endl;
		exit(1);
	}
	return arr[idx];
}
Account_Ptr& BoundCheckAccountPtrArray::operator[] (int idx) const {
	if (idx < 0 || idx >= arrlen) {
		cout << "Array index out of bound exception" << endl;
		exit(1);
	}
	return arr[idx];
}
int BoundCheckAccountPtrArray::GetArrlen() const {
	return arrlen; 
}
BoundCheckAccountPtrArray::~BoundCheckAccountPtrArray() {
	delete[]arr;
}

아래 5개 파일 변경 x
AccountHandler.cpp
BankingCommonDecl.h
BankingSystemMain.cpp
HighCreditAccount.h
NormalAccount.h

  1. AccountHandler 8행
    Account* accArr[100]; - > BoundCheckAccountPtrArray accArr; 으로 변경을 안했다. (배열 클래스를 만들기만하고 Handler 클래스에 적용을 안시킴)

  2. AccountArray.cpp 4행
    BoundCheckAccountPtrArray::BoundCheckAccountPtrArray(int len) : arrlen(len) {
    arr = new Account_Ptr[len];
    }
    위에 코드 처럼 int len 이라고 써야하는데 int len = 100 이라고 써서 오류 발생
    AccountArray.h 15 행 에서 int len = 100 이라고 선언해둬서 cpp 파일에서도 똑같이 int len = 100 이라고 써야 하는줄 알았는데 쓰면 오류가 난다.
    선언 int len = 100
    정의 int len

선언과 정의를 분리하여 작성하였고, 선언과 정의에 모두 디폴트 매개변수를 쓰면 오류코드 C2572 에러 발생
기본 매개 변수는 다시 정의할 수 없습니다. 매개 변수에 다른 값을 지정 해야 하는 경우에는 기본 매개 변수를 정의 하지 않은 상태로 두어야 합니다.

컴파일러 오류 C2572

0개의 댓글

관련 채용 정보