【C++】商品类中,定义和实现 构造函数、析构函数
代码演示:
#include < iostream >
#include < cstring >
using namespace std;
class Product
{
public:
Product(char * n, int p, int q);//构造函数
~Product(); //析构函数
void get(int q); //显示剩余产品数量
void buy(int i); //购买商品
private:
char name[20]; //产品名称
int price; //产品单价
int quantity; //剩余产品数量
int buynum; //购买产品数量
int mon;
};
Product::Product(char* n, int p, int q) //构造函数的实现
{
strcpy(name, n);
price = p;
quantity = q;
mon = 0;
}
Product::~Product() //析构函数的实现
{
//cout << “请输入购买产品数量:” << mon << endl;
}
void Product::get(int q)
{
cout << " 品质消费,品质生活!" << endl;
cout << " --------------------------