class

Write your question here.
Create a class called Account that a bank might use to represent customers' bank accounts. Your class should
include one data member of type int to represent the account balance. Your class should 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 is greater than or equal to 0. If not, the balance should be set to 0 and the
constructor should display an error message, indicating that the initial balance was invalid. The class should
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 should 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
should return the current balance. Create a program that creates two Account objects and tests the member
functions of class Account.
[code]
#include <iostream>
using std::cout;
using std::endl;

#include "Account.h" //include definition of Class Account

Account::Account(){} //default constructer

Account::Account(int balnce)
{
setBalance(balnce);
}// end constructer

void Account::setBalance(int initialBalance)
{
balance = initialBalance; // initial balance in account
}

void Account::accCredit(int amountd)
{

balance = balance + amountd;
}

void Account::accDebit(int amountw)
{
balance = balance - amountw;
}

int Account::getBalance()
{
return balance;
} // returns the balance

void Account::displayMsg()
{
cout << "Deposit:" << accCredit << endl;
cout << "Withdraw:" << accDebit << endl;
cout << "Current Balance:" << getBalance() << endl;
}

#include <iostream>
using std::cout;
using std::endl;

#include "Account.h"

//function main begins the program execution
int main()
{
Account account1(100);
Account account2(50);

/*cout << "Account1 balance:K" << account1.getBalance() << endl;
cout << "Account2 balance:K" << account2.getBalance() << endl;*/


const int arraySize = 2;
Account account[arraySize];
account[0] = account1;
account[1] = account2;

int counter = 0;
while(counter < arraySize){
account[counter].displayMsg();
account[counter].accCredit();
account[counter].accDebit();
counter++;

}
system("pause");
return 0; // indicate successful termination
} // end main

[]
displayMsg() looks a bit crazy, try moving the cout statements into their respective members [accDebit()/acCredit()] so that it logs the transaction when it happens.

here's an example main loop so you can get the idea...
1
2
3
4
5
for(int counter=0; counter<arraySize; counter++){
    account[counter].accCredit(counter*2);
    account[counter].accDebit(counter);
    cout << "Account" << counter << " balance:K" << account[counter].getBalance() << endl;
}

I was just doing this exercise in a book and I was also wondering if the exercise had asked for a user-input.
Topic archived. No new replies allowed.