Issue with my class

Hi,

I am trying to create a class that adds one to the variable "number_of_accounts_created" each time an object is created. However, this value is being set at 1000 when the first object is created. I am assuming this is because 1000 objects are being created which corresponds with the variable "MAX".

Any ideas how I can fix this? Please keep it simple as I am still new to this.

Here is my code

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

class Customer
{
	public:
		Customer (); 
		~Customer ();
		void set_name ();
		string get_name () const;
		void set_account_number ();
		string get_account_number () const;
		
	private:
		static int number_of_accounts_created;
		string customer_fullname;
		string account_number;
		double balance;
		double deposit;
		double withdrawl;
		double interest;
};

Customer::Customer ()
{
    ++number_of_accounts_created;               
}

Customer::~Customer ()
{
}

void Customer::set_name()
{
	string first_name, last_name;
	cout << "Please enter new customer's first name: ";
	cin >> first_name;
	cout << "Please enter new customer's last name: ";
	cin >> last_name;
	customer_fullname = first_name + " " + last_name;	
}

string Customer::get_name() const
{
	return customer_fullname;
}

void Customer::set_account_number ()
{
	int last_six_digits = 112280 + number_of_accounts_created;
	//the cout has just been added so I can see value of number_of_accounts_created being used
        cout << "Number of accounts created: " << number_of_accounts_created;
	ostringstream convert;
	convert << last_six_digits;
	string first_two_digits = "00";
	account_number = first_two_digits + convert.str();
}

string Customer::get_account_number () const
{
	return account_number;
}

const int MAX = 1000;
Customer new_customer [MAX];
int Customer::number_of_accounts_created = 0;

int main ()
{
    new_customer [0].set_name();
    new_customer [0].set_account_number();
    //system ("CLS");
    cout << "Customer's Name: " << new_customer [0].get_name() << endl;
    cout << "Account number is: " << new_customer [0].get_account_number() << endl;
	
    system ("PAUSE");
    return 0;
}
on line 69 you create and array of Customer with MAX elements. For each element of that array the default constructor is called and number_of_accounts_created is incremented. as long as you use an array this will continue to happen. I would use a vector instead and create customer objects as needed or you could use a dynamic array but that's a pain.
Cheers mate, I thought that was where the problem was. I will get my text book out and read about vectors then and see how I get on.
Topic archived. No new replies allowed.