0

I am new to c++. I want to create bank account. I want the first created bank account to have the account number 100000, the second should have 100001, the third should have 100002 and so on. I wrote a program but the value of "number" doesn't change. Every bank account has the number 100000. I am not sure how to solve the problem.

.h-File

#include <iostream>
#include <string>
using namespace std;
#ifndef _ACCOUNT_H
#define _ACCOUNT_H


class account
{
private:
    string name;
    int accNumber;
    int number= 100000;
    double balance;
    double limit;

public:
    void setLimit(double limit);
    void deposit(double amount);
    bool withdraw(double amount);
    void printBalance();
    account(string name);
    account(string name, double limit);
};

.cpp-File

#include <iostream>
#include <string>
#include "account.h"
using namespace std;

account::account(string name) {
    this->name= name;
    accNumber= number;
    number++;
    balance= 0;
    limit = 0;
}

account::account(string name, double amount) {
    this->name= name;
    accNumber = number;
    number++;
    balance= 0;
    limit = amount;
}

void account::setLimit(double limit) {
    this->limit = limit;
}
.
.
.
.
.
  • You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). Anyway, I can't personally speak for or against quality, but this has information: http://www.cprogramming.com/tutorial/statickeyword.html – chris Nov 14 '13 at 14:41

3 Answers3

0

You defined number as a simple member. If you want it to be a class variable, you must change number to

.h-File

class account {
static int number;
};

.cpp-File

int account::number = 100000;
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

Make number a static data member, and in your constructor, initialize accNumber with number++.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

You can do this with a static variable.

This is an example:

A.h

class A {
public:
  A();
  int getId() {
    cout << count_s;
  }
private:
  int id_;
  static int count_s;
}  

A.cpp

int A::count_s = 100000;

A::A() : id_(count_s++) {}
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146