Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // In .cpp
- int main() {
- Account acc1{"Jane Green", 48.50};
- Account acc2{"John Blue", -29.30};
- cout << "Account 1: "<< acc1.getName() << "Balance is $" << acc1.getBalance() << endl;
- cout << "Account 2: "<< acc2.getName() << "Balance is $" << acc2.getBalance() << endl;
- cout << "Enter deposit amount for account 1: ";
- double depositAmount;
- cin >> depositAmount;
- cout << "adding " << depositAmount << " to account 1 balance" << endl;
- acc1.deposit(depositAmount);
- cout << "Account 1's current balance is: " << acc1.getBalance() << endl;
- cout << "Enter amount you would like to withdraw from Account 1: ";
- double withdrawAmount;
- cin >> withdrawAmount;
- acc1.withdrawl(withdrawAmount);
- cout << "\nAccount 1's new balance is: " << acc1.getBalance();
- }
- //In .h
- class Account {
- public:
- Account(std::string accountName, double initialBalance)
- : name{accountName} {
- if (initialBalance > 0) {
- balance = initialBalance;
- }
- }
- //Adding money
- void deposit(double depositAmount) {
- if (depositAmount > 0) {
- balance = balance + depositAmount;
- }
- }
- //Taking money out
- void withdrawl(double withdrawAmount) {
- if (withdrawAmount <= balance) {
- balance = balance - withdrawAmount;
- }
- else {
- std::cout << "Sorry, You don't have enough money";
- }
- }
- //Checking amount
- double getBalance() const {
- return balance;
- }
- //Setting name
- void setName(std::string accountName) {
- name = accountName;
- }
- //Asking private for name
- std::string getName() const {
- return name;
- }
- private:
- std::string name;
- double balance{0.0};
- };
Advertisement
Add Comment
Please, Sign In to add comment