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!

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please read the sticky threads about using this forum, it'll make your time here more productive.

Mainly, be sure to put your code inside code tags. This will make it more readable.

When copying code from your text, be sure to copy it correctly. You have

void Account::credit( int amount )
{
     blance = blance = amount; // add amount to blance
}
[/code]
This misspells the variable "balance" and the statement has an incorrect operator.  It should read
[code]
void Account::credit( int amount )
{
     balance = balance + amount; // add amount to blance
}

Ok, on to your specific problem!
Using the model of the credit( ) function above, the main work will be to subtract the debit amount from the balance - which is a small change to the statement above.

But, before you can successfully subtract that, you must compare the amount to the balance to be sure the amount being subtracted is less than or equal to the balance. If it exceeds the balance, display a message and do nothing more. If it's OK to do the subtraction, do it.

All this adds about three lines to the function sample above.

Edited 3 Years Ago by happygeek: fixed formatting

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 balance

balance = balance - 500

int getBalance(); // return the account balance
int getDebit();
private:
int balance; // data member tha stores the balance
}; // 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 balance begins at 0

// if initialBalance is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0

if( initialBalance > 0 )
balance = initialBalance;

// if initialBlance is negative, print error message

if( initialBalance < 0 )
cout<< "Error: Initial blance cannot be negative.\n << endl;
} // end Account constructor

// credit(add) an amount to the account balance
void Account::credit( int amount )
{
balance = balance = amount; // add amount to balance
}

/* 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::getBalance()
{
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

This article has been dead for over six months. Start a new discussion instead.