?
Solved

Static ints and functions

Posted on 2005-04-18
4
Medium Priority
?
323 Views
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..

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
Comment
Question by:Helix
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
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

by:Axter
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

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

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
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

840 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