Help with OOP

I have been trying to simulate a basic bank system (create/delete bank accounts, keep track of the total balance existing in the bank as well as a local balance for each account; plus other small things).

Though, I've encountered some problems with the overloading operator==() and I am not sure if that's the only problem. I also have to mention that I am new to Object Orientated Programming, and I'm trying to learn it, so please excuse the possibly stupid mistakes.

Here is the full 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
// Bank.h
#ifndef BANK_H
#define BANK_H

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Bank {

	private:
		string name;
		int accountNumber;
		static double balance;

		static int totalAccounts;
		static double bankBalance;

		static int choice;
		static void choose(void);

		static int getAccount(vector<Bank>&);

		string getName(void) const;
		int getAccountNumber(void) const;
		double getBalance(void) const;
		void setName(string);
		void setAccountNumber(int);
		void setBalance(double);

		friend bool operator==(Bank, Bank);

	public:
		Bank(void);
		Bank(string, int, double);
		~Bank(void);

		static void printMenu(void);
		static int getChoice(void);
		static Bank createNewAccount(void);
		static void deleteAccount(vector<Bank>&);
		static void getAccountState(vector<Bank>&);
		static void setAccountState(vector<Bank>&);
		static void withdraw(vector<Bank>&);
		static void deposit(vector<Bank>&);
};

#endif 


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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Bank.cpp
#include "Bank.h"

/* Static Variables Definition */
int Bank::totalAccounts = 0;
int Bank::newAccountNumber = 0;
double Bank::bankBalance = 0.0;
/* --------------------------- */

/* Constructors / Destructor Definition */
Bank::Bank(void) {
	name = "";
	accountNumber = 0;
	balance = 0.0;
	totalAccounts++;
}

Bank::Bank(string newName, int newAccountNumber, double newBalance) {
	name = newName;
	accountNumber = newAccountNumber;
	balance = newBalance;
	totalAccounts++;
	bankBalance += newBalance;
}

Bank::~Bank(void) {
	totalAccounts--;
	bankBalance -= balance;
}
/* ----------------------- */

/* Private Functions Definiton */
int Bank::getAccount(vector<Bank>& bank) {
	string name;
	cout << "   Enter your name: "; cin >> name;

	vector<Bank>::iterator it;
	it = find(bank.begin(), bank.end(), name);
	
	return (*it).accountNumber;
}

string Bank::getName(void) const {
	return name;
}

int Bank::getAccountNumber(void) const {
	return accountNumber;
}

double Bank::getBalance(void) const {
	return balance;
}

void Bank::setName(string newName) {
	name = newName;
}

void Bank::setAccountNumber(int newAccountNumber) {
	accountNumber = newAccountNumber;
}

void Bank::setBalance(double newBalance) {
	bankBalance -= balance;
	balance = newBalance;
	bankBalance += balance;
}
/* -------------------------- */

/* Operator == Overload */
bool operator==(Bank accountOne, Bank accountTwo) {
	return (accountOne.getName() == accountTwo.getName());
}
/* -------------------- */

/* Public Functions Definiton */
/* Static Functions Definitions */
void Bank::printMenu(void) {
	cout << "\n";
	cout << "Welcome to the Bank" << "\n";
	cout << "1) Create new account." << "\n";
	cout << "2) Delete an account." << "\n";
	cout << "3) Check your account state." << "\n";
	cout << "4) Set your account state (only balance)." << "\n";
	cout << "5) Withdraw." << "\n";
	cout << "6) Deposit." << "\n";
	cout << "7) Exit." << "\n";
	choose();
}

void Bank::choose(void) {
	cout << "Enter choice: ";
	cin >> choice;
}

int Bank::getChoice(void) {
	return choice;
}

Bank Bank::createNewAccount(void) {
	string newName = "";
	double newBalance = 0.0;

	Bank newBank;

	cout << "    o Enter your name: ";
	cin >> newName;
	cout << "    o Enter your balance: ";
	cin >> newBalance;
	newAccountNumber++;
	cout << "    o Your account number will be " << newAccountNumber << "\n";
	
	newBank.setName(newName);
	newBank.setAccountNumber(newAccountNumber);
	newBank.setBalance(newBalance);

	return newBank;
}

void Bank::deleteAccount(vector<Bank>& bank) {
	int nr = getAccount(bank);
	bank.erase(bank.begin()+(nr-1));
	totalAccounts--;
	bankBalance -= bank[nr].balance;
}

void Bank::getAccountState(vector<Bank>& bank) {
	int nr = getAccount(bank);
	cout << "\n";
	cout << "  " << bank[nr].getName() << "'s account state: ";
	cout << "    o Account number: " << bank[nr].getAccountNumber();
	cout << "    o Account balance: " << bank[nr].getBalance();
	cout << "\n";
}

void Bank::setAccountState(vector<Bank>& bank) {
	int nr = getAccount(bank);
	double newBalance;
	cout << "   Enter the new balance: "; cin >> newBalance;
	bank[nr].setBalance(newBalance);
}

void Bank::withdraw(vector<Bank>& bank) {
	int nr = getAccount(bank);
	double withdraw;
	cout << "   Enter the amount to withdraw: "; cin >> withdraw;
	bank[nr].balance -= withdraw;
	bankBalance -= withdraw;
}

void Bank::deposit(vector<Bank>& bank) {
	int nr = getAccount(bank);
	double deposit;
	cout << "   Enter the amount to deposit: "; cin >> deposit;
	bank[nr].balance += deposit;
	bankBalance += deposit;
}
/* --------------------------- */
/* ----------------------------- */


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
// Main.cpp
#include <iostream>
#include <vector>
#include <string>
#include "Bank.h"

using namespace std;

int main(void) {
	vector<Bank> bank;	

	Bank::printMenu();
	int choice = Bank::getChoice();
	while ( choice > 0 && choice < 7){
		switch(choice){
			case 1:
				bank.push_back(Bank::createNewAccount());
				break;
			case 2:
				Bank::deleteAccount(bank);
				break;
			case 3:
				Bank::getAccountState(bank);
				break;
			case 4:
				Bank::setAccountState(bank);
				break;
			case 5:
				Bank::withdraw(bank);
				break;
			case 6:
				Bank::deposit(bank);
				break;
			default:
				break;
		}
		Bank::printMenu();
		choice = Bank::getChoice();
	}
}


Thanks in advance.
Last edited on
The outputted error I get on MS Visual Studio 2012:

1>------ Build started: Project: Bank, Configuration: Debug Win32 ------
1>  Bank.cpp
1>d:\software\microsoft\visual studio 2012 - ultimate\vc\include\xutility(3186): error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
1>          c:\users\jumper\desktop\c++ oop\bank\bank\bank.h(33): could be 'bool operator ==(Bank,Bank)' [found using argument-dependent lookup]
1>          d:\software\microsoft\visual studio 2012 - ultimate\vc\include\exception(507): or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &)'
1>          d:\software\microsoft\visual studio 2012 - ultimate\vc\include\exception(512): or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &)'
1>          d:\software\microsoft\visual studio 2012 - ultimate\vc\include\exception(517): or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t)'
1>          d:\software\microsoft\visual studio 2012 - ultimate\vc\include\system_error(426): or       'bool std::operator ==(const std::error_code &,const std::error_condition &) throw()'
1>          d:\software\microsoft\visual studio 2012 - ultimate\vc\include\system_error(434): or       'bool std::operator ==(const std::error_condition &,const std::error_code &) throw()'
1>          while trying to match the argument list '(Bank, const std::string)'
1>          d:\software\microsoft\visual studio 2012 - ultimate\vc\include\xutility(3219) : see reference to function template instantiation '_InIt std::_Find<Bank*,_Ty>(_InIt,_InIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _InIt=Bank *,
1>              _Ty=std::string
1>          ]
1>          c:\users\jumper\desktop\c++ oop\bank\bank\bank.cpp(36) : see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<_Myvec>,std::string>(_InIt,_InIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Bank>>>,
1>              _Myvec=std::_Vector_val<std::_Simple_types<Bank>>,
1>              _Ty=std::string
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You are trying to compare a Bank with a std::string

1
2
3
//it = find(bank.begin(), bank.end(), name);
it = find_if( bank.begin(), bank.end(), 
              [name]( const Bank& b) { return b.name == name ; } );

Thanks. Though, even after replacing this, I get some error. I have no idea what it means or what it is about. Could you please tell me what might be the issue ?

1>------ Build started: Project: Bank, Configuration: Debug Win32 ------
1>  Source.cpp
1>  Bank.cpp
1>  Generating Code...
1>Bank.obj : error LNK2001: unresolved external symbol "private: static double Bank::balance" (?balance@Bank@@0NA)
1>Bank.obj : error LNK2001: unresolved external symbol "private: static int Bank::choice" (?choice@Bank@@0HA)
1>C:\Users\Jumper\Desktop\C++ OOP\Bank\Debug\Bank.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
These two static members of Bank have not been defined.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Bank {

    // ...

    static double balance;

    // ...

    static int choice;
    
    // ...

};


Define them in Bank.cpp

Why are these are static members of Bank? What purpose do they serve?
Oh, right, thanks. :)
Well, the only reason I made them static is because the functions in which they active are also static and the compiler returned an error otherwise.

PS: Also, I did a mistake when taking the input of the choice. The loop shall let a user enter a new command on each loop, so therefore I changed that.

Well, all in all, thanks for the help JLBorges :)
Last edited on
Topic archived. No new replies allowed.