Static ints and functions

Posted on 2005-04-18
Last Modified: 2010-04-01
Hey everyone, im just trying to do a C++ question out of my textbook, but i cant make it run..


#include <iostream>

using namespace std;

class BankAccount{

      BankAccount(double amount = 0.0);
      void deposit(double amount);
      void withdraw(double amount);
      int getAccNum() const;
      double getBalance() const;  
    void printDetails()const;
      static int getNumberOfAccs();
   double balance;
   int accNum;
   static int numberOfAccs;


BankAccount::BankAccount(double amount)
      balance = amount;
      numberOfAccs = 0;
      accNum  =  numberOfAccs  * 1000;

void BankAccount::deposit(double amount)
      balance += amount;


void BankAccount::withdraw(double amount)
      balance -= amount;


int BankAccount::getAccNum() const
      return accNum;

double BankAccount::getBalance() const
      return balance;

void BankAccount::printDetails() const
      cout << "For account number" << getAccNum() << "Has a balance of " <<  getBalance() << endl;


int BankAccount::getNumberOfAccs()
      return numberOfAccs;

int main()
      BankAccount Test(2000);

return 0;

basically what I have to do is each time a bankaccount constructor is used, numberOfAcc is incrememnted by one, then the instance variable accNum can then be initialised from the static variable .
 e.g. if account numbers have 4 digits
accNum  =  numberOfAccs  * 1000;

Now, i think i have to initialise the static variable by doing something like:

BankAccount::numberOfAccs = 0;

but that doesn't seem to work. Does anyone have any pointers please?

Question by:Helix
    LVL 30

    Accepted Solution

    Hi Helix,
    > >BankAccount::numberOfAccs = 0;

    It should be in your *.cpp file, and you need the type.
    int BankAccount::numberOfAccs = 0;

    David Maisonave :-)
    LVL 30

    Expert Comment

    In your original code, you're missing the type (int) for your static variable declaration.

    Make sure that this is declared in your *.cpp file, and not in the header.

    Author Comment

    Aha, got it to run, but for some reason its not incrementing...

    Author Comment

    Nm got it working, thanks! :D

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now