先设计一个基本账户类,再通过继承基本账户类设计一个储蓄账户类,储蓄
账户类中增加密码、地址、最小余额和利率等成员变量,并增加一些银行账户经
常用到的成员函数。要求:
(1) 类中的成员函数具有输入、输出储户上述信息的功能;
(2) 将账号设计成不可更改;
(3) 修改密码时要提供原始密码。
#include
#include
using namespace std;
class Bank
{
private:
30
31
char name[20];//姓名
long id;//账号
long balance;//余额
public:
Bank(char *Name,long Id,long Balance)//const变量需要用初始化表初始化
{
strcpy(name,Name);
id=Id;
balance=Balance;
}
string GetName(){return name;}//获取姓名
long GetId(){return id;}//获取账号
long GetBalance(){return balance;}//获取余额
long cunkuan(long money)//存款
{
balance+=money;
return balance;
}
long qukuan(long money)//取款
{
balance-=money;
return balance;
}
string change_name()//修改姓名
{
char new_name[20];
cout<<"请输入修改后的姓名:"<<endl;
cin>>new_name;
strcpy(name,new_name);
cout<<"修改成功!"<<endl;
return name;
}
};
class SaveBank:public Bank
{
private:
char password[20];//密码
char address[20];//住址
long minimum_balance;//最小余额
32
float annual_interest_rate;//年利率
public:
SaveBank(char *Name,long Id,long Balance,char *Password,char
*Address,long Minimum_balance,float
Annual_interest_rate):Bank(Name,Id,Balance),minimum_balance(Minimum_balance)
,annual_interest_rate(Annual_interest_rate)
{
strcpy(password,Password);
strcpy(address,Address);
}
string GetPassword(){return password;}//获取密码
string GetAddress(){return address;}//获取住址
long Get_minimum_balance(){return minimum_balance;}//获取最小余额
float Get_annual_interest_rate(){return annual_interest_rate;}//获取年利率
string change_password()//修改密码
{
char new_password[20];
char number[20];
cout<<"请输入原始密码:"<<endl;
cin>>number;
if((strcmp(number,password))==0)
{cout<<"请输入新密码:"<<endl;
cin>>new_password;
strcpy(password,new_password);
cout<<"修改成功!"<<endl;
}
else
{cerr<<"原始密码错误!请重新选择操作!"<<endl;}
return password;
}
string change_address()//修改地址
{
char new_address[20];
cout<<"请输入修改后的地址:"<<endl;
cin>>new_address;
strcpy(address,new_address);
cout<<"修改成功!"<<endl;
33
return address;
}
void getbank()//获取客户账户信息
{
cout<<"姓名:"<<GetName()<<endl;
cout<<"账号:"<<GetId()<<endl;
cout<<"余额:"<<GetBalance()<<endl;
cout<<"密码:"<<GetPassword()<<endl;
cout<<"地址:"<<GetAddress()<<endl;
cout<<"最小余额:"<<Get_minimum_balance()<<endl;
cout<<"利率:"<<Get_annual_interest_rate()<<endl;
}
};
(4)添加源文件代码。在 bank.cpp 文件中添加主程序代码,如图 6 所示。
代码如下:
#include "stdafx.h"
#include
#include
#include
#include "bank.h"
using namespace std;
int main(void)
{
char name[20];
long id;
long balance;
char password[20];
char address[20];
long minimum_balance;
float annual_interest_rate;
int op; //操作
long money; //存款 or 取款数额
cout<<"请输入客户信息"<<endl;
cout<<"————————————————————————————————
————————"<<endl;
cout<<"姓名:";
cin>>name;
cout<<"账号:";
cin>>id;
cout<<"余额:";
cin>>balance;
cout<<"密码:";
35
cin>>password;
cout<<"地址:";
cin>>address;
cout<<"最小余额:";
cin>>minimum_balance;
cout<<"年利率:";
cin>>annual_interest_rate;
SaveBank
jianghan(name,id,balance,password,address,minimum_balance,annual_interest_rate);
cout<<"———————————请输入操作
———————————————————————"<<endl;
cout<<"1:打印客户信息"<<endl;
cout<<"2:存款"<<endl;
cout<<"3:取款"<<endl;
cout<<"4:修改密码"<<endl;
cout<<"5:修改姓名"<<endl;
cout<<"4:修改地址"<<endl;
while(cin>>op)
{
switch(op)
{
case 1:
jianghan.getbank();
break;
case 2:
cout<<"请输入存款金额:"<<endl;
cin>>money;
jianghan.cunkuan(money);
cout<<"当前余额:"<<jianghan.GetBalance()<<endl;
break;
case 3:
cout<<"请输入取款金额:"<<endl;
cin>>money;
jianghan.qukuan(money);
cout<<"当前余额:"<<jianghan.GetBalance()<<endl;
break;
case 4:
jianghan.change_password();
break;
case 5:
36
jianghan.change_name();
break;
case 6:
jianghan.change_address();
break;
default:
cerr<<"操作错误!"<<endl;
}
}
return 0;
}
(1)计算月利息
月利息=存款金额×年利率÷12
(2)更改利率(静态方法):重新设置年利率
最后,编写一个程序来测试所设计的储蓄账户类能否实现预定的功能。
#include
#include
using namespace std;
class basic {
public:
long id;
basic(long a):id(a) //基类构造函数
{}
void show(long id) {
cout <<"账号:" << id << endl;
}
~basic() //基类析构函数
{}
};
class account :private basic {
public:
char name[20];
long balance;
char password[20];
char address[20];
long minimum_balance;
float annual_interest_rate;
account(char *a, long b, long c, char *d, char *e, long f, float g): //派生类构造函数
basic(b), balance(c), minimum_balance(f), annual_interest_rate(g)
{
strcpy_s(name, a);
strcpy_s(password, d);
strcpy_s(address, e);
}
void show1(); //声明显示函数
void deposit(long money); //声明存款函数
void withdrawal(long money); //声明取款函数
void change_password(char* chpassword); //声明修改密码函数
void modify_name(char* chname); //声明修改姓名函数
void modification_address(char* chaddress);//声明修改地址函数
~account() //派生类析构函数
{}
};
void account::show1()
{
cout << "姓名:" << name << endl;
basic::show(id);
cout << "余额:" << balance << endl;
cout << "密码:" << password << endl;
cout << "地址:" << address << endl;
cout << "最小余额:" << minimum_balance << endl;
cout << "年利率:" << annual_interest_rate << endl;
}
void account::deposit(long money) {
balance += money;
}
void account::withdrawal(long money) {
balance -= money;
}
void account::change_password(char *chpassword) {
strcpy_s(password, chpassword);
}
void account::modify_name(char* chname) {
strcpy_s(name, chname);
}
void account::modification_address(char* chaddress) {
strcpy_s(address, chaddress);
}
int main() {
char name[20];
long id;
long balance;
char password[20];
char address[20];
long minimum_balance;
float annual_interest_rate;
long money; //存款或取款数额
int a;
cout << "请输入客户信息" << endl;
cout << "---------------------------------" << endl;
cout << "姓名:";
cin >> name;
cout << "账号:";
cin >> id;
cout << "余额:";
cin >> balance;
cout << "密码:";
cin >> password;
cout << "地址:";
cin >> address;
cout << "最小余额:";
cin >> minimum_balance;
cout << "年利率";
cin >> annual_interest_rate;
account n(name, id, balance, password, address, minimum_balance, annual_interest_rate);
cout << "------------------------请输入操作------------------------------" << endl;
cout << "1.打印客户信息" << endl << "2.存款" << endl << "3.取款" << endl;
cout << "4.修改密码" << endl <<"5.修改姓名" << endl << "6.修改地址" << endl;
while (scanf("%d", &a) != EOF) {
switch (a) {
case 1:
n.show1();
break;
case 2:
cout << "请输入存款金额:" << endl;
cin >> money;
n.deposit(money);
cout << "当前余额:" << n.balance <<endl;
break;
case 3:
cout << "请输入取款金额:" << endl;
cin >> money;
n.withdrawal(money);
cout << "当前余额:" << n.balance << endl;
break;
case 4:
char chpassword[20];
cout << "请输入原始密码:" << endl;
cin >> chpassword;
if (strcmp(n.password, chpassword) != 0) {
cout << "原始密码错误!请重新选择操作!" << endl;
}
else {
cout << "请输入新密码:" << endl;
cin >> chpassword;
n.change_password(chpassword);
cout << "修改成功!" << endl;
}
break;
case 5:
char chname[20];
cout << "请输入修改后的姓名:" << endl;
cin >> chname;
n.modify_name(chname);
cout << "修改成功!" << endl;
break;
case 6:
char chaddress[20];
cout << "请输入修改后的地址:" << endl;
cin >> chaddress;
n.modification_address(chaddress);
cout << "修改成功!" << endl;
break;
}
}
return 0;
}