2013-11-14 46 views
0

我是新来的C++。我想创建银行账户。我想要第一个创建的银行帐户的帐号为100000,第二个应该有100001,第三个应该有100002等等。 我写了一个程序,但是“数字”的值没有改变。每个银行帐户的编号为100000.我不知道如何解决问题。在构造函数中为每个创建的对象更改类变量

.H-文件

#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 - 文件

#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; 
} 
. 
. 
. 
. 
. 
+0

您使用的[保留标识符(http://stackoverflow.com/questions/ 228783 /什么-是最规则约-使用-AN-下划线在-AC-标识符)。无论如何,我无法亲自代言或反对质量,但是这有以下信息:http://www.cprogramming.com/tutorial/statickeyword.html – chris

回答

0

您定义number作为一个简单的成员。如果你希望它是一个类变量,你必须改变number

.H-文件

class account { 
static int number; 
}; 

的.cpp - 文件

int account::number = 100000; 
0

number一个static数据成员,并在你的构造函数,用number++初始化accNumber

0

你可以用一个静态变量做到这一点。

这是一个例子:

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++) {} 
相关问题