This program is in C++. Please include comments in the code. Thanks Exercise 16.
ID: 3622223 • Letter: T
Question
This program is in C++. Please include comments in the code. ThanksExercise 16.12 page 599
(Account Class) Create an Account class that a bank might use to represent customer’s bank accounts. Include a data member of type int to represent the account balance. Provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the function should print a message indicating “Debit amount exceeded account balance.” Member function getBalance shoud return the current balance.
Create a program that creates two Account objects and tests the member functions of class Account.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class Account{private:
int balance;
public:
Account(int val)
{if(val<0)
{balance=0;
cout<<"initial balance <0, set to 0 ";
}
else
balance=val;
}
void credit(int val)
{balance+=val;
}
void debit(int val)
{if(val>balance)
cout<<"Debit amount exceeded account balance. ";
else
balance-=val;
}
int getBalance()
{return balance;
}
};
int main()
{Account a(50),b(-5);
cout<<"for account a ";
cout<<"initial balance account a = "<<a.getBalance()<<endl;
a.credit(25);
cout<<"after 25 credit: balance account a = "<<a.getBalance()<<endl;
a.debit(12);
cout<<"after 15 debit: balance account a = "<<a.getBalance()<<endl;
cout<<" for account b ";
cout<<"initial balance account b = "<<b.getBalance()<<endl;
b.credit(25);
cout<<"after 25 credit: balance account b = "<<b.getBalance()<<endl;
b.debit(35);
cout<<"after 35 debit: balance account b = "<<b.getBalance()<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.