Class exercise

Hi everyone, please bear in mind that I am new to this...
I have the following files and they will not compile, can anyone help?
Its an attempt to create a simple bankaccount class as the exercise has stated but looks as if I have failed miserably...

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
 //create a bank account class
//constructor to provide an initial balance and validate greater than or equal to zero
//if not set balance to zero and display initial balance not valid
//provide 3 member functions
//memeber function credit to add to current balance
//member function debit to withdraw and ensure it does not exceed the balance
//if its does balance to remain unchanged but display message
//member function get balance to display current balance

#include<iostream>
#include<string>

using namespace std;

//BankAccount definition
class BankAccount
	{
		public:
			BankAccount();//constructor
			void creditAccount(float);//add to current balance
			void debitAccount(float);//withdraw from account
			float getBalance(float);//display current balance
		private:
			float balance;
	};
//end of class BankAccount

//BankAaccount Class
//create a bank account class
//constructor to provide an initial balance and validate greater than or equal to zero
//if not set balance to zero and display initial balance not valid
//provide 3 member functions
//memeber function credit to add to current balance
//member function debit to withdraw and ensure it does not exceed the balance
//if its does balance to remain unchanged but display message
//member function get balance to display current balance

#include "BankAccount.h"//include definition of BankAccount Class

BankAccount::BankAccount()//constructor for BankAccount
	{
		cout << "Welcome to your new bank account.\n";
		cout << "Please input your opening balance: ";
		cin >> balance;
		cout << endl;
		if ( balance < 0 )
			{
				balance = 0;
				cout << "Sorry your balance has been set to £" << balance << endl;
			}
	}
	
	void creditAccount(float balance)
		{
			float deposit = 0;
			cout << "How much would you like to deposit: ";
			cin >> deposit;
			balance = balance + deposit;
		}
		
	void debitAccount(float balance)
		{
			float debit = 0;
			cout << "What amount would you like to withdraw: ";
			cin >> debit;
			if ( debit > balance )
				{
					cout << "Sorry you have £" << balance << " available\n";
					cout << "Balanve left unchanged" << endl;
				}
			else
				balance = balance - debit;
		}
		
	float getBalance(float balance)
		{
			return balance;
		}

//create a bank account class
//constructor to provide an initial balance and validate greater than or equal to zero
//if not set balance to zero and display initial balance not valid
//provide 3 member functions
//memeber function credit to add to current balance
//member function debit to withdraw and ensure it does not exceed the balance
//if its does balance to remain unchanged but display message
//member function get balance to display current balance


//#include "BankAccount.h"
#include "BankAccount.cpp"

int main()
	{
		BankAccount newAccount;
		
		return 0;
		
	}
Since you wrote everything into one big file (which I do not recommend doing by the way), you don't need your #include statements, except for the necessary ones, like <iostream>. You don't need <string> because you're not doing C-style char arrays.

However, in the event that you DO separate implementation and output, you'd have exactly 3 files: a BankAccount.h, BankAccount.cpp, and Main.cpp. What you need to do for those is #include "BankAccount.h" inside BankAccount.cpp, and then for your Main.cpp, include the header file again.

For now, just get rid of lines 91 and 38
@VFGHNG - The OP may have indeed had three files and simply pasted all three files between one set of code tags. Three sets of code tags would have been clearer.

@OP - Line 91. NEVER include a .cpp file. I received no errors when compiling (as a single file) after commenting out lines 38 and 91.

If you do indeed have three separate files, uncomment line 90 in your main.cpp file.

Last edited on
@AbstractionAnon Yeah you're right, I read it more carefully and @OP did say "files".

But yeah I did the same thing and got rid of lines 38 and 91 and the entire thing ran perfectly fine in the shell as one file.
Sorry guys thats my mistake, I copied and pasted 3 seperta files the first file is upto line 28 and is BankAccount.h
next file is BankAccount.cpp and finishes at line 79
and last file is main

I do apologise
You don't need <string> because you're not doing C-style char arrays.

Not using C-strings is good, but <string> is not for C-strings, rather it is for the use of std::string. The include for the C-string functions would be <cstring>.
I copied and pasted 3 separate files

At least you used code tags. :)
Thanks for all of your help guys I finally got there in the end!!
I am new to all this...!

Thanks again.
Topic archived. No new replies allowed.