Can someone explain this to me?

In class we have a prompt to write a code for a bank account. http://www.cs.indiana.edu/classes/a290-gamebryo/examples/ch09.pdf <- this link goes through it in steps but i don't understand where things go. Like where do I put the constructors, member functions, etc. If you could explain it to me better than they do I'd appreciate it. Keep in mind that I only started to learn programming a few months ago. Thanks in advance!
You've been programming for a few months but dont even know where to put functions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class BankAccount
{
public:
 BankAccount();
 BankAccount(double initial_balance);
 void deposit(double amount);
 void withdraw(double amount);
 void add_interest(double rate);
 double get_balance() const;
private:
 ...
};

That code is in the example. This is called a class. [code]BankAccount();
is the constructor. the voids are member functions. etc.
[/code]

If you want to learn how classes works. Try googling it, or watch some tutorials on youtube. always helps.
You have several choices, but I'll only mention the 2 most appropriate here. I've organised the code into 3 blocks below and you could have:
1. each block in it's own file, one header and 2 implementation files
2. all 3 blocks in one implementation file.

See the comments I've added to the code:

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This is the class declaration. It could be in it's own file, e.g. BankAccount.h
// use header guards if it's in a separate file, like so:

// #ifndef BANK_ACCOUNT_H
// #define BANK_ACCOUNT_H

/**
 A bank account whose balance can be changed by deposits and withdrawals.
*/
class BankAccount
{
public:
	/**
	Constructs a bank account with zero balance.
	*/
	BankAccount();
	/**
	Constructs a bank account with a given balance.
	@param initial_balance the initial balance
	*/
	BankAccount(double initial_balance);
	/**
	Makes a deposit into this account.
	@param amount the amount of the deposit
	*/
	void deposit(double amount);
	/**
	Makes a withdrawal from this account, or charges a penalty if
	sufficient funds are not available.
	@param amount the amount of the withdrawal
	*/
	void withdraw(double amount);
	/**
	Adds interest to this account.
	@param rate the interest rate in percent
	*/
	void add_interest(double rate);
	/**
	Gets the current balance of this bank account.
	@return the current balance
	*/
	double get_balance() const;
private:
	double balance;
};

// use header guards if in a separate file, like so:
// #endif //BANK_ACCOUNT_H



*******************************************************************


// This is the implementation of the class declared above
// If the class is declared in a separate file, e.g. BankAccount.h,
// then this should also be in its own file, e.g. BankAccount.cpp
// it then follows that the header file must be included here like so:

// #include "BankAccount.h"

double BankAccount::get_balance() const
{
	return balance;
}

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

void BankAccount::withdraw(double amount)
{
	const double PENALTY = 10;
	if (amount > balance)
	{
		balance = balance - PENALTY;
	}
	else
	{
		balance = balance - amount;
	}
}

void BankAccount::add_interest(double rate)
{
	double amount = balance * rate / 100;
	deposit(amount);
}

BankAccount::BankAccount()
{
	balance = 0;
}

BankAccount::BankAccount(double initial_balance)
{
	balance = initial_balance;
}  



*******************************************************************


// This is the test program, again this could be in a separate file, 
// or all 3 could be in one .cpp file. notice the standard library 
// headers included, for cout, endl, fixed and setprecision
// If it's in a file by itself, the BankAccount class must be declared, 
// by including the header like so:

// #include "BankAccount.h"

#include <iostream>
#include <iomanip>

int main()
{
	BankAccount harrys_account(1000);
	harrys_account.deposit(500); // Balance is now $1500
	harrys_account.withdraw(2000); // Balance is now $1490
	harrys_account.add_interest(1); // Balance is now $1490 + 14.90
	std::cout << std::fixed << std::setprecision(2)
	<< harrys_account.get_balance() << std::endl;
	return 0;
}
Last edited on
tipaye: thanks for helping me out with that. it keeps saying balance is undefined and undeclared. do you know how to fix that?
Make sure line 44 exists and that you're implementing your member functions instead of creating identically-named global functions.
If you're still having problems, post your whole code. I also think LB's guess is the likely, problem. Otherwise, happy coding.
Topic archived. No new replies allowed.