I have an assignment due tomorrow and I haven't being able to do it. I do not understand nothing about classes and when to use them. My assignment is:
(Account Class) Modify class Account (Fig L 3.1 and Fig L 3.2) to provide a member function called debit that withdraws money from an Account. 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." Modifiy class AccountTest (fig L 3.3) to rest member function debit.
I started to do the header file but I really can't and don't understand.
The header file code is this one (Fig L 3.1):
// T3.6:: Account.h
// Definition of Account c
class Account
{
public:
Account(int); // contructor initializes balance
void credit(int); // add an amount to the account balance
Debit(int);
void debit(int);
MISSING 1 /* write code to declare member function debit. */
.. // subtract an amount from the account balnce
balance = balance - 500
int getBalance(); // return the account balance
int getDebit();
private:
int balance; // data member tha stores the blance
}; // end class account
The second file is (Fig. L 3.2):
// T3.6: Account.cpp
// Member-function definition for class Account.
#include <iostream>
using std::cout;
using std::endl;
#include "Account.h"
// Account constructor initializes data member balance
Account::Account(int initialBalance)
{
balance=0; // assume that the blance ebgins at 0
// if initialBalance is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0
if( initialBlance > 0 )
balance = initialBlance;
// if initialBlance is negative, print error message
if( initialBlance < 0 )
cout<< "Error: Initial blance cannot be negative.\n << endl;
} // end Account constructor
// credit(add) an amount to the account balnce
void Account::credit( int amount )
{
blance = blance = amount; // add amount to blance
}
/* write code to define member function debit. You have to write the dunction header and two conditions. May take up to 7 lines. */
MISSING 2
// debit (subtract) an amount from the account balance
...
...
...// debit amount exceeds balance
...
... //debit amount does not exceed balance
...
... //end function debit
...// return the account balance
int Account::getBlance()
{
return balance; // gives the value of balance to the calling function
}// end function getBalance
and the last one is (fig L 3.2):
// T3.6:: AccountTest.cpp
// Create and manipulate Account objects.
#include <iostream>
using std::cout;
using std:: cin;
using std:: endl;
// include definition of class Account from Account.h
#include "Account.h"
// function main begins program execution
int main()
{
Account account1( 50 ); // create Account object
Account account2( 25 ); // create Account object
// display initial balance of each object
cout << "account1 balance: $" << account1.getBalance()<<endl;
cout << "account2 balance: $" << account2.getBalance()<<endl;
int withdrawalAmount; // stores withdrawal amount read from user
cout << "\nEnter withdrawal amount for account1:"; // prompt
cin >> withdrawalAmount; // obtain user input
cout<< "\nattempting to subtract " << withdrawalAmount << " from account1 balance\n\n";
/* write code to withdraw money from account1 using function debit */
MISSING3
.... // try to subtract from account 1
// display balance
cout << "account1 balance: $" << account1.getBalance()<<endl;
cout << "account2 balance: $" << account2.getBalance()<<endl;
cout << "\nEnter withdrawal amount for account2:"; // prompt
cin >> withdrawalAmount; // obtain user input
cout<< "\nattempting to subtract " << withdrawalAmount << " from account2 balance\n\n";
/* write code to withdraw money from account2 using function debit */
MISSING 4
//try to subtract from account2
// display balances
cout << "account1 balance: $" << account1.getBalance()<<endl;
cout << "account2 balance: $" << account2.getBalance()<<endl;
return 0; // indicate successful termination
} // end main
please HELP ME!