GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Feb-2006, 16:12
GrassPuppet GrassPuppet is offline
New Member
 
Join Date: Feb 2006
Posts: 2
GrassPuppet is on a distinguished road

Help with class, member functions and consructor


Hello, I'm new here. I'm having trouble with an excersice program. I can't seem to implement a debit function that will deduct from the initial balance of an account, as well as a credit function.

It asks to create a class called Account. The class should include one data member of type int to represent the account balance. Your class should prode a construtor that recieves an initial balance and uses it to initialize the data member. It should also validate it that it's greater or equal to 0..

Okay, my problem is that it requres three member funcitons. 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. Member funciton getBalance should return the current balance. Create a program that creates two Account objects and tests the member functios of class Account.

This is my code so far:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

class Account
{
   public:

      Account( int amount )
      {
         setBalance( amount );
      }
      
      void setBalance( int amount )
      {
         if ( amount >= 0 )
            Balance = amount;
         
         if ( amount < 0 )
         {
            Balance = 0;
            
            cout << "Your current balance is invalid.\n" << endl;
         }
      }
      void credit()
      {
        Balance = Balance + 20;
      }

      void debit()
      {
         Balance = Balance - 36;
         
         if ( Balance >= 0 )
            Balance = Balance;
         
         if ( Balance < 0 )
         {
            cout << "Debit amount exceeded account balance." << endl;
         }
      }
      
      int getBalance()
      {
         return Balance;
      }

   private:
      int Balance;
};

int main()
{
   Account accountBalance1( 10 );
   Account accountBalance2( 30 );

   cout << "The new balance for account 1 is: " << accountBalance1.getBalance()
        << "\n\nThe new balance for account 2 is: " << accountBalance2.getBalance()
        << endl;
   
   getch();
   return 0;
}


I hope I'm the right track. Thanks
  #2  
Old 04-Feb-2006, 16:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with class, member functions and consructor


Quote:
Originally Posted by GrassPuppet
Hello, I'm new here. I'm having trouble with an excersice program. I can't seem to implement a debit function that will deduct from the initial balance of an account, as well as a credit function.
..

Okay, my problem is that it requres three member funcitons. 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.
.
.
.
I hope I'm the right track.

The code looks good, as far as it goes. Thank you for using code tags. (How many times have I said that for a first post? --- Not many.)


Just give the debit() and credit() functions parameters (arguments). Then calculate the new balance as the balance - the argument (for debit()) or balance + the argument (for credit()). Assuming integers (which you are using everywhere):

CPP / C++ / C Code:
      void credit(int amount)
      {
        // put a statement here to
        // calculate the new balance by adding amount
      }

Then, in main(), to credit your account number 1 with, say, 100:

CPP / C++ / C Code:
    accountBalance1.credit(100);

Regards,

Dave
  #3  
Old 04-Feb-2006, 17:34
GrassPuppet GrassPuppet is offline
New Member
 
Join Date: Feb 2006
Posts: 2
GrassPuppet is on a distinguished road

Re: Help with class, member functions and consructor


Thanks for the help. It worked.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.