결국, 제 맘대로 해버렸다는... <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; }
'Studies > C++ Espresso' 카테고리의 다른 글
Chapter 8 LAB solution (4) | 2011.12.11 |
---|---|
Programming 08-07 (p.338) 클래스 설계 (0) | 2011.12.06 |
[Review] Chapter 01. p.14 ~ 15 Name Space 와 변수 (0) | 2011.09.06 |
[Review] Chapter 01. p.06 - 객체지향방법 (Object-oriented approach)의 목적 (0) | 2011.09.06 |
뭐부터 할까? (4) | 2011.09.06 |