写一个个人账户类,里面有一些信息,比如:名字,密码,姓名。然后还有一些处理这些信息的函数,比如:获得名字,密码,性别的函数,和默认构造函数,复制构造函数。以及重载一些运算符,如:加,等于,输出。
程序最好简单一些
急!!!帮忙写个C++程序 (个人账户类)
答案:4 悬赏:40 手机版
解决时间 2021-04-10 18:49
- 提问者网友:箛茗
- 2021-04-10 04:06
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-04-10 04:30
拿2楼的代码改了下,应该满足你的要求。
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class pAccount
{
friend ostream & operator << (ostream &, pAccount &);
public:
pAccount();
pAccount(char *, char *, char s='F', long lM=0); //构造函数
pAccount(const pAccount &); //复制构造函数
~pAccount(); //析构函数
long operator+(const pAccount &);
pAccount & operator=(const pAccount &);
char* GetName() const;
char* GetPassw() const;
char GetSex() const;
long GetMoney() const;
private:
char sName[10];
char sPassw[20];
char cSex;
long iMoney;
};
char* pAccount::GetName() const
{
return (char*)sName;
}
char* pAccount::GetPassw() const
{
return (char*)sPassw;
}
char pAccount::GetSex() const
{
return cSex;
}
long pAccount::GetMoney() const
{
return iMoney;
}
pAccount::pAccount()
:cSex('F'),iMoney(10000000)
{
strcpy(sName, "Honey");
strcpy(sPassw, "111111");
}
pAccount::pAccount(char* name,char* passw,char sex,long im)
{
strcpy(sName,name);
strcpy(sPassw,passw);
cSex = sex;
iMoney = im;
}
pAccount::pAccount(const pAccount &p)
{
strcpy(sName,p.sName);
strcpy(sPassw,p.sPassw);
cSex = p.cSex;
iMoney = p.iMoney;
}
pAccount::~pAccount()
{
// cout<<"over!"<<endl;
}
ostream & operator << (ostream &out, pAccount &pc)
{
out<<pc.sName <<" " << pc.cSex <<" " <<pc.sPassw <<" " <<pc.iMoney <<endl;
return out;
}
long pAccount::operator +(const pAccount& p)
{
return this->iMoney + p.iMoney;
}
pAccount& pAccount::operator = (const pAccount &pc)
{
if(&pc != this)
{
strcpy(sName, pc.sName);
strcpy(sPassw, pc.sPassw);
cSex = pc.cSex;
iMoney = pc.iMoney;
}
return *this;
}
int main()
{
pAccount pc;
cout <<pc <<endl;
pAccount pc1("Lili", "000000", 'F', 100);
cout <<pc1 <<endl;
long iMoney = pc + pc1;
cout << iMoney <<endl;
pc = pc1;
cout <<pc <<endl;
return 0;
}
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
class pAccount
{
friend ostream & operator << (ostream &, pAccount &);
public:
pAccount();
pAccount(char *, char *, char s='F', long lM=0); //构造函数
pAccount(const pAccount &); //复制构造函数
~pAccount(); //析构函数
long operator+(const pAccount &);
pAccount & operator=(const pAccount &);
char* GetName() const;
char* GetPassw() const;
char GetSex() const;
long GetMoney() const;
private:
char sName[10];
char sPassw[20];
char cSex;
long iMoney;
};
char* pAccount::GetName() const
{
return (char*)sName;
}
char* pAccount::GetPassw() const
{
return (char*)sPassw;
}
char pAccount::GetSex() const
{
return cSex;
}
long pAccount::GetMoney() const
{
return iMoney;
}
pAccount::pAccount()
:cSex('F'),iMoney(10000000)
{
strcpy(sName, "Honey");
strcpy(sPassw, "111111");
}
pAccount::pAccount(char* name,char* passw,char sex,long im)
{
strcpy(sName,name);
strcpy(sPassw,passw);
cSex = sex;
iMoney = im;
}
pAccount::pAccount(const pAccount &p)
{
strcpy(sName,p.sName);
strcpy(sPassw,p.sPassw);
cSex = p.cSex;
iMoney = p.iMoney;
}
pAccount::~pAccount()
{
// cout<<"over!"<<endl;
}
ostream & operator << (ostream &out, pAccount &pc)
{
out<<pc.sName <<" " << pc.cSex <<" " <<pc.sPassw <<" " <<pc.iMoney <<endl;
return out;
}
long pAccount::operator +(const pAccount& p)
{
return this->iMoney + p.iMoney;
}
pAccount& pAccount::operator = (const pAccount &pc)
{
if(&pc != this)
{
strcpy(sName, pc.sName);
strcpy(sPassw, pc.sPassw);
cSex = pc.cSex;
iMoney = pc.iMoney;
}
return *this;
}
int main()
{
pAccount pc;
cout <<pc <<endl;
pAccount pc1("Lili", "000000", 'F', 100);
cout <<pc1 <<endl;
long iMoney = pc + pc1;
cout << iMoney <<endl;
pc = pc1;
cout <<pc <<endl;
return 0;
}
全部回答
- 1楼网友:酒醒三更
- 2021-04-10 08:08
#include<iostream>
#include <string>
#include<stdlib.h>
#include<fstream>
using namespace std;
class consumer;
class YH
{
public:
YH();
void set_account();
//银行开户
void del_account();
void transfer(int); void enter_account();
void addmoney(int,float);
void exitYH();
// 退出系统
void functionshow();
void save();
void load();
// 功能界面
protected:
consumer *account[20];
static int acnum;
//账户数
};
int YH::acnum=0;
YH::YH()
{
//for(int i=0;i<20;i++)
//{
//account[i] = NULL;
//}
}
class consumer:public YH
{
public:
friend class YH;
consumer(int id,string Name,string PassWord,float m)
{
ID=id;name=Name;money=m;passwd=PassWord;
}
consumer(){ID=0;name='0';money=0;passwd='0';}
int get_id(){return ID;}
void savemoney();
// 取钱
string get_passwd(){return passwd;}
// 取得密码
void display();
void fetchmoney(); //取钱
void change_passwd();
void add_money(float);
void dec_money(float);
float get_money();
//卡卡转帐
private:
int ID; //开户帐号
string passwd; // 用户密码
string name; // 用户姓名
float money;
};
void YH::save()
{ofstream ofile("bankdat.dat",ios::out);
ofstream outfile("bankdat.dat",ios::out);
int n=0;
outfile<<acnum<<" ";
for(;n<acnum;n++)
{outfile<<account[n]->ID<<" ";
outfile<<account[n]->money<<" ";
outfile<<account[n]->name<<" ";
outfile<<account[n]->passwd<<" ";
}
outfile.close();
}
void YH::load()
{ifstream infile("bankdat.dat",ios::in);
if(!infile)
{cerr<<"读取错误,无资料中!"<<endl;
return;
}
int n=0;
int id,m;
string nam,passw;
infile>>acnum;
for(;n<acnum;n++)
{infile>>id;
infile>>m;
infile>>nam;
infile>>passw;account[n]->passwd;
consumer * acc = new consumer(id,nam,passw,m);
account[n] = acc;
}
infile.close();
cout<<"读取资料正常!"<<endl;
}
void YH::transfer(int x)
{int id;
cout<<"请输入帐号:";
cin>>id;
int flag = 1;
int i = 0;
while((i<acnum)&&(flag))
{
if(id==account[i]->get_id()) flag = 0; else i++;
}
if(flag)
{
cout<<"帐号不存在!"<<endl<<endl;
return ;
}
float b;
cout<<endl<<"请输入你要转帐的金额:";
cin>>b;
while(b<=0)
{
cout<<"请输入正确的数字!"<<endl;
cout<<"$>";
cin>>b;
}
if(account[x]->get_money()<b) cout<<"对不起,金额不够!!"<<endl;
else {account[x]->dec_money(b);account[i]->add_money(b);}
return;
}
void consumer::add_money(float x)
{money=x+money;}
void consumer::dec_money(float x)
{money=money-x;}
void YH::addmoney(int x,float y)
{account[x]->money=account[x]->money-y;
}
float consumer::get_money()
{return money;}
void main()
{YH yh;
yh.functionshow();
}
void YH::functionshow()
{
int n;
do
{system("cls"); load();
cout<<endl<<"请你输入相应的操作序号进行操作:"<<endl;
cout<<"1) 用户开户"<<endl<<"2) 账户登陆"<<endl<<"3) 帐户注销"<<endl<<"4) 退出系统 "<<endl;
cout<<"$>";
cin>>n;
while(n<1||n>4)
{
cout<<"请输入正确的操作序号!"<<endl;
cout<<"$ >";
cin>>n;
}
switch(n)
{
case 1: set_account();
break;
case 2:enter_account();break;
case 3: del_account();
break;
case 4: exitYH();
break;
}
cin.get();
}
while(true);
}
void YH::enter_account()
{int id;
cout<<"请输入帐号:";
cin>>id;
int flag = 1;
int i = 0;
while((i<acnum)&&(flag))
{
if(id==account[i]->get_id()) flag = 0; else i++;
}
if(flag)
{
cout<<"帐号不存在!"<<endl<<endl;
return;
}
cout<<"请输入密码:";
string passw;
cin>>passw;
if(passw!=account[i]->get_passwd()) return;
account[i]->display();cin.get();cin.get();
int n;
do{system("cls");
cout<<"请选择你要进行的操作:"<<endl<<"1)查看信息"<<endl<<"2)取款"<<endl<<"3)存款"<<endl<<"4)修改密码"<<endl<<"5)转账"<<endl<<"6)返回"<<endl;
cin>>n;
switch(n)
{case 1: account[i]->display();break;
case 2: account[i]->fetchmoney();save();break;
case 3:account[i]->savemoney();save();break;
case 4:account[i]->change_passwd();save();break;
case 5:transfer(i);save();break;
case 6:return;
}cin.get();cin.get();
}
while(1);
}
void YH::set_account()
{
int id;
string nam;
string passw;
float m;
cout<<endl<<"请输入开户号:";
cin>>id;
cout<<endl<<"请输入开户人姓名:";
cin>>nam;
cout<<endl<<"请输入开户密码:";
cin>>passw;
cout<<endl<<"请输入存入金额:";
cin>>m;
while(m<=0)
{
cout<<"请输入正确的数字!"<<endl;
cin>>m;
}
consumer * acc = new consumer(id,nam,passw,m);
account[acnum] = acc;
cout<<"开户成功!!"<<endl<<endl;
acnum++;
save();
cin.get();
return;
}
void YH::del_account()
{
int id;
cout<<endl<<"请输入你要注销的帐户号:";
cin>>id;
int flag = 1;
int i = 0;
while((i<acnum)&&(flag))
{
if(id == account[i]->get_id())
{
flag = 0;
}
else
{
i++;
}
}
if(flag)
{
cout<<"帐号不存在!"<<endl<<endl;
return;
}
for(int j=i;j<acnum;j++)
{
account[j] = account[j+1];
}
account[acnum-1]=NULL;
acnum--;
cout<<"注销成功!!"<<endl<<endl;
save();
cin.get();
return;
}
void consumer::change_passwd()
{
string pwd,repwd;
cout<<"请输入新密码:";
cin>>pwd;
cout<<"请再输入一次新密码:";
cin>>repwd;
if(pwd!=repwd)
cout<<"你输入的两次密码不一样,请重新输入!"<<endl;
passwd=pwd;
cout<<"密码修改成功,请牢记!"<<endl;cin.get();
}
void consumer::fetchmoney()
{
float m;
char ch;
do
{
cout<<endl<<"你要取多少钱:"<<"$>"<<endl ;
cin>>m;
while(m<=0)
{
cout<<"请输入正确的数字!"<<endl;
cout<<"$>";
cin>>m;
}
if(money<m)
{
cout<<"对不起,你的余额不足!"
<<endl;
}
else
{money=money-m;
cout<<endl<<"操作成功,请收好钱!"
<<endl;
}
cout<<"是否要继续该项操作:(Y/N) "
<<endl;
cout<<"$ >";
cin>>ch;
while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')
{
cout<<"$ >";
cin>>ch;
}
}while(ch=='y'||ch=='Y');
}
void consumer::savemoney()
{
float c;
char ch;
do
{
cout<<endl<<"你要存多少钱:"<<"$>"<<endl ;
cin>>c;
while(c<=0)
{
cout<<"请输入正确的数字!"<<endl;
cout<<"$>";
cin>>c;
}
money=money+c;
cout<<"操作已成功!"<<endl;
cout<<"是否要继续该项操作:(Y/N) "<<endl;
cout<<"$ >";
cin>>ch;
while(ch!='n'&&ch!='N'&&ch!='Y'&&ch!='y')
{
cout<<"$ >";
cin>>ch;
}
}while(ch=='y'||ch=='Y');
}
void consumer::display()
{system("cls");
cout<<"**********************************"<<endl;
cout<<"*"<<endl;
cout<<"* 用户姓名:"<<name<<endl;
cout<<"* 帐号: "<<ID<<endl;
cout<<"* 余额: "<<money<<endl;
cout<<"**********************************"<<endl;
}
void YH::exitYH()
{
cout<<endl<<"感谢你对本银行储蓄管理系统的支持,欢迎下次光临!"<<endl;
exit(0);
}
- 2楼网友:拾荒鲤
- 2021-04-10 07:03
我也是初学者,你看看吧,可以一起学习。
class pAccount
{
public:
char* GetName();
char* GetPassw();
char GetSex();
pAccount(char*,char*,char); //构造函数
pAccount(const pAccount &); //复制构造函数
~pAccount(); //析构函数
int operator+(const pAccount &);
pAccount & operator=(const pAccount &);
private:
char sName[10];
char sPassw[20];
char cSex;
long iMoney;
};
char* pAccount::GetName()
{
return sName;
}
char* pAccount::GetPassw()
{
return sPassw;
}
char pAccount::GetSex()
{
return cSex;
}
pAccount::pAccount(char* name,char* passw,char sex)
{
strcpy(sName,name);
strcpy(sPassw,passw);
cSex = sex;
}
pAccount::pAccount(const pAccount &p)
{
strcpy(sName,p.sName);
strcpy(sPassw,p.sPassw);
cSex = p.cSex;
}
pAccount::~pAccount()
{
cout<<"over!"<<endl;
}
int pAccount::operator +(const pAccount& p)
{
long sum = 1000;
long totleMoney = sum + p.iMoney;
return totleMoney;
}
pAccount& pAccount::operator = (const pAccount &)
{
return *this;
}
- 3楼网友:長槍戰八方
- 2021-04-10 05:38
又见SB题
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯