학교생활!

[230325]자료구조 과제2: BankAccount

Daybreak21 2023. 3. 25. 03:35

자료구조 2주차 : 배열과 클래스 과제

배열과 클래스를 배우고 BankAccount클래스를 만들었다. 

 

※ 요구 사항

  • . 클래스 BackAccount 를 정의하라. 해당 클래스에는 멤버 변수로 은행명(bankName), 계좌주(owner), 계좌번호(address), 잔액(balance)이 있다.
  • 계좌주, 은행명, 계좌번호는 모든 곳에서 공개가 가능한 반면, 잔액은 해당 객체 외에 어느곳에서도 공개가 되지 않는. 그리고 다음과 같은 모든 곳에서 공개되는 멤버 함수를 가지고 있다.
  • 기본 생성자, 매개 변수를 받는 생성자, 및 소멸자

※구현할 코드  

  • void displayInfo(): 해당 객체의 은행명, 계좌주, 및 계좌번호를 프린트한다. 
  • void displayBalance(): 해당 객체의 계좌번호와 잔액을 프린트한다. 
  • void getMoney(BankAccount sender, int money): 해당 객체에서 인수로 받는 sender 에게 money 만큼의 돈을 송금 받는다. 즉 받은 만큼 해당 객체의 잔액이 증가한다. 송금 받은 후 sender 의 은행명, 계좌주, 및 계좌번호를 프린트한다. 
  • void sendMoney(BankAccount receiver, int money): 해당 객체에서 인수로 받는 receiver 에게 money 만큼의 돈을 보낸다. 송금 금액 외 1000 원의 수수료가 발생한다. 보낸 돈과 수수료만큼 해당 객체의 잔액에서 차감된다. 
  • void deposit(int money): 해당 객체에 돈을 입금한다. 수수료는 없다. 
  • void withdraw(int money): 해당 객체에서 돈을 출금한다. 수수료는 없다

제출 후 내가 추가한 점

  • 돈을 송금받는 getMoney함수는 돈을 송금하는 sendMoney함수에 의해서만 일어나는 일이기때문에, sendMoney함수를 이용하여 receiver에게 송금하면 send함수 안에 getMoney함수를 호출하여 자동으로 receiver의 잔액의 돈을 더하였다. 
  • 계좌를 생성할때 잔액을 초기화해준다.
  • 돈을 송금하거나 인출하기 전에 잔액이 충분한지 검사하는 코드 
  • 함수가 실행 될때 안내문 + 잔액 출력

 

완성된 화면

 


 

BankAccount.h 

#include <iostream>
#include <cstring>
using namespace std;

class BankAccount {
private:
	int balance;

public:
	char bankName[20];
	char owner[20];
	int address;

	BankAccount() { }
	~BankAccount() { }

	BankAccount(const char* n, const char* o, int a, int b)
		: address(a), balance(b) {
		strcpy(bankName, n), strcpy(owner, o);
	}

	void displayInfo() {
		cout << "은행명: " << bankName << endl;
		cout << "계좌주: " << owner << endl;
		cout << "계좌번호: " << address << endl;

	}
	void displayBalance() {
		cout << "계좌번호: " << address << endl;
		cout << "잔액: " << balance << endl << endl;
	}

	void sendMoney(BankAccount& receiver, int money) {
		if (balance < money) {
			cout << "잔액이 부족합니다." << endl;
			return;
		}
		balance -= (money+1000);

		cout << money << "원을 " << receiver.owner << "님에게 보냈습니다." << endl;
		displayBalance();

		receiver.getMoney(*this, money);
	}
	void getMoney(BankAccount sender, int money) {
		balance += money;
		cout << money << "원을 " << sender.owner << "님에게 받았습니다." << endl;
		displayBalance();
	}

	void deposit(int money) {
		balance += money;
		cout << money << "원을 입금했습니다." << endl;
	}
	void withdraw(int money) {
		if (balance < money) {
			cout << "잔액이 부족합니다." << endl;
			return;
		}
		balance -= money;
		cout << money << "원을 출금했습니다." << endl;
	}
};

"계좌주, 은행명, 계좌번호는 모든 곳에서 공개가 가능한 반면, 잔액은 해당 객체 외에 어느곳에서도 공개가 되지 않는다."
라는 조건이 있으니 balance는 private로,  bankName, owner, address는 public으로 멤버 접근 지정자를 설정해주었다. 

생성자에서 문자열을 파라미터로 받을때 const로 지정해주어야 컴파일된다. .. 

 

 

 

  • sendMoney()
void sendMoney(BankAccount& receiver, int money) {
		if (balance < money) {
			cout << "잔액이 부족합니다." << endl;
			return;
		}
		balance -= (money+1000);

		cout << money << "원을 " << receiver.owner << "님에게 보냈습니다." << endl;
		displayBalance();

		receiver.getMoney(*this, money);
	}
  • getMoney()
void getMoney(BankAccount sender, int money) {
		balance += money;
		cout << money << "원을 " << sender.owner << "님에게 받았습니다." << endl;
		displayBalance();
	}

매개변수로 받는사람의 레퍼런스와 보내는 돈을 받고, receiver.getMoney를 이용해서 받는사람의 객체에서 getMoney함수를 호출한다.  
이때 getMoney의 매개변수를 *this로 해주어 getMoney에서 보내는 사람의 클래스의 객체에 접근하여 sender.owner라는 이름을 사용할 수 있게해주었다.  

 

void getMoney(BankAccount sender, int money) {
		balance += money;
		cout << money << "원을 " << sender.owner << "님에게 받았습니다." << endl;
		displayBalance();
	}

 

                        

  • main함수
더보기

클래스를 확인하기 위한 메인함수

#pragma warning(disable : 4996)
#include <iostream>
#include "Bank.h"


int main() {
	BankAccount owner1("pretty", "minjeong", 101, 2000);
	BankAccount owner2("pretty", "jimin", 411, 2000);

	owner1.displayInfo();
	owner2.displayInfo();

	owner1.deposit(10000);
	owner1.withdraw(1000);
	owner1.displayBalance();

	owner2.withdraw(1000);
	owner2.deposit(20000);
	owner2.displayBalance();

	owner1.sendMoney(owner2, 10000);
	owner1.displayBalance();
	owner2.displayBalance();

	owner1.withdraw(1000);


	return 0;
}

 

 

배운 점, 느낀 점

처음 sendMoney함수를 작성했을 때, sendMoney에 의해 호출된 void displayBalance()로만 제대로된 잔액이 나타나고, 함수를 벗어나면 잔액이 송금 전으로 돌아가는 문제가 있었다. 

문제의 원인은 클래스 자체를 매개변수로 넘기게 해준것이였다. (복사연산자를 배우기전...)

 void sendMoney(BankAccount receiver, int money) { }

c++에서 함수에 매개변수로 클래스를 넘기면 복사연산자를 호출한다. main에서 receiver로 owner2를 넘기는 동시에, 함수 내부의 receiver는 main의 owner2와는 서로 다르다.  

그래서 sendMoney함수를 사용하여 돈을 전달하고 함수를 종료하면 돈을 받는사람의 계좌에 잔액이 증가하지 않았던것이였다. 이를 매개변수에 receiver의 레퍼런스를 넘겨주었다. 

매개변수를 레퍼런스로 넘겨주면 main의 owner를 참조하여 문제가 해결된다. 

 void sendMoney(BankAccount& receiver, int money) { }