본문 바로가기

Studies/C++ Espresso

9장 Programming 1. 은행계좌



※ 아 이건 문제가 좀 거지 같네요. Virtual 함수 쓰는 법 연습인 건 알겠지만, 문제 정의도 별로고... 실제 테스트 시나리오도 너무 부실하게 기술되어 있고... 좀더 문제가 이해하기 쉬웠으면 좋겠습니다. 

결국, 제 맘대로 해버렸다는... <iostream> 에서 cout으로 출력할 대, float 계산 결과의 소수점 맞추는 것도 마음에 안드네요. 이건 c의 stdio의 fprinf 같은 것 이용해야 깔끔합니다. 

● BankAcct.h
#ifndef __BACKACCT_H__
#define __BACKACCT_H__

#include <iostream>
#include <string>

class BankAcct
{
	protected:
		int money;

	public:
		BankAcct();
		~BankAcct();

		void deposit(int v_money);
		int withdraw(void);

		virtual float getInterest(void) = 0;
};
#endif




●BankAcct.cpp
#include "BankAcct.h"

BankAcct::BankAcct()
{
	/* do nothing */
}

BankAcct::~BankAcct()
{
	/* do nothing */
}

void BankAcct::deposit(int v_money)
{
	money += v_money;

	return;
}

int BankAcct::withdraw(void)
{
	float temp = money;

	money = 0;

	return temp;
}



●Accounts.h
#ifndef __ACCOUNTS_H__
#define __ACCOUNTS_H__

#include "BankAcct.h"

class SavingAcct:public BankAcct
{
	private:

	public:
		SavingAcct();
		~SavingAcct();
		virtual float getInterest(void);
};

class CheckingAcct:public BankAcct
{
	private:

	public:
		CheckingAcct();
		~CheckingAcct();
		float getInterest(void);
};
#endif



●Acounts.cpp
#include "Accounts.h"

SavingAcct::SavingAcct()
{
	money = 0;
}

SavingAcct::~SavingAcct()
{
	/* do nothing */
}

float SavingAcct::getInterest(void)
{
	return 0.09;
}

CheckingAcct::CheckingAcct()
{
	money = 0;
}

CheckingAcct::~CheckingAcct()
{
	/* do nothing */
}

float CheckingAcct::getInterest(void)
{
	/* do nothing */
	return 0.05;
}




●main.h
#include <iostream>
#include <iomanip>
#include <string>
#include "Accounts.h"

using namespace std;



●main.cpp
#include "main.h"

int main()
{
	float s_temp=0, c_temp = 0;

	/* Create acconts */
	SavingAcct S = SavingAcct();
	CheckingAcct C = CheckingAcct();

	/* Save some money */
	S.deposit(20000);
	C.deposit(15000);

	/* Calculate interest incomes */
	s_temp = S.withdraw() * S.getInterest();
	c_temp = C.withdraw() * C.getInterest();

	cout << "Saving Account Interest Incomes : expecting : " << setw(8) << setprecision(6) << s_temp << "\n";
	cout << "Checking Account Interest Incomes : expecting : " << setw(8) << setprecision(6) << c_temp << "\n";

	/* Save more money */
	S.deposit(s_temp + 20000);
	C.deposit(c_temp + 17000);

	/* Widthraw and Calculate total incomes */
	s_temp = S.withdraw() * (1 + S.getInterest());
	c_temp = C.withdraw() * (1 + C.getInterest());

	cout << "Saving Account Total Incomes : " << setw(8) << setprecision(6) << s_temp << "\n";
	cout << "Checking Account Total Incomes : " << setw(8) << setprecision(6) << c_temp << "\n";

	return 0;
}