Base classes and derived classes.

Hi,
I don't really have a compiler issue, but I know it is something with my code. I am trying to create a base BankAccount object that has 2 derived classes (Checkings and Savings, for this I will just post savings cause solving 1 will probably solve my other problem). I have defined what each functions do but either the main.cpp or the concept is off to me. (The .h file was provided to me by my teacher I wrote a test main.cpp and the derived classes functions).

This is my main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;

int main()
{
    double initial;
    double withdrawing;
    double rate = 0.01;
    int maxwithdrawal = 5;
    cout << "Please enter an initial amount: ";
    cin >> initial;
    cout << "Please enter how much to withdraw: ";
    cin >> withdrawing;
    Savings dog2(initial, rate);
    cout << "Savings: ";
    dog2.printMonthlyStatement();
    cout << endl;
}


This is my base class BankAccount:

1
2
3
4
5
6
7
8
9
10
11
12
13
class BankAccount {
protected:
	double balance;
public:
	BankAccount(double startingBalance)
		{ balance = startingBalance; }
	virtual ~BankAccount() {}
	virtual void deposit(double amt)
		{ balance += amt; }
	virtual void withdraw(double amt)
		{ balance -= amt; }
	virtual void printMonthlyStatement() = 0; // pure virtual function
};


This is my savings derived class:

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
Savings::Savings(double amt, double intRate) : BankAccount(balance) {
    interestRate = amt * intRate;
}

Savings::~Savings()
{
    //dtor
}

void Savings::applyInterest() {
    balance += interestRate;
}

void Savings::deposit(double amt) {
    deposit(amt);
}   // calls deposit from base class

void Savings::withdraw(double amt) {
    if (balance <= 0) {
        exit(0);
        cout << "Your balance is negative!" << endl;
    } else {
        withdraw(amt);
    }
}   // checks if enough funds before calling withdraw from base class

void Savings::printMonthlyStatement() {
    cout << "Your savings balance after all charges is: " << balance;
}// prints monthly statement after calculating the interest to update the balance


So I think my problem is in my printMonthlyStatement I don't have anything in "balance" as I have yet to call the deposit/withdraw functions. But even doing so my program seems to stop responding after the user enters the withdrawn amount.

This is what I feel should be in my printMontlyStatement function (while main just calls the printMonthlyStatement function) so balance can be updated but how would the function know what parameters to use if the user has to enter it?

1
2
3
4
5
6
void Savings::printMonthlyStatement() {
    applyInterest();
    deposit(_____);
    withdraw(_____);
    cout << "Your savings balance after all charges is: " << balance;
}// prints monthly statement after calculating the interest to update the balance 


If anyone can help that would be great, I know its long but I am stuck conceptually on what I need to do. Thank You!
Last edited on
Topic archived. No new replies allowed.