创建一个银行的类,要求有用户的姓名,余额,以及存钱,取钱函数的实现。
以下是我的代码:
//text.cpp
#include <iostream>
#include<iomanip>
#include"Account.h"
using namespace std;
int main()
{
int a;
Account k("0001","sunny","2018年10月12日",0);
cout << "客户要存入多少钱\n";
cin >>a;
k.setAccount1(a);
cout << "余额为";
k.getAccount();
cout <<"客户要取多少钱\n";
cin >>a;
k.setAccount2(a);
cout <<"余额为";
k.getAccount();
}
//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include<iostream>
class Account
{
public:
explicit Account(std::string n,std::string na,std::string da,int se);
void setAccount(std::string n,std::string na,std::string da,int se);
void setAccount1(int se);
void setAccount2(int se);
void getAccount();
private:
void counter(int a, int b);
void sub(int a , int b);
std::string number;
std::string name;
std::string date;
int semain;
};
#endif
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
public:
protected:
};
#endif
Account.cpp
#include "Account.h"
#include<iostream>
#include<iomanip>
using namespace std;
Account::Account(string n=0,string na="0",string da="0",int se=0)
{
number=n,name=na,date=da,semain=se;
}
void Account::setAccount(string n,string na,string da,int se)
{
number=n,na=name,da=date,se=semain;
}
void Account::setAccount1(int se)
{
counter(se, semain);
}
void Account::setAccount2(int se)
{
sub(se,semain);
}
void Account::getAccount()
{
cout << semain << endl;
}
void Account::counter(int a, int b)
{
semain=a+b;
}
void Account::sub(int a,int b)
{
if(b-a<0)
{
cout << "取钱超过余额,只能取" << b << endl;
}
else
{
semain=b-a;
}
}
#include "Account.h"