2013-11-14 1 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); 
}; 

통화 당 - 파일

#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/

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

:

은 일례이며 228783/what-are-the-the-the-that-using-an-ac-identifier (역자 주)). 어쨌든, 나는 개인적으로 또는 품질에 대해 말할 수는 없지만 다음 정보가 있습니다. http://www.cprogramming.com/tutorial/statickeyword.html – chris

답변

0

당신은 간단한 회원으로 number을 정의했다. 당신이 클래스 변수를하길 원한다면, 당신은

int account::number = 100000; 
0

numberstatic 데이터 멤버를 확인 number

class account { 
static int number; 
}; 

통화 당 - 파일

에 .H-파일 변경에 있어야합니다 생성자가 accNumber 인 경우 number++으로 초기화하십시오.

0

정적 변수를 사용하여이를 수행 할 수 있습니다. A.cpp

int A::count_s = 100000; 

A::A() : id_(count_s++) {}