Hello everyone! I've read a lot of articles on DaniWeb and I've decided to join your community!

I have a university project to make a bank account system, which consists of at least 10 account classes, and 3 of them inherit from at least 2 other. The system has 1 base account "Account" and all other classes inherit from it. I'd like to share with you guys my progress and if anyone has any suggestions, please do post and let me know...
So far, my ideas are of classes - Account, SavingsAccount, Salary account, CardAccount, NetworkAccount, SharedAccount(so multiple people can withdraw money) ... and I am out of account ideas for now! If you have any let me know :)

I've just started and I have the Account class defined as follows
Account.h

#include <iostream>
#include "string"
using namespace std;
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account {
public:
    Account();
    int deposit(int);
    int withdraw(int);
    void account_balance()const;


private:
    float balance;
};
#endif

Account.cpp

#include "Account.h"
#include <iostream>
using namespace std;

Account::Account() {balance = 0.0;}

int Account::deposit(int amount){
    balance += amount;
    return balance;
}

int Account::withdraw(int amount){
    balance -= amount;
    return balance;
}
void Account::account_balance()const{
    cout<<balance;
}

And I have a main to test the account class
main.cpp

#include <iostream>
#include "Account.h"

using namespace std;

int main()
{
    Account *acc;
    acc = new Account;
    acc->deposit(23);
    acc->account_balance();
    cout<<endl;
    acc->withdraw(12);
    acc->account_balance();
    return 0;
}

I am wondering now if I need an interest method, to calculate different interest for different accounts?

I will put the implementation of the other classes as soon as possible! I hope if I get into a trouble with inheritance someone will be able to help me out :)

I've implemented the SavingsAccount now too.
SavingsAccount.h

#include "Account.h"
#include <iostream>
#include "string.h"
using namespace std;
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H

class SavingsAccount :virtual public Account {
public:
    SavingsAccount();
    void deposit(int); // for example 1%
    void withdraw(int); // with penalty

};

#endif

SavingsAccount.cpp

#include "SavingsAccount.h"

SavingsAccount::SavingsAccount(){}

void SavingsAccount::deposit(int amount){
    amount += amount/100; // adds 1% interest if you deposit into your savings account
    Account::deposit(amount);
}
void SavingsAccount::withdraw(int amount){
    amount +=amount/100; // 1% of the withdrawn money is added as penalty
    Account::withdraw(amount);
}

and the main I used to test it:
main.cpp

#include <iostream>
#include "Account.h"
#include "SavingsAccount.h"
using namespace std;

int main()
{
    SavingsAccount *acc;
    acc = new SavingsAccount;
    acc->deposit(500);
    acc->account_balance();
    cout<<endl;
    acc->withdraw(200);
    acc->account_balance();
    cout<<endl;
    return 0;
}

However, I have a question here ... i used the keyword virtual, i am not quite sure do I have to use it on the functions themselves or I am allowed to use it for the entire class that I inherit? The confusing part for me is that the functions in Account are not declared as virtual but I can use them in SavingsAccount just by using virtual public Account?

If you want to save yourself some frustration then don't include this in your header file

using namespace std;

Header files shouldn't expose anything in a namespace.

Edited 2 Years Ago by gerard4143

This article has been dead for over six months. Start a new discussion instead.