Hi, I'm a first year college CS major. We've started OOP and I've just been asked to create a class for a bank account. I'm pretty solid at the idea of a class, but the actual format of using the member functions inside main have got my head spinning. I need a little help with this problem:
Write a C++ program in which you define and test the
class BankAccount. This class shall have as data members:
balance (double), an account number (string) and account
type (1-savings, 2-checking). The class shall allow the
following actions (member functions): debit (reduces the
account’s balance by the provided positive amount), credit
(increases the account’s balance by the provided positive
amount) and printAccountInfo (prints account number, type
and balance on the screen). The class BankAccount shall
also define the following constructors:
Default constructor which sets the initial balance to
zero, the type of the account to checking and the account
number to "000001"
Copy Constructor, and
Overloaded constructor for initializing all the data
members of the class.

Here is my code so far, I think the actual definition of the class is correct, but I'm confused on how to implement it in main:

class BankAccount
{
private:
    double balance ;
    char accountNumber ;
    bool accountType ;
    double amountS ;
public:
    void defaultCTOR(){
                balance = 0 ;
        accountType = true ;
        accountNumber = 0000001;}
    void debit(double balance, BankAccount amount){
        balance = balance - amount.amountS ;}
    void credit(double balance,double amount){
        balance = balance + amount ;}
    void printAccountInfo(){
        cout << accountNumber << endl ;
        if (accountType == true)
        {
            cout << "Checking" <<endl ;
        }
        else
        {
            cout << "Savings" <<endl ;
        }
        cout << accountType << endl ;
        cout << balance << endl ;}

};
int main()
{
    double amountS ;

    /* declare and test instances of the class BankAccount */
    BankAccount amount ;
    amount.defaultCTOR();
    cout << "Enter the amount you wish to credit to your account: " ;
    cin >> amountS ;
    amount.debit(0,amount) ;

    amount.printAccountInfo();


    return 0;
}

Hi chapelhoffer and welcome to DaniWeb,

This question should probably go in the C++ forum, but now that we are here I will try to help you out.

Let's take for example your member function called debit. Inside your main function, if you want to debit a certain bank account you can go:

// debit the bank account called amount $123.45
debit( 123.45, amount );

Now what the program does when it runs is it gets to this line and it "jumps" to your debit function, executes the code in that function, then returns and continues from where it was before in main.

So why do we do that do you ask? Well in this example it's a little difficult to see. But the beauty of doing it this way is that imagine we had 100 bank accounts to debit different amounts in our program. Or 1000? It is much easier to just make the call to the debit function rather than just writing that code over and over. Imagine what would happen if one day you wanted to change the way in which you debit an account? You would have to change your program in many places, but doing it like this means that you only have to change it in one place. Much neater to say the least. ;)

Anyway, I hope this has helped, but feel free to ask any other questions if I haven't explained enough.

darkagn,
If I write this:
kev.debit(25, kev) ;

for a BankAccount kev, will this debit the account? And I still am having trouble calling the member functions with the notation like "kev.printAccountInfor() ;"

I'm not sure which parts of this mean what; that is what does the "kev." component do? and when using the arguments such as in the debit() function, which argument is the one being modified?

Ah ok, now I see your confusion...

If I have a variable called kev that is a type BankAccount and I write kev.debit(), the program looks in the class BankAccount for the debit() function to run. However, if I am already in the BankAccount class, I can just write debit() and the program knows to look in the current class for the function.

As for your arguments, as in debit(25, kev), either can be modified, that's up to how you write the debit method. In the example code you posted previously, the kev that you pass in as a parameter gets modified by the amount 25. This is because the kev variable relates directly to the BankAccount parameter called amount in the function as it is the second parameter passed in.

I'm not sure I'm explaining this too well, so let me give an example. Imagine I have a class called Foo and it has one function, void bar( int i ). In another class I can write the following:

class Hello
{
  int main()
  {
    Foo f = new Foo();
    f.bar( 5 );
  }
}

Now how does this code get executed? First the program creates an object of type Foo and calls it f. Then it performs the function bar() on the object f with the value 5 passed in as an argument. Where does it find the implementation of bar()? In the class Foo since f is of type Foo.

I hope this has helped, although it's not the best explanation I've ever given. Sorry but it's late here in Australia...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.