### 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
 1234567891011121314151617 #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
 123456789101112131415161718192021222324252627282930313233343536373839404142434445 #include 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
 123456789101112131415161718 #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
 12345678910111213141516171819202122232425262728293031 #include 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<<"\$"<

SavingsAccount.h
 1234567891011121314151617 #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
 1234567891011 #include "SavingsAccount.h" SavingsAccount::SavingsAccount(double initialBalance, double interestRate) :Account(initialBalance){} double SavingsAccount::calculateInterest(double balance, double interestRate) { return balance*interestRate; }

bankAccounts.cpp
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 #include #include 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.