Programming Exercises:
1. Define a class to represent a bank account. Data members include names, accounts
Numbers (using strings) and deposits. member functions perform the following actions:
* Create an object and initialize it;
* Displays the name, account number and deposit of the depositor;
* Depositing the specified deposit in the parameter;
* Take out the specified amount of the parameter;
Write a small program to show all the features.
The procedure is written as follows:
File Bankaccount.h class Bankaccount {Private:char name[40];
Char acctnum[25];
Double balance;
Public:bankaccount (const char* client, const char* num, double bal = 0.0);
void Show (void) const;
void deposit (double cash);
void withdraw (double cash);
}; File Bankaccount.cpp #include "Bankaccount.h" #include <string.h> #include <iostream> Bankaccount::
Bankaccount (const char* client, const char* num, double bal) {strncpy (name, client,39);
Name[39]= ' ";
strncpy (acctnum,num,24);
Acctnum[24]= ' ";
Balance=bal;
} void Bankaccount::show (void) const {using Std::cout;
cout<< "Name:" <<name<< "Acctnum:" <<acctnum<< "Balance:" <<balance<< "\ n";
} void Bankaccount::d eposit (double cash) {using Std::cout; if (cash<0) cout<< "Number of balance deposited can" t be negative.
";
else Balance+=cash;
} void Bankaccount::withdraw (double cash) {using Std::cout; if (cash<0) cout<< "Number of balance withdrawed can" t be negative.
"; else if (cash>balance) cout<< "can ' t withdraw more than you have."
"; File Bankaccount1.cpp #include <iostream> #include <cctype> #include "Bankaccount.h" int main () {using NAMESP
Ace STD;
Bankaccount banka1 ("Zhang San", "24536", 19804);
Banka1.show ();
Double cash1;
Char ch; cout<< want to deposit or withdraw? "<<" Please choose D to deposit, choose W to withdraw money or choose Q to quit.
";
while (Cin>>ch&&toupper (CH)!= ' Q ') {while (Cin.get ()!= ' \ n ') continue;
if (!isalpha (ch)) {cout<< ' \a ';
Continue Switch (CH) {case ' d ': Case ' d ': cout<< ' Please enter the number of Mon
EY you deposit: ";
cin>>cash1;
Banka1.deposit (CASH1); banka1.show ();
Break
Case ' W ': Case ' W ': cout<< ' Please enter the number of your withdraw: ";
cin>>cash1;
Banka1.withdraw (CASH1);
Banka1.show ();
Break
} system ("Pause");
return 0;
}
The results of the program run meet the expected requirements.