Inheritance

Hi All,
I'm currently in a C++ programming class and have to create a bank account hierarchy using inheritance. The teacher provided us with a template and we had to fill in the blanks. below are the files. The problem I'm having is that when i debug my bankAccounts.cpp, the transaction fee is -9.25etc and so is my interest rate. I'm not sure where the program got that number though?

Account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
public:
	Account(){};
	Account( double b);
	void credit( double );
	bool debit( double );
	void setBalance(double);
	double getBalance();
private:
	double balance;
};

#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
#include <iostream>
using namespace std;

#include "Account.h"


Account::Account( double initialBalance )
{
   if ( initialBalance >= 0.0 )
      balance = initialBalance;
   else
   {
      cout << "Error: Initial balance cannot be negative." << endl;
      balance = 0.0;
   }
}

void Account::credit( double amount )
{
   balance = balance + amount; // add amount to balance
}

bool Account::debit( double amount )
{
   if ( amount > balance ) // debit amount exceeds balance
   {
      cout << "Debit amount exceeded account balance." << endl;
      return false;
   }
   else 
   {
      balance = balance - amount;
      return true;
   }
}

void Account::setBalance( double newBalance )
{
   balance = newBalance;
}

double Account::getBalance()
{
   return balance;
}


CheckingAccount.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CHECKING_H
#define CHECKING_H

#include "Account.h"							

class CheckingAccount : public Account
{
public:
	CheckingAccount( double, double);
	void credit( double );
	bool debit( double );
   
private:

   double transactionFee;
	void chargeFee();
};
#endif 


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

#include "CheckingAccount.h"

CheckingAccount::CheckingAccount(double initialBalance, double transactionFee)
	:Account(initialBalance){}


void CheckingAccount::credit(double amount)
{
	
	Account::setBalance(Account::getBalance() + amount);
	chargeFee();

};

bool CheckingAccount::debit(double amount)
{
	if (Account::debit(amount))
			Account::setBalance(Account::getBalance() - amount);
		chargeFee();
	else break;
}


void CheckingAccount::chargeFee()
{
	Account::setBalance(Account::getBalance()  - transactionFee);
	cout<<"$"<<transactionFee<<" transaction fee charged." <<endl;
}


SavingsAccount.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SAVINGS_H
#define SAVINGS_H

#include "Account.h" 

class SavingsAccount:public Account 
{
public:
	SavingsAccount(double, double);

	double calculateInterest (double, double);
private:

   double interestRate;								
};

#endif 


SavingsAccount.cpp
1
2
3
4
5
6
7
8
9
10
11

#include "SavingsAccount.h"

SavingsAccount::SavingsAccount(double initialBalance, double interestRate)
	:Account(initialBalance){}

   double SavingsAccount::calculateInterest(double balance, double interestRate)
	{
		return balance*interestRate;
	}



bankAccounts.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
#include <iostream>
#include <iomanip>
using namespace std;

#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"

int main()
{
   Account account1( 0.0); // create Account object
   SavingsAccount account2(25.0, .03); // create SavingsAccount object
   CheckingAccount account3(80.0, 1.0); // create CheckingAccount object

   cout << fixed << setprecision( 2 );

   cout << "account1 balance: $" << account1.getBalance() << endl;
   cout << "account2 balance: $" << account2.getBalance() << endl;
   cout << "account3 balance: $" << account3.getBalance() << endl;

   cout << "\nAttempting to debit $25.00 from account1." << endl;
   account1.debit( 25.0 ); // try to debit $25.00 from account1
   cout << "\nAttempting to debit $30.00 from account2." << endl;
   account2.debit( 30.0 ); // try to debit $30.00 from account2
   cout << "\nAttempting to debit $40.00 from account3." << endl;
   account3.debit( 40.0 ); // try to debit $40.00 from account3

   cout << "\naccount1 balance: $" << account1.getBalance() << endl;
   cout << "account2 balance: $" << account2.getBalance() << endl;
   cout << "account3 balance: $" << account3.getBalance() << endl;

   cout << "\nCrediting $40.00 to account1." << endl;
   account1.credit( 40.0 ); // credit $40.00 to account1
   cout << "\nCrediting $65.00 to account2." << endl;
   account2.credit( 65.0 ); // credit $65.00 to account2
   cout << "\nCrediting $20.00 to account3." << endl;
   account3.credit( 20.0 ); // credit $20.00 to account3

   cout << "\naccount1 balance: $" << account1.getBalance() << endl;
   cout << "account2 balance: $" << account2.getBalance() << endl;
   cout << "account3 balance: $" << account3.getBalance() << endl;


   double interestEarned = .90;
   cout << "\nAdding $" << interestEarned << " interest to account2." 
      << endl;

   account2.calculateInterest( account2.getBalance() , interestEarned);

   cout << "\nNew account2 balance: $" << account2.getBalance() << endl;

   system("pause");
}

Last edited on
> the transaction fee is -9.25
You never initialize that variable, so it holds garbage
doesn't it initialize when i create the objects?
When you create an object, it runs the constructor.
You've got no code that sets `transactionFee' value
Unlike Java, C++ will not set default values for any variables. If you don't set it yourself then you'll have garbage there.
use data type "class"
Topic archived. No new replies allowed.