Can't Use My Classes


This program is working fine as a procedural program, but I cannot get it to run as a classes/object-oriented program. All of the work for the program is being done in the main atm_machine.cpp file, whenever I try to access the functions in the account.h and account.cpp files, I break something and the program won't compile.

So, what is the correct way, for example, to set the account balance to $100 for each of the 10 accounts in the array, and what is the correct way, for example, to make a withdrawal?

In summary, I need to make this procedural program use classes.

Thank you

account.h
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
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
	
	public: 
		Account();
		Account(int newID, double newBalance, double newAnnualInterestRate);  
		int getID() const; 
		double getBalance() const; 
		double getAnnualInterestRate() const;  
		int setID(); 
		double setBalance(); 
		double setAnnualInterestRate();  	 
		double getMonthlyInterestRate() const;
		void withdraw (double amount); 
		void deposit (double amount); 

	private:	
		int id;
		double balance, annualInterestRate; 
};  

 
#endif


account.cpp
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
#include "account.h"

Account::Account()
{
	id = 0; 
	balance = 0; 
	annualInterestRate = 0;  
} 

Account::Account (int newID, double newBalance, double newAnnualInterestRate) 
{ 
	id = newID; 
	balance = newBalance; 
	annualInterestRate = newAnnualInterestRate; 
} 

int Account::getID() const
{ 
	return id; 
} 
double Account::getBalance() const 
{
	return balance; 
}  
double Account::getAnnualInterestRate() const
{
	return annualInterestRate; 
} 

int Account::setID()
{ 
	return id; 
}
double Account::setBalance()
{
 	return balance; 
} 
double Account::setAnnualInterestRate()
{
	return annualInterestRate; 
} 

double Account::getMonthlyInterestRate() const
{
	return (annualInterestRate / 12); 
}

void Account::withdraw (double amount)
{
	balance -= amount; 
}

void Account::deposit (double amount)
{
	balance += amount; 
}  


atm_machine.cpp
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
#include "account.h"
#include <iostream>
using namespace std; 

int main ()
{
	Account myAccount; 	

	double a[10]; 
	for (int i = 0; i < 10; i++) 
		a[i] = 100.00; 

	int menuChoice = 0; 
	double amount = 0.0; 

	do
	{
		do
		{
		cout << "Enter an account id number: "; 
		cin >> myAccount.id;   		
			if (myAccount.id < 0 || myAccount.id > 9)
				cout << "Invalid Entry" << endl; 
		}	
		while (myAccount.id < 0 || myAccount.id > 9);
		
		do
		{
		cout << "Main Menu" << endl; 
		cout << "1: check balance" << endl; 
		cout << "2: withdraw" << endl; 
		cout << "3: deposit" << endl; 
		cout << "4: exit" << endl;  
		cout << "enter a choice: "; 
		cin >> menuChoice; 
		if (menuChoice < 1 || menuChoice > 4) 
			cout << "Invalid Entry" << endl << endl;  
		} 
		while (menuChoice < 1 || menuChoice > 4); 
		if (menuChoice == 1) 
			cout << a[myAccount.id] << endl << endl; 
 		if (menuChoice == 2) 
			{
			cout << "Enter an amount to withdraw: ";  
			cin >> amount; 
			a[myAccount.id] -= amount; 
			cout << "The balance is: " << a[myAccount.id] << endl << endl; 
			}
		if (menuChoice == 3) 
			{
			cout << "Enter an amount to deposit: ";  
			cin >> amount; 
			a[myAccount.id] += amount; 
			cout << "The balance is: " << a[myAccount.id] << endl << endl;  
			}
		if (menuChoice == 4) 
			cout << endl; 
			 
	}
	while (myAccount.id >= 0 || myAccount.id <= 0);  
	
	system("PAUSE"); 
	return 0; 
} 


By the way, as strange as it may sound, the program is supposed to function endlessly, which is why it's contained in an endless loop.

Thanks
Last edited on
you cannot enter directly to a private variable. you need accessors.

1
2
3
               // this will not work
		cin >> myAccount.id;
 


for example
create a public function that will set and get values from your private variable.
1
2
3
4
void Account::setID(int id_val)
{ 
	id = id_val; 
}
Topic archived. No new replies allowed.