Hi ,

I am stuck with my this bank account programming. The question is like this:

Create a base class Bank of a bank account with member functions to allow withdrawal, deposit and calculation of balance. Account should have a name, account number and type.

Create derived class Saving of a saving account, which allows overdraft up to $8000 and gives interest of 2% per annum. Create another derived class Current of a current account, which does not allow overdraft and does not give any interest.

Create two objects of the derived classes with initial deposits of $2000 and $5000 each. Demonstrate the use of various member functions for withdrawal, deposit, interest calculation for one month, showing balance and giving warning when the account balance is zero or less.

The display can be as given below (user inputs are in bold):

Account Name: Jacky Chan(in bold)
Account No: 12345(in bold)
Type: Saving
Balance: $6000
Enter positive amount to deposit and negative to withdraw: $1000(in bold)
Balance: $7000
Interest next month: $11.67

This is what i have done so far:

#include <iostream>
#include <string>
using namespace std;

class Bank
{
private:
	string myname;
	int number;
	double changes;

public:
	void SetBank(string , int , double);
	void EnterName();
	void ShowName(void );
	void EnterNumber();
	void ShowNumber(void );
	void EnterChanges();
	void ShowChanges(void );
};

void Bank::SetBank(string name, int num, double c)
{
	myname  = name;
	number = num;
	changes = c;
}

void Bank::EnterName()
{
	cout<<"Please enter your Account Name: ";
	cin>>myname;
}

void Bank::ShowName()
{
	cout<<"\n\nAccount Name: "<<myname<<endl;
}

void Bank::EnterNumber()
{
	cout<<"Please enter your Account Number: ";
	cin>>number;
}

void Bank::ShowNumber()
{
	cout<<"Account No: "<<number<<endl;
}

void Bank::EnterChanges()
{
	cout<<"Please enter positive amount to deposit and negative to withdraw: $";
	cin>>changes;

}

void Bank::ShowChanges()
{
	cout<<"Enter positive amount to deposit and negative to withdraw: $"<<changes<<endl;
}

void main()
{
	Bank bank;


	bank.EnterName();
	bank.EnterNumber();
	bank.EnterChanges();

	bank.ShowName();
	bank.ShowNumber();
	bank.ShowChanges();


	cout<<"\n\n\n                  ^_^ Thank you for using this program ^_^       "<<endl;
	cout<<"                              Have a nice day         "<<endl;
}

Please >.< i need a detail explanation how the program work ... Thankk in advance

>>i need a detail explanation how the program work
I assum then that you did not write that code? If you had then you would know how it works.

What part(s) do you need help with?

I am sorry for not making my question clear enough .

The part that i need help is :
Create derived class Saving of a saving account, which allows overdraft up to $8000 and gives interest of 2% per annum. Create another derived class Current of a current account, which does not allow overdraft and does not give any interest.

Create two objects of the derived classes with initial deposits of $2000 and $5000 each. Demonstrate the use of various member functions for withdrawal, deposit, interest calculation for one month, showing balance and giving warning when the account balance is zero or less.


By the way the program i wrote only able to display :

Account Name: Jacky Chan
Account No: 12345
Enter positive amount to deposit and negative to withdraw: $1000

I not too sure how to do the saving and current derived class and i need some explanation for it too .

i just updated my program , but i encounter some problem . May i know where have i make the mistake ?

The program:

#include <iostream>
#include <string>
using namespace std;

class Bank
{
private:
	//double balance, overdraft, interest;
	string myname;
	int number;
	double changes;
	double initialbal;
	double endingbal,amount;

public:
	//Bank(double bal , double o , double i )
    // {
	//     balance = bal;
	//     overdraft = o;
	//     interest = i;

    // }
	void SetBank(string , int , double , double , double);
	void EnterName();
	void ShowName(void );
	void EnterNumber();
	void ShowNumber(void );
	void EnterChanges();
	void ShowChanges(void );
	void EnternComputeInitialBal(double );
	void ComputeEndinglBal(void );

};

void Bank::SetBank(string name, int num, double c, double ibal, double ebal)
{
	myname  = name;
	number = num;
	changes = c;
	initialbal = ibal;
	endingbal = ebal;

}

void Bank::EnterName()
{
	cout<<"Please enter your Account Name: ";
	cin>>myname;
}

void Bank::ShowName()
{
	cout<<"\n\nAccount Name: "<<myname<<endl;
}

void Bank::EnterNumber()
{
	cout<<"Please enter your Account Number: ";
	cin>>number;
}

void Bank::ShowNumber()
{
	cout<<"Account No: "<<number<<endl;
}

void Bank::EnterChanges()
{
	cout<<"Please enter positive amount to deposit and negative to withdraw: $";
	cin>>changes;

}

void Bank::ShowChanges()
{
	cout<<"Enter positive amount to deposit and negative to withdraw: $"<<changes<<endl;
}

void Bank::EnternComputeInitialBal(double ibal)
{
	cout<<"Initial balance : $"<<ibal<<endl;
}

void Bank::ComputeEndinglBal()
{
	amount=initialbal+changes;
	cout<<"Ending balance : $"<<amount<<endl;
}

void main()
{
	Bank bank;


	bank.EnterName();
	bank.EnterNumber();
	bank.EnterChanges();

	
	bank.ShowName();
	bank.ShowNumber();
	bank.EnternComputeInitialBal(2000);
	bank.ShowChanges();
	bank.ComputeEndinglBal();

	cout<<"\n\n\n                  ^_^ Thank you for using this program ^_^       "<<endl;
	cout<<"                              Have a nice day         "<<endl;
}

The error i encounter is the bank.ComputeEndinglBal(); ...
the output is : $-9.25596e+061

Thk in advance

It is using uninitialized variables -- initialbal was never set to anything. You need to code a default constructur that initializes all variables to 0 or some other known value. Don't assume the compiler will do this for you because it won't.

#include <iostream>
#include <string>
using namespace std;

class Bank
{
private:
    string myname;
    int number;
    double changes;
    double initialbal;
    double endingbal;

public:
    Bank(string name=" ", int num=0, double c=0, double ibal=0, double ebal=0)
    {
        myname = name;
        number = num;
        changes = c;
        initialbal = ibal;
        endingbal = ebal;
    }
    void EnterName();
    void ShowName(void );
    void EnterNumber();
    void ShowNumber(void );
    void EnterChanges();
    void ShowChanges(void );
    void EnternComputeInitialBal(double );
    void ComputeEndinglBal(double );

};

void Bank::EnterName()
{
    cout<<"Please enter your Account Name: ";
    getline(cin, myname);
}

void Bank::ShowName()
{
    cout<<"\n\nAccount Name: "<<myname<<endl;
}

void Bank::EnterNumber()
{
    cout<<"Please enter your Account Number: ";
    cin>>number;
}

void Bank::ShowNumber()
{
    cout<<"Account No: "<<number<<endl;
}

void Bank::EnterChanges()
{
    cout<<"Please enter positive amount to deposit and negative to withdraw: $";
    cin>>changes;

}

void Bank::ShowChanges()
{
    cout<<"Enter positive amount to deposit and negative to withdraw: $"<<changes<<endl;
}

void Bank::EnternComputeInitialBal(double ibal)
{
    cout<<"Enter inital balance : $"<<ibal<<endl;
}

void Bank::ComputeEndinglBal(double ibal)
{
    endingbal=ibal+changes;
    cout<<"Ending balance : $"<<endingbal<<endl;
}

void main()
{
    Bank bank;


    bank.EnterName();
    bank.EnterNumber();
    bank.EnterChanges();

    
    bank.ShowName();
    bank.ShowNumber();
    cout<<"Type: Saving "<<endl;
    bank.EnternComputeInitialBal(6000);
    bank.ShowChanges();
    bank.ComputeEndinglBal(6000);
    cout<<"Interest next month: $11.67"<<endl;

    cout<<"\n\n\n                  ^_^ Thank you for using this program ^_^       "<<endl;
    cout<<"                              Have a nice day         "<<endl;
}

Sorry ancient dragon ... hopefully this time i can get the code tag works...
Now i had no problem with the initialising of datas , but i am not too sure how to get the derived classes work in my program(as mention in the first post , i need a saving and current derived class) . Any kind person willing to advise me on that ? Thanks in advance :)

Sorry ancient dragon ... hopefully this time i can get the code tag works...
Now i had no problem with the initialising of datas , but i am not too sure how to get the derived classes work in my program(as mention in the first post , i need a saving and current derived class) . Any kind person willing to advise me on that ? Thanks in advance :)

You need to put the

tags around the area of text where your code is


To create a derived class which inherits from Bank is as simple as [CODE=CPP]class saving : public Bank
{
    // saving-specific code in here
};

I had tried that before .. but a error occur when i compile and run the program . I think the problem is because the main function is used once in the "class Bank" , so i cant use the main function again in the "class Saving" . Is there any other way to run the functions that the "class Saving' without using the main function ?

I'm not sure what you're doing there, but your program must have exactly one main() function, and that must stand alone in your program, it cannot form a part of any class.

What error message did you get when you created your saving class?

You need to put the

tags around the area of text where your code is


To create a derived class which inherits from Bank is as simple as [CODE=CPP]class saving : public Bank
{
    // saving-specific code in here
};

Also, I have some general questions about this entire thread:

1) Isn't overkill to have functions defined for EVERY mundane thing such as name and account entry, etc?

2) wouldn't it be simpler if the base class included many of the functions into one all encompasing one like getAccount()?

3)I guess the derived class would satisfy the "Is a" qualification for inheritance in this case? This would only require very minor modification of the base class if I understand the proper application of these things...

Sorry to crash the thread like this, but i thought these questions might help both of us.

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