(Abstract classes) My counter is not working

I am making a program that uses abstract classes, this is an assignment and I have 6 days to finish it. I have seen the problem around and no one is helping no one, I think it's because of the competitive culture...
Now let's get to the problem.
I have made a class ServingChargeChecking that "is a" CheckingAccount that "is a" BankAccount. The purpose of ServingChargeChecking is to have a limited number of checks per month and if the user passes the limit (in my case 4 checks is the limit) then a serviceCharge (static const double serviceCharge = 15.00) is applied to the withdrawed amount from the check. So I have included a counter in ServingChargeChecking that keeps track of the number of checks written. The counter doesn't seem to work right. I have written a test program that sets balance to 1000.10 and writes 6 checks (each withdrawing 10$) since the limit is 4 checks then there are supposed to be 2 serviceCharge added (30$) so I'm supposed to get 910.1 as an output but the serviceCharge is not being applied.... I have provided all the code... The question is: Why is my counter not working? and if it is then: Why aren't the serviceCharge instances not being applied?

BankAccount.h
#pragma once
#include <string>
#include <iostream>

using namespace std;

class BankAccount
{
public:
BankAccount();
void setName(string);
void setAccountNumber(string);
void setBalance(double);
string getName();
string getAccountNumber();
double getBalance();
void deposit(double);
virtual void withdraw(double) = 0;
// virtual void createMonthlyStatements() = 0;
private:
string accountNumber;
string ownerName;
double balance;
};

BankAccount.cpp
#include "BankAccount.h"

BankAccount::BankAccount()
{
accountNumber = "";
ownerName = "";
balance = 0;
}
void BankAccount::setName(string name)
{
ownerName = name;
}
void BankAccount::setAccountNumber(string number)
{
accountNumber = number;
}
// setBalance(int bal)////////////////////////////////////////////////////////////////////////////////
// sets a value for the balance of the account, if the value is set less than 0 then the balance is 0/
void BankAccount::setBalance(double bal)
{
if (bal > 0)
{
balance = bal;
}
else
{
balance = 0;
}

}
string BankAccount::getName()
{
return ownerName;
}
string BankAccount::getAccountNumber()
{
return accountNumber;
}
double BankAccount::getBalance()
{
return balance;
}
//deposit(double depositAmt) if the depositAmt is <= 0 then balance stays the same./
////////////////////////////////////////////////////////////////////////////////////

void BankAccount::deposit(double depositAmt)
{
if (depositAmt > 0)
{
balance = balance + depositAmt;
}
else
{
balance = balance;
}
}
CheckingAccount.h
#pragma once
#include "BankAccount.h"
class CheckingAccount :
public BankAccount
{
public:
void withdraw(double);
virtual void writeCheck(double amount) = 0;
};


CheckingAccount.cpp
#include "CheckingAccount.h"

void CheckingAccount::withdraw(double withdrawAmt)
{
double balance = 0;

if (withdrawAmt <= getBalance())
{
balance = getBalance() - withdrawAmt;
setBalance(balance);
}
else
{
balance = getBalance();
setBalance(balance);
}


}
ServiceChargeChecking.h
#pragma once
#include "CheckingAccount.h"

static const int checkLimit = 4;
static const double serviceCharge = 15.00;

class ServiceChargeChecking :
public CheckingAccount
{
public:
ServiceChargeChecking();
void writeCheck(double);

private:
int checkCount;


};
ServiceChargeChecking.cpp
#include "ServiceChargeChecking.h"


ServiceChargeChecking::ServiceChargeChecking()
{
int checkCount = 0;
}
void ServiceChargeChecking::writeCheck(double checkAmt)
{
if (checkCount <= checkLimit)
{
if (checkAmt <= getBalance())
{
withdraw(checkAmt);
checkCount++;
}
}
else
{
if (checkAmt <= (getBalance() - serviceCharge))
{
double balance = 0;
withdraw(checkAmt);

balance = getBalance() - serviceCharge;
setBalance(balance);
checkCount++;
}
}


}
source.cpp
#include "BankAccount.h"
#include "ServiceChargeChecking.h"

using namespace std;

int main()
{
ServiceChargeChecking SCA;
SCA.setBalance(1000.10);

SCA.writeCheck(10);
SCA.writeCheck(10);
SCA.writeCheck(10);
SCA.writeCheck(10);
SCA.writeCheck(10);
SCA.writeCheck(10);

cout << SCA.getBalance();


return 0;
}


Any additional feedback would be greatly appreciated. This is my first post!
I am ready to be a part of the community.
1
2
3
4
ServiceChargeChecking::ServiceChargeChecking()
{
int checkCount = 0;
}

This creates a local var checkCount with the value 0, which goes out of scope when the ctor finishes.
I guess you wanted to initialize the member var checkCount.
Hi Ponvo, welcome to the forum!

There are two small problems with your code.
First, take a look at your ServiceChargeChecking constructor:

1
2
3
4
ServiceChargeChecking::ServiceChargeChecking()
{
	int checkCount = 0;
}


This is creating a local integer named 'checkCount', and initializing it to zero. This is not the same 'checkCount' counter which belongs to your class, and this local 'checkCount' will cease to exist after the constructor terminates. This also leaves your actual 'checkCount' uninitialized with garbage, causing undefined behavior.

To fix this, simply get rid of the int, like this:
1
2
3
4
ServiceChargeChecking::ServiceChargeChecking()
{
	checkCount = 0;
}


Now, this sets 'checkCount' (the actual counter that belongs to your class) to zero.

Here's the second problem, in your 'writeCheck' member function (I only need to paste the first few lines of it):

1
2
3
4
5
6
7
8
9
10
11
void ServiceChargeChecking::writeCheck(double checkAmt)
{
	if (checkCount <= checkLimit)
	{
		if (checkAmt <= getBalance())
		{
			withdraw(checkAmt);
			checkCount++;
		}
	}
	//... 


The problem is on line 3 of the snippet I posted. Since you're checking whether the counter is less than OR equal to the limit (4), you will actually have a limit of 5, since you're counting from 0 - 4 inclusive.

To fix this, change line 3 from:
if (checkCount <= checkLimit)

to:

if (checkCount < checkLimit)
Last edited on
Thanks! It's working like I planned! now I can continue
Topic archived. No new replies allowed.