This skin was created by Rocky of the IF Skin Zone

Enjoy forums? Start your own community for free.
InvisionFree - Free Forum Hosting
Welcome to C++ Game Dev. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Name:   Password:


 

 Bank Account Simulator
Stenny
Posted: May 27 2006, 01:46 PM


C++ Composer


Group: Members
Posts: 475
Member No.: 49
Joined: 17-January 06



Hello there reader,

After reading some books (C++ for dummies, which is quite a good book! laugh.gif ) I now know about classes, pointer, and functions. After I read about those I thought it was a good idea to first create a program with them and become familiar with them.
So I came up with a banksaving simulator. You could deposit, withdraw money and check you're balance. After all, there are a lot here who posted a program...now it's my turn tongue.gif

There might be easier way to do these things, but remember I had to use pointers, classes and functions.

Please give critics, here it is:

CODE
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class saving
{
public:
int accountNumber;
float balance;
};

void deposit(int amount, saving* pAccount)
{
pAccount->balance += amount;
}

void withdraw(int amount, saving* pAccount)
{
pAccount->balance -= amount;
}

float getBalance(saving* pAccount)
{
return pAccount->balance;
}

int main(int nNumberofArgs, char* pszArgs[])
{
saving account;
bool correct = false;
int option;
float amount;

while (correct == false)
{
 system("CLS");
 cout << "Welcome to you're very own banksaving program.\n"
  << "Here you can simulate you're own bank account\n"
  << "and in that check up you're balance. (yes I know,\n"
  << "you can also use a calculator)"
  << endl
  << "What is you're bankaccountnumber: ";
 
 cin >> account.accountNumber;
 
 cout << "What is you're current balance: ";
 
 cin >> account.balance;

 cout << "\nGood, is this information correct?\naccountnumber: " << account.accountNumber
   << "\ncurrent balance: " << account.balance << "\n0 - no\n1 - yes" << endl;

 cin >> correct;
}

while (true)
{
 system("CLS");
 cout << "Now, what do you want to do?\n"
   << "1 - Deposit money\n"
   << "2 - Withdraw money\n"
   << "3 - Check my balance\n"
   << "4 - Quit\n";
 
 cin >> option;

 if (option == 1)
 {
  cout << "\nHow much money do you want to deposit?\n";
  cin >> amount;
  deposit(amount, &account);
     cout << amount << " is deposited." << endl;
  system("PAUSE");
 }
 if (option == 2)
 {
  cout << "\nHow much money do you want to withdraw?\n";
  cin >> amount;
  withdraw(amount, &account);
  cout << amount << " is withdrawn." << endl;
  system("PAUSE");
 }
 if (option == 3)
 {
  amount = getBalance(&account);
  cout << "You're balance is: " << amount << "." << endl;
  system("PAUSE");
 }
 if (option == 4)
 {
  break;
 }
}

system("CLS");
cout << "Thank you for using this program." << endl;

system("PAUSE");
return 0;
}


--------------------
user posted image

BLOOP!
Top
Neken
Posted: May 27 2006, 02:35 PM


DirectX Guru


Group: Global Moderators
Posts: 515
Member No.: 11
Joined: 24-September 05



2 things here :

#1 :

CODE
class saving
{
public:
int accountNumber;
float balance;
};


a struct is like a class except that by default, it is public: .
while there is no other differences, c++ programmers prefer to use structs when they only contain public variables and methods. but what you did is NOT an error.

#2 :

CODE

class saving
{
public:
int accountNumber;
float balance;
};

void deposit(int amount, saving* pAccount)
{
pAccount->balance += amount;
}

void withdraw(int amount, saving* pAccount)
{
pAccount->balance -= amount;
}

float getBalance(saving* pAccount)
{
return pAccount->balance;
}


since these functions are touching the class, why are you not putting them inside of the class ? like this :

CODE

class saving
{
public:
int accountNumber;
float balance;
void deposit(int amount);
void withdraw(int amount);
float getBalance();
};

void saving::deposit(int amount)
{
balance += amount;
}

void saving::withdraw(int amount)
{
balance -= amount;
}

float saving::getBalance()
{
return balance;
}


this way, it'll be easier to do the things with the class :

CODE

saving account;
account.deposit(10);
cout << account.getBalance();


#3 :

variables members of a class or struct should take a "m_" in front of their names, to differienciate the "member of"->(the meaning of m_ is this) the function :

CODE

class saving
{
public:
   int   m_accountNumber;
   float m_balance;

   void deposit(int amount);
   void withdraw(int amount);
   float getBalance();
};

void saving::deposit(int amount)
{
   m_balance += amount;
}

void saving::withdraw(int amount)
{
   m_balance -= amount;
}

float saving::getBalance()
{
   m_return balance;
}




--------------------
Top
Stenny
Posted: May 27 2006, 02:44 PM


C++ Composer


Group: Members
Posts: 475
Member No.: 49
Joined: 17-January 06



first of all, thnx for the reply laugh.gif

#1. Thanks for the comment. I have never heard of a struct though, so that's not really a prob (I think the book'll cover that later on)

#2. I did that only because I had to use pointers and that was the first I thought of

CODE
void deposit(int amount, saving* pAccount)
{
pAccount->balance += amount;
}

void withdraw(int amount, saving* pAccount)
{
pAccount->balance -= amount;
}

float getBalance(saving* pAccount)
{
return pAccount->balance;
}


in those functions each time there is one argument which points towards saving account. I also know you can place functions in classes. New Q:
What's more commonly used:
declaring the prototype in the class and defining the function outside the class (to save memory) or defining it in the class to. If the answer is the first, then where do you define it. in the main *.cpp or in some header?

#3. Ok, thanks

~ Stenny


--------------------
user posted image

BLOOP!
Top
Neken
Posted: May 27 2006, 02:51 PM


DirectX Guru


Group: Global Moderators
Posts: 515
Member No.: 11
Joined: 24-September 05



defining a class method in the class definition is touchy. it all depends of where you declare it.

if you define the class in a source file (.cpp), it won't change anything ... like this :

CODE

class saving
{
public:
  int   m_accountNumber;
  float m_balance;

  void deposit(int amount)
  {
     m_balance += amount;
  }

  void withdraw(int amount)
  {
     m_balance -= amount;
  }

  float getBalance()
  {
     m_return balance;
  }
};


HOWEVER, if you declare your class in a header (.h) and define some methods there too, they will be inline and you will show their content :

inline : the code in inline functions is not compiled there. except, it is copied where it is needed then compiled there. if you use it really often, and it is kinda big, it can really make you .exe bigger. since (.h) are never compiled, it's kinda logic the defined function there will be inline.

show their content : most of the time, in a header, you don't want users of the class to see all the implementation of it. they want to see a brief view of the class possibilities, their methods.


--------------------
Top
Stenny
Posted: May 27 2006, 02:55 PM


C++ Composer


Group: Members
Posts: 475
Member No.: 49
Joined: 17-January 06



yes, that's what I mean. Cuz if you do it inline ánd in the class, then each time the class will be needed the methods will be compiled too. So you'd say to compile them inside the header but outside the class...


--------------------
user posted image

BLOOP!
Top
Maxine
Posted: May 27 2006, 03:46 PM


Mega Member


Group: Members
Posts: 71
Member No.: 97
Joined: 23-May 06



allthough a bankprogram where you can deposit and withdraw etc.. could be made without pointers and classes smile.gif for the less experienced guys i'll post it soon


--------------------
My social life...

Playing with my band
Partying with friends
Being with my girlie
Learning computer programming
------------------------------------------------>
Top
Stenny
Posted: May 27 2006, 03:52 PM


C++ Composer


Group: Members
Posts: 475
Member No.: 49
Joined: 17-January 06



yes, but here's the pointers and classes version tongue.gif


--------------------
user posted image

BLOOP!
Top
Cardinal
Posted: May 31 2006, 07:35 PM


The Lizard King


Group: Members
Posts: 244
Member No.: 34
Joined: 6-January 06



Damn you people just know far to much, I'm lazy I did GH lessons then thats about it, I just mess about with stuff.

I'm impressed at the skill level on here !!!

Any of you ever find that you can just read stuff though and know what it means etc ? I have not learnt it but once you have a basic knowledge about some things the rest just follows suit.


--------------------
Aiming for heaven in 2007......
user posted image
Top
Stenny
Posted: Jun 1 2006, 04:44 PM


C++ Composer


Group: Members
Posts: 475
Member No.: 49
Joined: 17-January 06



Whaha thnx, but this is nothing compared to all the things in C++. And if you want to know, I just read (not finished) C++ for dummies, that's all.


--------------------
user posted image

BLOOP!
Top
polar productions
Posted: Jun 11 2006, 05:34 AM


Advanced Member


Group: Members
Posts: 32
Member No.: 62
Joined: 26-January 06



Where you are checking which option is checked, would it not have been better to use a switch case structure? I believe that is what I would have done, however, I am not to experienced in c++, so this is really more for me to learn from!
Top
InvisionFree - Free Forum Hosting
Free Forums with no limits on posts or members.

Topic Options



Hosted for free by InvisionFree* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.1122 seconds | Archive