I am also new to C++, but here are some of my suggestions to help you out.
In my example I was using VS C++.Net and had to turn off the ?Using the Precompiled headers?.
(Thanks to someone on AnandTech)
Right click on your project in the Solution Explorer.
Click on Properties
Go to the C/C++ item
Go to precompiled headers there.
1) The test driver is in its own .cpp file. In the end you should be able to create:
SavingsAccount my_savings(....<you data here>....);
2) Every class should have it's own .h and .cpp file. An exception to this rule is (for portability sake) when you are designing a template class where you will put the member class back into the .h file.
For your example you should have the following files:
BankAccount.h
BankAccount.cpp
SavingsAccount.h
SavingsAccount.cpp
TestDriver.cpp
3) Use ifndef/define/endif so the definition of the class gets called only once.
#ifndef <some_name_h>
#define<some_name_h>
class some_name
{
public:
protected:
private:
}
#endif //#ifndef <some_name_h>
4) BankAccount should hold all of the common elements to all types of accounts. It has different rules than a SavingsAccount, but still has all of the parts that are common in the base class BankAccount .
5) Your SavingsAccount is derived from the BankAccount. Your line "SavingsAccount : BankAccount" should be "SavingsAccount : public BankAccount"
6) The constructor for SavingsAccount needs all variables for the base class BankAccount since the constructor for the base class is called before the derived class. See my example.
7) Use virtual functions in the base class BankAccount so you can overload functions that are in common in name (See Display and Apply) but may differ in function. What if you add another type of account like a checking account or money market account with different rules? A
8) In BankAccount the variables should be protected not public.
9) Here is what I did for my C++ class?I am using an advance way to initialize the variables in the consrutors. See my note in SavingsAccount.
#ifndef bankaccount_h
#define bankaccount_h
class BankAccount
{
friend std :: ostream& operator << ( std :: ostream& os, const
BankAccount& rhs);
public:
BankAccount(int account_id, const Money& balance);
virtual ~BankAccount(void);
bool operator == (const BankAccount& rhs) const;
bool operator != (const BankAccount& rhs) const;
bool BelongsTo (const Transaction& transaction) const;
virtual void Display( std :: ostream& os ) const;
virtual bool Apply( const Transaction& transaction ) = 0;
protected:
int account_id; // Account id
Money balance; // Current amount of account (money)
};
#endif // #ifndef bankaccount_h
BankAccount :: BankAccount(int accountId, const Money& balanceBA):
account_id(accountId),
balance(balanceBA)
{
};
BankAccount :: ~BankAccount(void)
{
};
#ifndef savingsaccount_h
#define savingsaccount_h
#include ?BankAccount.h?
class SavingsAccount : public BankAccount
{
public:
SavingsAccount(int account_id, const Money& balance, const Date&
last_withdrawl);
~SavingsAccount(void);
virtual bool Apply (const Transaction& transaction);
private:
Date last;
};
#endif // #ifndef savingsaccount_h
SavingsAccount :: SavingsAccount(int accountId, const Money& balanceCA, const Date& last_withdrawl):
BankAccount(accountId, balanceCA),
last(last_withdrawl)
{
// No code
// Instead of last (last_withdrawl) I could of used the following
// within this space.
// last = last_withdrawl
};
SavingsAccount :: ~SavingsAccount(void)
{
};
#ifndef Checking_Account_h
#define Checking_Account_h
class CheckingAccount : public BankAccount
{
public:
CheckingAccount(int account_id, const Money& balance, const Money& overdraft_limit);
~CheckingAccount(void);
virtual bool Apply (const Transaction& transaction);
private:
Money overdraft_limit; //Maximum allowed negative balance
};
#endif //infndefined Checking_Account_h
CheckingAccount :: CheckingAccount(int accountId, const Money& balanceCA, const Money& over_limit):
BankAccount(accountId, balanceCA),
overdraft_limit(over_limit)
{
};
CheckingAccount :: ~CheckingAccount(void)
{
};