issue with inheritance inheritance

Hey, I am working on an assignment that basically wants me to create a header class called BankAccount.h and then create A CheckingAccount.h and a SavingAccount.h that are derived from BankAccunt.H Then in the tester create two Accounts of each type (checking and savings) and do a few basic functions such as deposit, withdraw and such.


I have this for my code--

BankAcount.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
using namespace std;

class BankAccount{
public:

	BankAccount();
	BankAccount(double balance);
	int get_actNumber();
	void deposit(double amount);
	void withdrawl(double amount);
	double get_balance();
	void printInfo();


protected:
	double _balance;
	static int _number;
};
BankAccount::BankAccount()
{
	_balance = 0.0;
	
}

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

int BankAccount::get_actNumber()
{
	return _number = _number + 100;
}
void BankAccount::deposit(double amount)
{
	_balance = _balance + amount;
}
void BankAccount::withdrawl(double amount)
{
	_balance = _balance - amount;
}
double BankAccount::get_balance()
{
	return _balance;
}
int BankAccount::_number = 1000;


CheckingAccount.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
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
#include <iostream>
#include <iomanip>
#include "BankAccount.h"

#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H
using namespace std;

class CheckingAccount : public BankAccount
{
public:
	CheckingAccount(double balance);

	
	//CheckingAccount(double balance);
	void set_interestRate(double rate);
	double get_interestRate();
	void set_minBalance(double minBal);
	double get_minBalance();
	void set_fee(double fee);
	double get_fee();
	void postInterest();
	bool checkMin();
	void withdrawl(double amount);
	void writeCheck(double amount);
	void printInfo();
	//void CheckingAccount::printInfo()
	



protected:
	double _rate, _minBalance, _fee;
	


};
#endif

CheckingAccount::CheckingAccount(double balance)//Since the tester that was provided doesnt set the rate, min balance or fee I just set it here
	: BankAccount(balance)
{
	//_number = get_actNumber();
	_fee = 15;
	_minBalance = 200;
	_rate = 0.04;
}

void CheckingAccount::set_interestRate(double rate)
{
	_rate = rate;
}
double CheckingAccount::get_interestRate()
{
	return _rate;
}
void CheckingAccount::set_minBalance(double minBal)
{
	_minBalance = minBal;
}
double CheckingAccount::get_minBalance()
{
	return _minBalance;
}
bool CheckingAccount::checkMin()
{
	if (_balance >= _minBalance)
	{
		return true;
	}
	else
		return false;
}
void CheckingAccount::set_fee(double fee)
{
	_fee = fee;
}
double CheckingAccount::get_fee()
{
	return _fee;
}
void CheckingAccount::postInterest()
{
	_balance += (_balance * _rate);
}
void CheckingAccount::withdrawl(double amount)
{
	if (_balance < amount)
	{
		cout << "insufficient funds. Withdrawl rejected, does not affect balance." << endl;
	}
	else//I assume the bank will allow a withdrawl as long as the balance is greater than the amount even if the fee drops the account into the negative
	{
		if (_balance - amount >= _minBalance)
		{
			_balance = _balance - amount;
		}
		else
		{
			_balance -= (amount + _fee);
			
		}

	}
}
void CheckingAccount::writeCheck(double amount)
{
	withdrawl(amount);
}
void CheckingAccount::printInfo()
{
	cout << "Interest Checking ACCT#:" << get_actNumber() << "                    Balance: $" << setprecision(2) << fixed << get_balance() << endl;
}



SavingAccunt.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
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
#include <iostream>
#include <iomanip>
#include "BankAccount.h"


#ifndef SAVINGACCOUNT_H
#define SAVINGACCOUNT_H
using namespace std;

class SavingAccount : public BankAccount
{
public:
	SavingAccount(double balance);
	void set_interestRate(double rate);
	double get_interestRate();
	void postInterest();
	void withdrawl(double amount);
	void printInfo();
protected:
	double _rate;



};
#endif
SavingAccount::SavingAccount(double balance)//Since the tester that was provided doesnt set the rate, min balance or fee I just set it here
	: BankAccount(balance)
{
	_rate = .06;
}
void SavingAccount::set_interestRate(double rate)
{
	_rate = rate;
}
double SavingAccount::get_interestRate()
{
	return _rate;
}
void SavingAccount::postInterest()
{
	_balance += (_balance * _rate);
}
void SavingAccount::withdrawl(double amount)
{
	if (_balance >= amount)
	{
		_balance = _balance - amount;
	}
	else
	{
		cout << "Insufficient funds. Withdrawl transaction rejected, balance remains the same" << endl;
	}
}
void SavingAccount::printInfo()
{
	cout << "Interest Saving ACCT#:" << get_actNumber() << "                    Balance: $" << setprecision(2) << fixed << get_balance() << endl;

}


Originally after I wrote my BankAccount header and my CheckingAccount header I then used the tester that was provided to me with just the checking account objects/functions and it ran fine. However when I did my savingAccount header and tried to run both in the tester I got a bunch of errors but if I run just the saving account in the tester it works fine. So basically I can run either of the CheckingAccount or SavingAccount in the main but not both. I am not sure what I am doing wrong or why, do I need a deconstructor? This is the test program with just the checking account objects and functions that runs fine.

tester.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
#include <iostream>
#include <iomanip>
#include "CheckingAccount.h"
using namespace std;

int main()
{
	
	CheckingAccount jackAccount(1000);
	CheckingAccount lisaAccount(450);
	

	jackAccount.deposit(1000);
	lisaAccount.deposit(2300);
	

	jackAccount.postInterest();
	lisaAccount.postInterest();
	

	cout << "***********************************" << endl;
	jackAccount.printInfo();
	lisaAccount.printInfo();
	
	cout << "***********************************" << endl << endl;

	jackAccount.writeCheck(250);
	lisaAccount.writeCheck(350);
	

	cout << "********After withdrawals ***************" << endl;
	jackAccount.printInfo();
	lisaAccount.printInfo();
	
	cout << "***********************************" << endl << endl;


	system("pause");
	return 0;


}


This runs fine (besides a few things I need to fix such as the account numbers).

But when I throw in #include SavingAccunt.h and the functions I get errors

full tester
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
#include <iostream>
#include <iomanip>
#include "SavingAccount.h"
#include "CheckingAccount.h"

using namespace std;

int main()
{


	CheckingAccount jackAccount(1000);
	CheckingAccount lisaAccount(450);
	SavingAccount samirAccount(9300);
	SavingAccount ritaAccount(32);

	jackAccount.deposit(1000);
	lisaAccount.deposit(2300);
	samirAccount.deposit(800);
	ritaAccount.deposit(500);

	jackAccount.postInterest();
	lisaAccount.postInterest();
	samirAccount.postInterest();
	ritaAccount.postInterest();

	cout << "***********************************" << endl;
	jackAccount.printInfo();
	lisaAccount.printInfo();
	samirAccount.printInfO();
	ritaAccount.printInfo();
	cout << "***********************************" << endl << endl;

	jackAccount.writeCheck(250);
	lisaAccount.writeCheck(350);
	samirAccount.withdrawl(120);
	ritaAccount.withdrawl(290);

	cout << "********After withdrawals ***************" << endl;
	jackAccount.printInfo();
	lisaAccount.printInfo();
	samirAccount.printInfo();
	ritaAccount.printInfo();
	cout << "***********************************" << endl << endl;

	system("pause");
	return 0;
}


When I try to run the full tester all of a sudden a ton of errors such as undefined or undeclared variables. I am not sure why if I am just leaving something out or if it has to do with the fact that SavingAccount.h and CheckingAccount.h are both derived from BankAccount.h and I need to add something for it or something. Any ideas on why this is happening or a direction I should go in?


I got help with it, I needed guards on my BankAccount.h and to move them to the end of the file instead of just the end of the class. Thank you though :)
Hi,

I got help with it, I needed guards on my BankAccount.h and to move them to the end of the file instead of just the end of the class. Thank you though :)


It doesn't sound quite right to me, instead you need to have separate files. The class declaration goes into a header file (I prefer .hpp rather than .h, but that's just my preference), and the class function definitions go into a .cpp file.

That way header files can be included wherever they are needed - in multiple cpp files where required.

As in :

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
#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H
using namespace std;             // *** try to avoid doing this

class CheckingAccount : public BankAccount
{
public:
	CheckingAccount(double balance);

	
	//CheckingAccount(double balance);
	void set_interestRate(double rate);
	double get_interestRate();
	void set_minBalance(double minBal);
	double get_minBalance();
	void set_fee(double fee);
	double get_fee();
	void postInterest();
	bool checkMin();
	void withdrawl(double amount);
	void writeCheck(double amount);
	void printInfo();
	//void CheckingAccount::printInfo()
	



protected:
	double _rate, _minBalance, _fee;
	


};
#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
#include <iostream>
#include <iomanip>
#include "BankAccount.h"

// using namespace std;             // *** don't have this in a header file


CheckingAccount::CheckingAccount(double balance)//Since the tester that was provided doesnt set the rate, min balance or fee I just set it here
	: BankAccount(balance)
{
	//_number = get_actNumber();
	_fee = 15;
	_minBalance = 200;
	_rate = 0.04;
}

void CheckingAccount::set_interestRate(double rate)
{
	_rate = rate;
}
double CheckingAccount::get_interestRate()
{
	return _rate;
}
void CheckingAccount::set_minBalance(double minBal)
{
	_minBalance = minBal;
}
double CheckingAccount::get_minBalance()
{
	return _minBalance;
}
bool CheckingAccount::checkMin()
{
	if (_balance >= _minBalance)
	{
		return true;
	}
	else
		return false;
}
void CheckingAccount::set_fee(double fee)
{
	_fee = fee;
}
double CheckingAccount::get_fee()
{
	return _fee;
}
void CheckingAccount::postInterest()
{
	_balance += (_balance * _rate);
}
void CheckingAccount::withdrawl(double amount)
{
	if (_balance < amount)
	{
		cout << "insufficient funds. Withdrawl rejected, does not affect balance." << endl;
	}
	else//I assume the bank will allow a withdrawl as long as the balance is greater than the amount even if the fee drops the account into the negative
	{
		if (_balance - amount >= _minBalance)
		{
			_balance = _balance - amount;
		}
		else
		{
			_balance -= (amount + _fee);
			
		}

	}
}
void CheckingAccount::writeCheck(double amount)
{
	withdrawl(amount);
}
void CheckingAccount::printInfo()
{
	cout << "Interest Checking ACCT#:" << get_actNumber() << "                    Balance: $" << setprecision(2) << fixed << get_balance() << endl;
}


I have just cut & paste your code, none of it is tested - hopefully you get the idea of what I am saying.

It's good practise for a constructor initialiser list to set values for all the class data members. Also, derived classes initialiser lists should call base class constructors so that all their data is set.

Try to avoid having get / set functions for everything, this program doesn't seem to need them - values are set once. So no need for the set functions

Get functions aren't needed by class member functions, as they have direct access to all class members. So in the PrintInfo function, you don't need to call a get function to access the class data.

With the variable & function names, try to avoid abbreviating everything - longer names are often clearer. Try to incorporate the exact variable name in a function that uses it. For example not get_actNumber() with _number

Protected data members are also often not a good idea. Instead a class has an interface of public functions that access private data. Note that this doesn't mean get / set functions for everything - only provide functions for what is needed. You can make a function that has a reference to a class object as an argument, then that function can access all the public functions that belong to that class.

With static int _number; , because it is static , there is only 1 value across all objects of that class. I am fairly sure this is not what you want. You could have a static variable (in the BankAccount class) that stores the last account number allocated.

With the BankAccount::get_actNumber() it adds 100 to the account number every time it is called, so if it is called multiple times for 1 account the account number changes. Combined with the previous point about being static, that makes for quite a bit of confusion. One shouldn't rely on a function only being called once.

Any way I hope all this helps a bit :+)

Edit:

If you are going to have a get function,then it should only return the value, not do something else to it.
Last edited on
Topic archived. No new replies allowed.