Solved

# Static ints and functions

Posted on 2005-04-18
Medium Priority
323 Views
Hey everyone, im just trying to do a C++ question out of my textbook, but i cant make it run..

Code:

#include <iostream>

using namespace std;

class BankAccount{

public:
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();

private:
double balance;
int accNum;
static int numberOfAccs;

};

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

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?

0
Question by:Helix
• 2
• 2

LVL 30

Accepted Solution

Axter earned 1000 total points
ID: 13808059
Hi Helix,
> >BankAccount::numberOfAccs = 0;

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

David Maisonave :-)
Cheers!
0

LVL 30

Expert Comment

ID: 13808080
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.
0

Author Comment

ID: 13809017
Aha, got it to run, but for some reason its not incrementing...
0

Author Comment

ID: 13809026
Nm got it working, thanks! :D
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
###### Suggested Courses
Course of the Month14 days, 23 hours left to enroll