"Account class" problem

This is a discussion on "Account class" problem within the C++ Programming forums, part of the General Programming Boards category; The problem: (Account Class) Create a class called Account that a bank might use to represent customers' bank accounts. Your ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    "Account class" problem

    The problem:

    (Account Class) 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. [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75)called floating-point valuesto represent dollar amounts.] 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.


    How can I do an operation in getBalance?
    Last edited by Salem; 07-27-2008 at 08:44 AM. Reason: Remove overuse of bold

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    You don't need to do any operation in function getBalance, all it says that you have return the current balance which is stored in the data member of the class

    Code:
    int getBalance()
    {
        return currentBalance;
    }
    So when you call getBalance() it would simply return the balance of the account.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    so whats the use of debit and credit? I thought that you should add credit from Account and debit should subtract? Where should I do those operations?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    The debit function withdraws cash from the account (subtraction)

    and function credit adds money to the current balance of the account. (addition)

    You should use those operations when you want to add or subtract money from the account

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Yeah I follow that, so is this right?:
    Code:
    	void credit(int valCredit)
    	{
    		cout<<valCredit + getAccntBlnce();
    	}
    	void debit(int valDebit)
    	{
    		cout<<valDebit + getAccntBlnce();
    	}

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    so waht should i change?

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Those functions just print a number. They don't change the balance of the Account.

    Read your instructions carefully. Everything you need to do is outlined in great detail.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Well, your method won't work, because the data member isn't changed at all, think what would happen if, after showing the amount added to the current balance, then you call the getBalance() function? The balance won't be updated. It would be the same as it was earlier.

    You should, instead

    Code:
    void credit(int balToAdd)
    {
        currentBalance = currentBalance + balToAdd; /* where currentBalance is the data member
         that stores the balance of the account and balToAdd is the amount that is to be added to
         the currentBalance */
    }

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Instead of asking what is right, try compiling and running your program with sample input and examining the output. Play, tinker, experiment. That's what programming is about. That's the *fun* part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21