kylehannon

ch3, P9

Jan 31st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1.  
  2. // In .cpp
  3. int main() {
  4.     Account acc1{"Jane Green", 48.50};
  5.     Account acc2{"John Blue", -29.30};
  6.    
  7.    
  8.     cout << "Account 1: "<< acc1.getName() << "Balance is $" << acc1.getBalance() << endl;
  9.     cout << "Account 2: "<< acc2.getName() << "Balance is $" << acc2.getBalance() << endl;
  10.     cout << "Enter deposit amount for account 1: ";
  11.     double depositAmount;
  12.     cin >> depositAmount;
  13.     cout << "adding " << depositAmount << " to account 1 balance" << endl;
  14.     acc1.deposit(depositAmount);
  15.     cout << "Account 1's current balance is: " << acc1.getBalance() << endl;
  16.    
  17.  
  18.     cout << "Enter amount you would like to withdraw from Account 1: ";
  19.     double withdrawAmount;
  20.     cin >> withdrawAmount;
  21.     acc1.withdrawl(withdrawAmount);
  22.     cout << "\nAccount 1's new balance is: " << acc1.getBalance();
  23.    
  24. }
  25.  
  26. //In .h
  27. class Account {
  28. public:
  29.     Account(std::string accountName, double initialBalance)
  30.         : name{accountName} {
  31.        
  32.            
  33.             if (initialBalance > 0) {
  34.                 balance = initialBalance;
  35.             }
  36.     }
  37.    
  38.     //Adding money
  39.     void deposit(double depositAmount) {
  40.         if (depositAmount > 0) {
  41.             balance = balance + depositAmount;
  42.         }
  43.     }
  44.    
  45.     //Taking money out
  46.     void withdrawl(double withdrawAmount) {
  47.         if (withdrawAmount <= balance) {
  48.             balance = balance - withdrawAmount;
  49.         }
  50.         else {
  51.             std::cout << "Sorry, You don't have enough money";
  52.         }
  53.     }
  54.    
  55.     //Checking amount
  56.     double getBalance() const {
  57.         return balance;
  58.     }
  59.    
  60.     //Setting name
  61.     void setName(std::string accountName) {
  62.         name = accountName;
  63.     }
  64.    
  65.     //Asking private for name
  66.     std::string getName() const {
  67.         return name;
  68.     }
  69.    
  70.    
  71. private:
  72.     std::string name;
  73.     double balance{0.0};
  74. };
Advertisement
Add Comment
Please, Sign In to add comment