c++菜鸟,求大神指点。。Vc6.0编译后没错,但就是不能运行。vs2013报错strcpy的问题,但不会改,求指点。。
#include <iostream>#include<cstring>
using namespace std;
class Book
{
private:
int num; //该书的数量
double price; //该书的价格
char * bkname; //该书的名称,取值可能为“algorithm”“c++”等
static double sum; //所有库存书的价格总和
static int counter; //书店库存书的总数
public:
Book() :num(0), price(0){ strcpy(bkname, "no name"); }
Book(int n, double p, char * b); //构造
Book(Book & b); //复制构造
~Book(); //析构函数
void print() const; //输出书的名称、单价和数量(已给出)
void set(int, double); //设置数量、价格(已给出)
void set(char *); //设置书的名称
static double getAveragePrice(); //计算所有库存书的平均价格
friend Book& compare(Book &, Book &); //比较两类书
};//end of class
double Book::sum = 0;
int Book::counter = 0;
Book::Book(int n, double p, char *a){
num = n;
price = p;
if (a)
{
bkname = new char[strlen(a) + 1];
strcpy(bkname, a);
}
else {
bkname = new char[8];
strcpy(bkname, "no name");
}
sum += price;
counter += num;
}
Book::Book(Book & b1)
{
num = b1.num;
price = b1.price;
if (b1.bkname)
{
bkname = new char[strlen(b1.bkname) + 1];
strcpy(bkname, b1.bkname);
}
else {
bkname = new char[8];
strcpy(bkname, "no name");
}
cout << bkname << " copy !!" << endl;
}
Book :: ~Book(){
delete[] bkname;
}
//比较两类书b1和b2(compare(b1,b2)),返回库存量大的书,用友元实现
Book & compare(Book & b1, Book & b2)
{
if (b1.num >= b2.num)
return b1;
else
return b2;
}
double Book::getAveragePrice(){
double AveragePrice;
AveragePrice = sum / counter;
return AveragePrice;
}
void Book::print() const
{
cout << "Book is :" << bkname << "/" << num << "/" << price << endl;
}
void Book::set(char * a) //设置书的名称
{
if (a)
{
bkname = new char[strlen(a) + 1];
strcpy(bkname, a);
}
else {
bkname = new char[8];
strcpy(bkname, "no name");
}
}
void Book::set(int n, double p)
{
num = n; price = p;
}
void main()
{
char nm[] = "c++";
Book b[3] = { Book(60, 32.5, "English"), Book(20, 40.5, nm) };
for (int i = 0; i<3; i++)
{
b[i].print();
}
cout << "average price = " << Book::getAveragePrice() << endl;//输出平均价格
cout << "**********************" << endl;
b[1].set(20, 30.5);
b[2].set(100, 35.5);
b[1].set("Math");
b[2].set("Java");
for (int j = 0; j<3; j++)
{
b[j].print();
}
cout << "average price = " << Book::getAveragePrice() << endl;//输出平均价格
cout << "b[0] compare with b[2],bigger one——";
compare(b[0], b[2]).print();
}
用vc6.0编译时没有报错,但不能运行。
vs2013报错“ error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.”不会改,,求指导。。