c++程序 帮我修改下啊

来源:百度知道 编辑:UC知道 时间:2024/05/20 20:51:47
#include <iostream>
using namespace std;
class product
{
private:
char *name;//显示产品数量;
int price;//产品的价格;
int quantity;//剩余产品数量;
public:
product(char *n,int p,int q);//构造函数;
int buy(int money);
};
product::product(char *n,int p,int q)//构造函数的实现
{
*name=*n;
price=p;
quantity=q;

};
int product::buy(int money)
{
int quantity;
int price;
quantity-=(money/price);
return (quantity);
}

int main()
{
product myproduct("wzx",5,6);
cout<<myproduct.buy(20)<<endl;
return 0;
}
错误显示 是:\Backup\我的文档\第四章 类和对象\3\Cpp1.cpp(24) : warning C4700: local variable 'price' used without having been initialized
D:\Backup\我的文档\第四章 类和对象\3\Cpp1.cpp(24) : warning C4700: local variable 'quantity' used without having been initialized

#include <iostream>
using namespace std;
class product
{
private:
char *name;//显示产品数量;
int price;//产品的价格;
int quantity;//剩余产品数量;
public:
product(char *n,int p,int q);//构造函数;
int buy(int money);
};
product::product(char *n,int p,int q)//构造函数的实现
{
name=n; //这行改成这样
price=p;
quantity=q;

};
int product::buy(int money)
{
// int quantity; //这二行删除掉,因为是对类成员变量操作,而非临时变量,警告问题就出在这里
// int price;
quantity-=(money/price);
return (quantity);
}

int main()
{
product myproduct("wzx",5,6);
cout<<myproduct.buy(20)<<endl;
int i;
cin>>i;
return 0;
}

vc6下运行通过,结果是2.

'price'、 'quantity'
只定义,没被赋值!
调试下,找找位置。

#include <iostream>
using namespace std;
class product
{
private: