(1) 类中的成员函数具有输入、输出储户上述信息的功能;
(2) 将账号设计成不可更改;
(3) 修改密码时要提供原始密码。
开发工具Clion
Account.h
//
// Created by Dell on 2021/10/18.
//
#ifndef INC_10_SAVEACCOUNT_ACCOUNT_H
#define INC_10_SAVEACCOUNT_ACCOUNT_H
class Account {
protected:
int id;
char name[15];
public:
int money;
Account();
Account(int id);
Account(int id, char *name, int money);
void print();
};
#endif //INC_10_SAVEACCOUNT_ACCOUNT_H
Account.cpp
//
// Created by Dell on 2021/10/18.
//
#include <iostream>
#include "Account.h"
#include <string.h>
using namespace std;
Account::Account() {}
Account::Account(int id, char *name, int money) : id(id), money(money) {
strcpy(name, name);
}
void Account::print() {
cout<<"账号:"<<id<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"余额:"<<money<<endl;
}
Account::Account(int id) : id(id) {}
SaveAccount.h
//
// Created by Dell on 2021/10/18.
//
#ifndef INC_10_SAVEACCOUNT_SAVEACCOUNT_H
#define INC_10_SAVEACCOUNT_SAVEACCOUNT_H
#include "Account.h"
class SaveAccount: public Account{
private:
char password[15];
char address[15];
float rate;
int Minmoney;
public:
SaveAccount();
SaveAccount(int id);
SaveAccount(int id, char *name, int money, char *password, char *address, float rate, int minmoney);
void show();
void repassword(char *repassword);
void inMoney(int m);
void outMoney(int m);
void in0();
void out();
};
#endif //INC_10_SAVEACCOUNT_SAVEACCOUNT_H
SaveAccount.cpp
//
// Created by Dell on 2021/10/18.
//
#include <iostream>
#include "SaveAccount.h"
#include <string.h>
using namespace std;
SaveAccount::SaveAccount() {}
SaveAccount::SaveAccount(int id, char *name, int money, char *password, char *address, float rate, int minmoney)
: Account(id, name, money), rate(rate), Minmoney(minmoney) {
strcpy(address,address);
strcpy(password,password);
}
void SaveAccount::show() {
Account::print();
cout<<"原始密码:"<<password<<endl;
cout<<"地址:"<<endl;
cout<<"利率:"<<rate<<endl;
cout<<"最小余额:"<<Minmoney<<endl;
}
void SaveAccount::repassword(char *repassword) {
cout<<"请输入新密码"<<endl;
cin>>repassword;
strcpy(password,repassword);
}
void SaveAccount::inMoney(int m) {
money +=m;
}
void SaveAccount::outMoney(int m) {
if (m > money) {
cout << "余额不足,无法操作" << endl;
}
if (m <= money) {
money -= m;
}
}
SaveAccount::SaveAccount(int id) : Account(id) {}
void SaveAccount::in0() {
cout<<"请输入姓名"<<endl;
cin>>this->name;
cout<<"请输入余额"<<endl;
cin>>this->money;
cout<<"请输入原始密码"<<endl;
cin>> this->password;
cout<<"请输入地址"<<endl;
cin>>this->address;
cout<<"请输入利率"<<endl;
cin>>this->rate;
cout<<"请输入最小余额"<<endl;
cin>>this->Minmoney;
}
SaveAccount.cpp
#include <iostream>
#include "SaveAccount.h"
using namespace std;
int main() {
SaveAccount zhangsan(1);
zhangsan.in0();
cout<<"请输入操作"<<endl;
cout<<"1.打印信息"<<endl<<"2.存款"<<endl<<"3.取款"<<endl<<"4.修改密码"<<endl;
int n;
cin>>n;
switch (n) {
case 1:{
zhangsan.show();
break;
}
case 2:{
int m;
cout<<"请输入存款金额"<<endl;
cin>>m;
zhangsan.inMoney(m);
zhangsan.show();
break;
}
case 3:{
int m;
cout<<"请输入取款金额"<<endl;
break;
}
case 4:{
char *repassword;
zhangsan.repassword(repassword);
}
}
return 0;
}