#include <iostream>
#include <string>
#include <assert.h>
#include <climits>
using namespace std;
class Accout
{
protected:
string name;
double money;
string ID;
string Phone;
public:
Accout(string name,
double money=0,
string ID="000",
string Phone="000"):name(name),money(money),ID(ID),Phone(Phone){}
string getname()const{return this->name;}
virtual bool savemoney ()=0;
virtual bool getoutmoney()=0;
virtual bool showAccoutInfo()=0;
Accout *next;
};
class Normal:public Accout
{
public:
Normal(string name,
double money,
string ID,
string Phone):Accout(name,money,ID,Phone){}
virtual bool savemoney ();
virtual bool getoutmoney();
virtual bool showAccoutInfo();
};
bool Normal::savemoney()
{
double m;
cout<<"请输入存款金额"<<endl;
cin>>m;
this->money+=m;
return true;
}
bool Normal::getoutmoney()
{
double m;
cout<<"请输入取款金额"<<endl;
cin>>m;
if (m>money) {
cout<<"您的余额不足"<<endl;
}
else
this->money-=m;
return true;
}
bool Normal::showAccoutInfo()
{
cout<<this->name<<" "<<this->money<<" "<<this->ID<<" "<<this->Phone<<endl;
return true;
}
class Vip:public Accout
{
public:
Vip(string name,
double money,
string ID,
string Phone):Accout(name,money,ID,Phone){}
virtual bool savemoney ();
virtual bool getoutmoney();
virtual bool showAccoutInfo();
private:
double already=0;
double left=5000;
};
bool Vip::savemoney()
{
double m;
cout<<"请输入存款金额"<<endl;
cin>>m;
this->money+=m;
return true;
}
bool Vip::getoutmoney()
{
double m;
cout<<"请输入取款金额"<<endl;
cin>>m;
if (m>this->money) {
this->money=0;
already=m-this->money;
left=left-already;
cout<<"您的余额不足,透支金额为:"<<already<<endl;
cout<<"可透支的额度为:"<<left<<endl;
}
return true;
}
bool Vip::showAccoutInfo()
{
cout<<name<<endl<<money<<endl<<ID<<endl<<Phone<<endl<<left<<endl<<already<<endl;
return true;
}
class Bank
{
public:
Bank():head(NULL){}
bool append();
bool deleteUser(const string &name);
void view()const;
Accout* find(const string &name) const;
private:
Accout *head;
};
bool Bank::append()//创建链表
{
Accout *mid;
char a;
string name,ID,Phone;
double money;
cout<<"需要办理VIP请按0,需要办理普通用户请按1"<<endl;
cin>>a;
cout<<"请输入用户名 存入金额 身份证号和手机号码"<<endl;
cin>>name>>money>>ID>>Phone;
if (this->head==NULL)
{
if (a=='1')
{
head=new Normal(name,money,ID,Phone);
head->next=NULL;
}
else
{
head=new Vip(name,money,ID,Phone);
head->next=NULL;
}
}
else
{
mid=head;
while (mid!=NULL) {
mid=mid->next;
//移动指针至表尾
}
if(a=='1')
{
mid=new Normal(name,money,ID,Phone);
mid->next=NULL;
}
else
{
mid=new Vip(name,money,ID,Phone);
mid->next=NULL;
}
}
return true;
}
Accout* Bank::find(const string&name1) const//查找节点
{
Accout *mid;
mid=head;
while (mid->getname()!=name1&&mid!=NULL) {
mid=mid->next;
}
if(mid==NULL)
cout<<"没有找到该用户,请检查用户名是否填写正确"<<endl;
assert(mid!=NULL);
return mid;
}
bool Bank::deleteUser(const string& name2)//删除节点
{
Accout *mid;
mid=head;
if (head->getname()==name2) {
head=head->next;
delete mid;
mid=NULL;
}
else
{
while (mid->next!=find(name2)) {
mid=mid->next;
}
mid->next=find(name2)->next;
delete find(name2);
mid=find(name2);
mid=NULL;
}
return true;
}
void Bank::view()const//遍历链表
{
Accout *mid;
mid=head;
while (mid!=NULL) {
mid->showAccoutInfo();
mid=mid->next;
}
}
int main()
{
char b;
Bank bank;
string user;
while (1) {
cout<<"创建账户请按1"<<endl;
cout<<"存款请按2"<<endl<<"取款请按3"<<endl<<"查看用户信息请按4"<<endl<<"查看所有用户信息请按5"<<endl<<"销户请按6"<<endl;
cin>>b;
//cin.ignore(numeric_limits::max());
switch (b) {
case '1':
bank.append();
break;
case '2':
cout<<"请输入用户名"<<endl;
cin>>user;
bank.find(user)->savemoney();
break;
case '3':
cout<<"请输入用户名"<<endl;
cin>>user;
bank.find(user)->getoutmoney();
break;
case '4':
cout<<"请输入用户名"<<endl;
cin>>user;
bank.find(user)->showAccoutInfo();
break;
case '5':
bank.view();
break;
case '6':
cout<<"请输入用户名"<<endl;
cin>>user;
bank.deleteUser(user);;
break;
default:
break;
}
}
return 0;
}
对抽象类的认识不够。 只能用于被继承而不能直接创建对象的类设置为抽象类(Abstract Class)。 之所以要存在抽象类,最主要是因为它具有不确定因素。我们把那些类中的确存在,但是在父类中无法确定具体实现的成员函数称为纯虚函数。纯虚函数是一种 特殊的虚函数,它只有声明,没有具体的定义。抽象类中至少存在一个纯虚函数;存在纯虚函数的类一定是抽象类。存在纯虚函数是成为抽象类的充要条件。