C++编程语言

来源:百度知道 编辑:UC知道 时间:2024/05/27 04:36:41
以下是图书类Book的定义,但没有类的实现部分,请根据类的定义编写类的实现部分的代码,并编写相应的程序对所定义的类进行测试。
class Book
{
private:
char *name;
char *author;
int sale;
public:
Book(); Book(char *a,char *b,int c);
void print();
~Book();
};

BOOK::Book()
{
name = new char[1];
if(name == NULL)
{
printf("Book Initalizing Failed,Memroy is not enough\n");
}
author = new char[1];
if(author == NULL)
{
printf("Book Initalizing Failed,Memroy is not enough\n");
}

*name = 0;
*author = 0;
sale = 0;
}
BOOK::Book(char *a,char *b,int c)
{
if((a == NULL) || (b == NULL))
{
printf("Inputs are invalid string\n");
}
name = new char[strlen(a) + 1];
if(name == NULL)
{
printf("Book Initalizing Failed,Memroy is not enough\n");
}
author = new char[strlen(b) + 1];
if(author == NULL)
{
printf("Book Initalizing Failed,Memroy is not enough\n");
}
strcpy(name,a);
strcpy(author,b)