Hello. First, here is my code:

class BankAccount
{
private:
	int accountNum;
	double balance;
public:
	bool setAccountNum(int num);
	int getAccountNum() const;
	double getBalance() const;
	void depositMoney(double money);
	bool withdrawMoney(double money);
	void displayAccount() const;
	BankAccount();
};

class SavingsAccount: public BankAccount
{
private:
	int accountNum;
	double sBalance, rate;
public:
	void setInterest(double rates);
	double showInterest() const;
	void postInterest();
	void displayAccount() const;
	SavingsAccount();
};

class CheckingAccount: public BankAccount
{
private:
	int accountNum;
	double balance, rate, MinBal, sCharge;
public:
	void setInterest(double rates);
	void setMinBal(double bal);
	void setServiceCharge(double amount);
	double showServiceCharge() const;
	bool serviceCharge();
	double showInterest() const;
	double showMinBal() const;
	void postInterest();
	bool writeCheck(double amount);
	void displayAccount() const;
	CheckingAccount();
};

Above is prototyping for the classes. Now, the classes can modify member variables besides the accountNum and balance just fine. However, when I try to directly modify the balance of either the Checking or Savings class using member functions respective to those two classes, it won't let me; I can only modify the balance variable using the base class functions. Why is that?

Because data members that are in the private scope are only accessible from the class in which they are declared (or a friend), but not in derived classes.

You must use the protected scope to make the data members of the base class accessible to the derived classes. In other words:

class BankAccount
{
protected:   // NOTICE 'protected' here, not 'private'.
	int accountNum;
	double balance;
public:
	bool setAccountNum(int num);
	int getAccountNum() const;
	double getBalance() const;
	void depositMoney(double money);
	bool withdrawMoney(double money);
	void displayAccount() const;
	BankAccount();
};

See this for more info.

But this is the problem; I'm using member functions from the base class to modify the member variables of the derived classes. In other words, each dervied class has its own balance and I'm trying to modify the dervied class's balance. I can only do this with the base class member functions though; the derived class can't modify its own balance using its own functions. I tried changing the declaration from public to protected and private, but this creates an error in the VC 2010 compliler. What now?

But this is the problem; I'm using member functions from the base class to modify the member variables of the derived classes. In other words, each dervied class has its own balance and I'm trying to modify the dervied class's balance. I can only do this with the base class member functions though; the derived class can't modify its own balance using its own functions.

Why have you duplicated the balance information in the derived classes? Surely an account can only have a single balance?

At both posters, you're right; I was declaring the base class wrong. I realized that, with inheritance, the derived classes inherit everything from the base class including it's variables, and by changing the base class variables to "protected" and removing multiple instances of those variables, everything works peachy as pie now :)

My new code:

class BankAccount //Base BankAccount Class
{
protected: //Member variables for the account number and balance
	int accountNum;
	double balance;
public: //Functions to manipulate above variables
	bool setAccountNum(int num); //Sets account number
	int getAccountNum() const; //Gets account number
	double getBalance(); //Gets balance
	void depositMoney(double deposit); //Deposits money into account
	bool withdrawMoney(double takout); //Withdraws money from account
	void displayAccount(); //Displays account information
	BankAccount(); //BankAccount constructor
};

class SavingsAccount: public BankAccount //SavingsAccount class derived from BankAccount class
{
private:
	double rate; //Variable for interest rate for savings
public:
	void setInterest(double rates); //Sets interest rate for savings
	double showInterest() const; //Shows interest rate for savings
	void postInterest(); //Post interest balance on account for savings
	void displayAccount(); //Display savings account information for savings
	SavingsAccount(); //SavingsAccount constructor
};

class CheckingAccount: public BankAccount //CheckingAccount class derived from BankAccount class
{
private:
	double rate, MinBal, sCharge; //Variables for interest rate, minimum balance and service charge of checking account
public:
	void setInterest(double rates); //Sets interest rate for checking
	void setMinBal(double bal); //Sets minimum balance amount for checking
	void setServiceCharge(double amount); //Sets service charge for checking
	double showServiceCharge() const; //Displays service charge for checking
	bool serviceCharge(); //Assesses service charge on checking account
	double showInterest() const; //Displays interest rate for checking
	double showMinBal() const; //Displays minimum balance amount on checking
	void postInterest(); //Post interest balance on checking
	bool writeCheck(double amount); //Write a check from checking
	void displayAccount(); //Display account information for checking
	CheckingAccount(); //CheckingAccount constructor
};

You could also move the rate variable to the base class (and the member functions for getting and setting it, and postInterest() ), since all accounts have an interest rate of some kind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.