Home > Back-end >  C How to Auto Increment static integer variable on object creation?
C How to Auto Increment static integer variable on object creation?

Time:08-06

C How to Auto Increment static integer variable on object creation?

When I run the program I want the account number to increment, and I am required to use a static member of the class to automatically assign numbers.

I am currently getting a make error for undefined reference to `bankAccount::accountNumber'

bankAccount.h

#include <string>

using std::string;

class bankAccount {
        string accountName;
        int accountNumber;
        string accountType;  //Checking or Savings.
        double accountBalance;
        double interestRate;

    public:
        static int nextAccNum;
        bankAccount();
        bankAccount(string accName, string accType, double accBalance, double intRate);
        void deposit();
        void withdraw();
        void display();
};

bankAccountImp.cpp

#include <iostream>
#include "bankAccount.h"

using std::cout;
using std::cin;
using std::endl;

        bankAccount::bankAccount() {
            accountName = "";
            accountNumber = nextAccNum  ;
            accountType = "";
            accountBalance = 0.00;
            interestRate = 0.00;
            accountNumber  ;
        }
            
        bankAccount::bankAccount(string accName, string accType, double accBalance, double intRate) {
            accountName = accName;
            accountNumber = nextAccNum  ;
            accountType = accType;
            accountBalance = accBalance;
            interestRate = intRate;
        }
        
        void bankAccount::deposit() {
            int depAmt;
            cout << "Please enter an amount to deposit: ";
            cin >> depAmt;
            accountBalance  = depAmt;
        }
        
        void bankAccount::withdraw() {
            int delAmt;
            cout << "Please enter an amount to deposit: ";
            cin >> delAmt;
            accountBalance -= delAmt;
        }
        void bankAccount::display() {
            cout << "Account Number: " << accountNumber << endl;
            cout << "Holder's Name: " << accountName << endl;
            cout << "Account Type: " << accountType << endl;
            cout << "Interest Rate: " << interestRate << endl;
        }

The program

#include <iostream>
#include <iomanip>
#include "bankAccount.h"

using std::cout;
using std::endl;

int main() {
    bankAccount acct1;
    bankAccount acct2;
    
    acct1.display();
    cout << endl;
    acct2.display();
    return 0;
}

CodePudding user response:

Like this, I've renamed the static variable nextAccNum. Having one variable called accNum and another called accountNumber is obviously a recipe for confusion.

   bankAccount::bankAccount() {
        accountName = "";
        accountNumber = nextAccNum  ;
        accountType = "";
        accountBalance = 0.00;
        interestRate = 0.00;
    }
        
    bankAccount::bankAccount(string accName, string accType, double accBalance, double intRate) {
        accountName = accName;
        accountNumber = nextAccNum  ;
        accountType = accType;
        accountBalance = accBalance;
        interestRate = intRate;
    }
  • Related