编程练习答案——第10章
1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10_1_ACCOUNT_H 5 6 #include <iostream> 7 #include <string> 8 9 10 class Account { 11 private: 12 std::string name_; 13 std::string id_; 14 double deposit_; 15 public: 16 Account(); 17 Account(std::string,std::string,double); 18 void show() const; 19 bool in(double); 20 bool out(double); 21 }; 22 23 24 #endif //LEARN_CPP_CHAPTER10_1_ACCOUNT_H 25 26 // chapter10_1_account.cpp 27 28 #include "chapter10_1_account.h" 29 30 Account::Account() { 31 name_ = "none"; 32 id_ = "none"; 33 deposit_ = 0; 34 } 35 36 Account::Account(std::string name, std::string id, double deposit) { 37 name_ = name; 38 id_ = id; 39 deposit_ = deposit; 40 } 41 42 void Account::show() const { 43 using namespace std; 44 cout.precision(16); 45 cout << "account info: " << endl 46 << " name: " << name_ << endl 47 << " id: " << id_ << endl 48 << " deposit: " << deposit_ << endl; 49 } 50 51 bool Account::in(double n) { 52 if (n <= 0) 53 return false; 54 else 55 deposit_ += n; 56 return true; 57 } 58 59 bool Account::out(double n) { 60 if (deposit_ < n) 61 return false; 62 else 63 deposit_ -= n; 64 return true; 65 } 66 67 // run 68 69 void ch10_1() { 70 Account a; 71 a.show(); 72 Account b{"kxgkhy","4523452345",1000.123}; 73 b.show(); 74 b.in(123.123); 75 b.show(); 76 b.out(123.123); 77 b.show(); 78 }