Hello

I am creating a bank account for an assignment and the account number must be automatically generated with consecutive numbers. I have written some code that works, but now I need to know how to use it in conjunction with my Account class, especially how to add it to my class constructor. I have the code below, the whole program doesn't compile and I am working at it one chunk at a time. The first piece of code is the accountNumber class which gives a new incremented account number each time it is called. The second part is my Account class and how I think it has to be added. Thank you in advance.

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

using namespace std;

class AccountNumber
{
private:
	static int accountNumber;
public:
	static int addNum()
	{ accountNumber++;
	cout << "new account number is :" << accountNumber << endl;
	return accountNumber;
	}
	static int getAccountNumber()
	{
		return accountNumber;
	}
};

int AccountNumber::accountNumber = 1000;	

int main()
{
	AccountNumber::addNum();
	AccountNumber::addNum();
	AccountNumber::addNum();
	AccountNumber::addNum();
	AccountNumber::addNum();
	AccountNumber::addNum();

system ("Pause");

	return 0;
}
class Account
{      

protected: //these are protected since there are child classes
	AccountNumber accountNumber;   
	double accountBalance;  
	
public:
	Account(AccountNumber acctNum, double bal); //I have my doubts about this
	void enterAccountData(AccountNumber acctNum, double bal); // and this								
	void displayAccount();
};

and finally my Account constructor

Account::Account (AccountNumber acctNum, double bal)
{
	AccountNumber accountNumber = acctNum;
	accountBalance = bal;
}

Recommended Answers

All 4 Replies

I'm curious...why did you create a separate class for the account number? It seems to me that you could have put your static variable in the Account class. Then, you constructor could retrieve the account number, copy it to the member accountNumber, and increment it.

This would obviate your issue about how to use the two classes together, plus it eliminates the need for a separate class to hold the next available number.

I agree with mzimmers - unless you need it for some special reason, the AccountNumber class is superfluous, you can easily replace it with a static int.

I would change the code to be something like this (i have made these sample definitions inline for demonstrative purposes):

class Account
{      

protected: // these are protected since there are child classes
	static int currentAccountNumber = 0;
	int accountNumber;
	double accountBalance;  
	
public:
	Account(double bal) {
		accountNumber = currentAccountNumber++;
		setBalance(bal);
		// anything else...
	}
	void setBalance(double bal) {
		accountBanace = bal;
	}

	double balance() {
		return accountBalance;
	}
	
	void displayAccount();
};

Thank you both, for your help. I am really new to this and I think I make things harder for myself by thinking a solution is too easy to be right. Thanks again

That's very easy to do, George. One of the secrets to successful engineering of any kind is learning to determine what is, and what isn't necessary to do. Less is more.

As a relatively junior programmer, I worked on two projects that totalled over a million lines of code. Both were grotesquely over-designed by academic types that got fascinated with the challenge of making something complex and interesting, rather than simply addressing the customer's needs. Both projects failed, and cost a lot of people a lot of money.

In the example of your program, the consequences are of course much more modest. You wouldn't have "killed" your program by the addition of the extra class; you'd have just made a bit more work for yourself, and for anyone who would be maintaining your code. But, the best way to un-learn bad habits is to avoid them in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.