//cashier.cpp 类的实现
#include
#include
#include "cashier.h"
using namespace std;
Cashier::Cashier(char *pname,float pri,int pc)
{ name=new char[strlen(pname)+1];
strcpy(name,pname);
price=pri;
count=pc;
}
Cashier::Cashier(const Cashier & init)
//拷贝构造函数
{ name=new char[strlen(init.name)+1];
strcpy(name,init.name);
price=init.price;
count=init.count;
}
Cashier & Cashier:: operator=(const Cashier & q) //重载赋值运算符
{ delete [] name;
name=new char[strlen(q.name)+1];
strcpy(name,q.name);
price=q.price;
count=q.count;
return *this;
}
Cashier::~Cashier()
//析构函数
{delete [] name;}
void Cashier::input()
//输入成员数据
{ cout>name;
cout>price;
cout>count;
}
float Cashier::total()
//计算总和
{return price*count;
}
void Cashier::print()
//打印输出
{cout.width(10);
cout.fill(' ');
cout.setf(ios::left);
cout
#define n 20
#include "cashier.h"
using namespace std;
void main()
{ Cashier a[n];
//创建Cashier的类对象数组a[n]
int i=0;
int flag=1,tag=1; //设置状态变量
float sum=0.0,pay;
char c;
while(tag&&flag)
{
while((c=getchar())!='\n'||i>pay;
cout<<"找零:"<<pay-sum;
while(tag)
//状态变量tag为真时,执行循环体
{cout<<"按enter键重新开始输入或者按esc键退出";
if((c=getchar())=='\n')
//按enter键重新开始输入,设置状态变量tag=0退出本循环,flag=1则执行外循环,重新输入
{tag=0;flag=1;}
else if((c=getchar())==027)
//按esc键,ASCII码是027.设置状态变量tag=0,flag=0.退出外循环
{tag=0;flag=0;}
else tag=1;
}
}
}
|