~product(){
delete [] name ;
}//析构函数 释放申请的空间
char *name;//产品名称 如果定义成指针,则在使用strcpy之前先要分配指针空间
product::product(char*n,int p,int q)
{
name=new char[strlen(n)+1];//申请存储空间
strcpy(name,n);
price=p;
quantity=q;
}
析构函数没有实现
product::~product()//析构函数
{} // 什么都不用做
#include
using namespace std;
class product
{
char *name;//产品名称
int price;//产品单价
int quantity;//产品剩余数量
public:
product(char *n,int p,int q);//构造函数
//~product();//析构函数
void buy(int money);//购买数量
void get()const;//显示剩余产品数
};
void product::buy(int money)
{
int n;
n=money/price;//购买数量
cout<<"you have buy"<
}
void product::get()const
{
cout<<"there are"<
product::product(char*n,int p,int q)
{
name = (char *)malloc(sizeof(n)); //你定义了指针,应该要给他申请内存空间
strcpy(name,n);
price=p;
quantity=q;
}
void main()
{
product A("medicine",4,20);//单价4,数目20
A.buy(16);
A.get();
}
1.需要默认构造函数和析构函数
2.需要为成员name申请内存
using namespace std;
class product
{
char *name;//产品名称
int price;//产品单价
int quantity;//产品剩余数量
public:
product(){};
product(char *n,int p,int q);//构造函数
~product(){};//析构函数
void buy(int money);//购买数量
void get()const;//显示剩余产品数
};
void product::buy(int money)
{
int n;
n=money/price;//购买数量
cout<<"you have buy"<
}
void product::get()const
{
cout<<"there are"<
product::product(char*n,int p,int q)
{
name = new char[strlen(n)+1];
strcpy(name,n);
price=p;
quantity=q;
}
void main()
{
product A("medicine",4,20);//单价4,数目20
A.buy(16);
A.get();
}