12. Create an Account class that a bank might use to represent customers bank accounts. Include a data member to represent the account balance. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account. Member function get Balance should return the current balance.

C++ How to Program (8th Edition)

Chapter 3

Introduction to Classes, Objects and Strings

Exercise

Problem # 12: 

Create an Account class that a bank might use to represent customers 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 get Balance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.

#include<iostream>
#include <string>
using namespace std;

class Account
{
 public:
  Account( int);
  void credit(int );
  void debit(int );
  int getAccountBalance();
 private:
  int accountBalance;
};
Account::Account( int balance)
{
 if(balance>=0)
  accountBalance=balance;
 else
 {
  accountBalance=0;
  cout<<"Initial balance was invalid."<<endl;
 }
}
void Account::credit( int balance)
{
 accountBalance = accountBalance + balance;
}
void Account::debit( int balance)
{
 if(accountBalance>=balance)
 {
  accountBalance = accountBalance - balance;
 }
 else
 {
  cout<<"Debit amount exceeded account balance."<<endl;
 }
}
int Account::getAccountBalance()
{
 return accountBalance;
}
int main()
{
 Account Account1(89);
 Account Account2(-76);
 cout << "Account1's initial balance is: "<< Account1.getAccountBalance()<< "\nAccount2's initial balance is: "<< Account2.getAccountBalance() << endl;
 Account1.credit(200);
 Account2.credit(34);
 cout << "\nAccount1's balance is: "<< Account1.getAccountBalance()<< "\nAccount2's balance is: "<< Account2.getAccountBalance() << endl;
 Account1.debit(50);
 Account2.debit(34);
 cout << "\nAccount1's balance is: "<< Account1.getAccountBalance()<< "\nAccount2's balance is: "<< Account2.getAccountBalance() << endl;
}

Let me know in the comment section if you have any question.

Similar Posts:

Chapter 3

  1. Modify class GradeBook, Include a string data member for course instructor’s name, Provide a set function and a get function to change and retrieve it, Modify display Message to output the welcome message and course name.
  2. Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members a part number, a part description, a quantity of the item being purchased and a price per item. 
  3. Create a class called Employee that includes three pieces of information as data members a first name, a last name and a monthly salary. Provide a set and a get function for each data member. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again. 
  4. Create a class called Date that includes three pieces of information as data members a month, a day and a year. Provide a set and a get function for each data member. Provide a member function display Date that displays the month, day and year separated by forward slashes (/). 

 Chapter 2

 

Comments

Post a Comment