简单的问题:相同的成员函数运行两次,第二次总是不能运行。请高手指点。
程序运行时,编译和连接均无问题,但运行时总是出现如下错误:
Debug error!
DAMAGE:after Normal block(#34)at 0x00430Ec0.
源程序如下,请高手指点一二。
#include<iostream.h>
#include<string.h>
class Product
{
public:
char *name; //产品名称
int price; //产品单价
int quantity; //剩余产品数量
Product(char *n,int p,int q);
~Product();
void buy(int money);
void get()const;
};
Product::Product(char *n,int p,int q)
{
name=new char(strlen(n)+1);
strcpy(name, n);
price=p;
quantity=q;
}
Product::~Product()
{
if (name)
delete []name;
name=0;
}
void Product::buy(int money)
{
int n,r;
n=money/price;
if(n>quantity)
cout<<"数量不够!"<<endl;
else
{
quantity-=n;
r=money%price;
cout<<"产品: "<<name<<" 单价 "<<price<<" 元 顾客";
cout<<money<<"元, 买了"<<n<<"台,剩余"<<r<<"元"<<endl;
}
}
void Product::get()const
{
cout<<"产品: "<<name<<" 单价:"<<price
<<"元 剩余:" <<quantity<<"台\n";
}
void main()
{
Product p1("电视机",2000,15);
p1.buy(7000);
p1.get();
p1.buy(4500);
p1.get();
}