实验一类和对象(续一)

图书类

描述

以下是图书类Book的声明,缺少实现部分,请实现成员函数并编写main函数测试Book类。

class Book
{
private:
   char *name;                //书名
   char *author;              //作者
   int sale;                  //销售量
public:
   Book();                               //无参构造函数
   Book(char *a, char *b, int c);         //有参构造函数
   Book(const Book &);                   //拷贝构造函数
   void print();                          //显示数据
   ~Book();                              //析构函数
};

输入

在main函数中,我们输入三行数据,第一行是书的名称(长度不超过100,可能含有空格),第二行是作者的名字(长度不超过100,可能含有空格),第三行是销量(整数类型)。

类中有三个对应的成员变量,分别为name,author和sale,利用题目中所给的构造函数来实例化对象,需要注意的是,题目中有三个构造函数,分别是有参构造函数和无参构造函数还有拷贝构造函数。在此我们特别声明:

(1)当输入的name,author和sale都为-1的时候,请使用无参构造函数来实例化对象,此时我们将name的默认值设置为"No name",author的默认值设置为"No author",sale的默认值设置为0.

(2)当输入都为0的时候,我们使用拷贝构造函数来处理,这种情况具体在main函数中的实现是这样的:

  Book bk1;

  Book bk2(bk1);

  bk2.print();

(3)其他情况下一律用有参数的构造函数来构造对象。

输出

使用类中的void print()方法来输出一定格式的字符串,详见样例。

输入样例 1 

The Art of Computer Programming
Donald Ervin Knuth
1000

输出样例 1

Name: The Art of Computer Programming Author: Donald Ervin Knuth Sale: 1000

提示

1、注意输出格式,每个图书的信息占一行,信息的项目之间用\t分隔,最后以\n换行。冒号后面都有一个空格

2、输入书名和作者时,因为会含有空格,请用cin.getline()函数,例如cin.getline(name,sizeof(name))

3、比较两个字符串是否相等,请用strcmp(s1,s2)函数,如果s1=s2,则返回0。例如strcmp(name, "-1"),如果name是“-1”,则返回值为0.

4、请注意,必须要用类(class)来实现代码,否则不得分

实例一(助教)、

	#include <iostream>
#include <cstring>
using namespace std;
 
class Book
{
private:
	char *name;
	char *author;
	int sale;
 
public:
	Book()
	{
		name = new char [100];
		strcpy(name, "No name");
		author = new char [100];
		strcpy(author , "No author");
		sale = 0;
	}
	Book(char *a, char *b, int c)
	{
		name = new char [100];
		strcpy(name, a);
		author = new char [100];
		strcpy(author, b);
		sale = c;
	}
	Book(const Book &B)
	{
		this->name = new char [100];
		strcpy(this->name, B.name);
		this->author = new char [100];
		strcpy(this->author, B.author);
		this->sale = B.sale;
	}
	void print()
	{
		cout << "Name: " << name << "\t"
			<< "Author: " << author << "\t"
			<< "Sale: " << sale << endl;
	}
	~Book()
	{
		delete[] name;
		delete[] author;
		
		if( name != NULL )
			name = NULL;
		if( author != NULL )
			author = NULL;
	}
};
 
int main()
{
	char name[100];
	char author[100];
	int sale;
 
	cin.getline(name, sizeof(name));
	cin.getline(author, sizeof(author));
	cin >> sale;
	
	if( strcmp(name, "-1") == 0 && strcmp(author, "-1") == 0 && sale == -1 )
	{
		Book bk1;
		bk1.print();
	}
	else if( strcmp(name, "0") == 0 && strcmp(author, "0") == 0 && sale == 0 )
	{
		Book bk1;
		Book bk2(bk1);
		bk2.print();
	}
	else
	{
		Book bk1( name, author, sale );
		bk1.print();
	}
 
	return 0;
}

实例二(品学)、

#include<iostream>
#include<cstring>
using namespace std;
 
class Book
{
private:
	char* name;                //书名
	char* author;              //作者
	int sale;                  //销售量
public:
	Book();                               //无参构造函数
	Book(char* a, char* b, int c);         //有参构造函数
	Book(const Book&);                   //拷贝构造函数
	void print();                          //显示数据
	~Book();                              //析构函数
};
Book::Book()
{
	name = new char[100];
	strcpy(name,"No name");
	author= new char[100];
	strcpy(author,"No author");
	sale = 0;
}
Book::Book(char* a, char* b, int c)
{
	name = new char[100];
	strcpy(name,a );
	author = new char[100];
	strcpy(author, b);
	sale = c;
}
void Book::print()
{
	cout << "Name: " << name<<"\t" << "Author: " << author <<"\t"<< "Sale: " << sale << endl;
}
Book::Book(const Book&bk)
{
	name = new char[100];
	strcpy(name, bk.name);
	author = new char[100];
	strcpy(author, bk.author);
	sale = bk.sale;
}
Book::~Book()
{
	//cout << "nothing";
}
int main()
{
		char name[100];
		char author[100];
		int sale;
 
		cin.getline(name, sizeof(name));
		cin.getline(author, sizeof(author));
		cin >> sale;
 
		if (strcmp(name, "-1") == 0 && strcmp(author, "-1") == 0 && sale == -1)
		{
			Book bk1;
			bk1.print();
		}
		else if (strcmp(name, "0") == 0 && strcmp(author, "0") == 0 && sale == 0)
		{
			Book bk1;
			Book bk2(bk1);
			bk2.print();
		}
		else
		{
			Book bk1(name, author, sale);
			bk1.print();
		}
		return 0;
	}

实例三(露婷)、

#include<iostream>
#include<cstring>
using namespace std;
 
class Book
{
private:
	char* name;               
	char* author;              
	int sale;                  
public:
	Book() {
		name = new char[100];
		author = new char[100];
		strcpy(name, "No name");
		strcpy(author,"No author");
		sale = 0;
	}                              
	Book(char* a, char* b, int c) {
		name = new char[100];
		author = new char[100];
		strcpy(name, a);
		strcpy(author, b);
		sale = c;
	}
	Book(const Book&b1) {
		name = new char[100];
		author = new char[100];
		strcpy(name, b1.name);
		strcpy(author, b1.author);
		sale = b1.sale;
	}
	void print() {
		cout << "Name: " << name << "\t" << "Author: " << author << "\t" << "Sale: " << sale << endl;
	}
	~Book() {
		delete []name;
		delete []author;
	}
};
int main() {
	Book b1;
	char xname[100];
	char xauthor[100];
	int xsale;
	cin.getline(xname, sizeof(xname));
	cin.getline(xauthor, sizeof(xauthor));
	cin >> xsale;
	if ((strcmp("-1", xname) == 0) &&( strcmp("-1", xauthor)==0 )&& (xsale == -1))
	{
		Book b2;
		b2.print();
	}
	else if ((strcmp("0", xname) == 0) &&( strcmp("0", xauthor)==0) &&( xsale == 0))
	{
		Book b2(b1);
		b2.print();
	}
	else {
		Book b2(xname, xauthor, xsale);
		b2.print();
	}
	/*delete []xname;
	delete []xauthor;*/
	return 0;
}

实例四(编程天才)、

#include <iostream>
#include <string.h>
using namespace std;
class Book
{
private:
	char *name;
	char *author;
	int sale;
public:
	Book();
	Book(char *a, char *b, int c);
	Book(const Book &obj);
	void print();
	~Book();
};
Book::Book()
{
	name=new char[20];
	author=new char[20];
	strcpy(name,"No name");
	strcpy(author,"No author");
	sale = 0;
}
Book::Book(char *a, char *b, int c)
{
	name=new char[50];
	author=new char[50];
	strcpy(name,a);
	strcpy(author,b);
	sale = c;
}
Book::Book(const Book &obj)
{
	name=new char[100];
	author=new char[100];
	name=obj.name;
	author=obj.author;
	sale=obj.sale;
}
Book::~Book()
{
	delete[] name;
	delete[] author;
}
void Book::print()
{
	cout << "Name: " << this->name << "\t" << "Author: " << this->author << "\t" << "Sale: " << this->sale << "\n";
}
int main()
{
	char name[100];
	char author[100];
	int sale;
	cin.getline(name,sizeof(name));
	cin.getline(author,sizeof(author));
	cin>>sale;
	if (strcmp(name,"-1") == 0 && strcmp(author,"-1") == 0 && sale == -1)
	{
		Book bk1;
		bk1.print();
	}
	else if (strcmp(name,"0")== 0&&strcmp(author,"0") == 0  && sale == 0)
	{
		Book bk1;
		Book bk2(bk1);
		bk2.print();
	}
	else
	{
		Book bk1(name,author,sale);
		bk1.print();
	}
	return 0;
}

实例四(牟)

#include<iostream>
#include<cstring>
using namespace std;
class Book
{
	private:
		char *name;                //书名
		char *author;              //作者
		int sale;                  //销售量
	public:
		Book();                               //无参构造函数
 		Book(char *a, char *b, int c);         //有参构造函数
 		Book(const Book &);                   //拷贝构造函数
 		void print();                          //显示数据
 		~Book();                              //析构函数
 };
 
 Book::Book()  //无参构造函数 
 {
     name=new char[100];
     strcpy(name,"No name");
     author= new char[100];
     strcpy(author,"No author");        
     sale=0;
 }
 
 Book::Book(char *n,char*a,int s)  //有参构造函数 
 {
     name=new char[strlen(n)+1];
     strcpy(name,n);
	 author=new char[strlen(a)+1];
     strcpy(author,a);
     sale=s;
 }
 
 Book::Book(const Book &s)  //拷贝构造函数 
 {
     name=new char[strlen(s.name)+1];
     strcpy(name,s.name);
     author=new char[strlen(s.author)+1];
     strcpy(author,s.author);
     sale=s.sale;
 }
 void Book::print()
 {
     cout<<"Name: "<<name<<"\t"<<"Author: "<<author<<"\t"<<"Sale: "<<sale<<"\n";
 }
 
 Book::~Book()
 {
     delete[] name;
     delete[] author;
 }
 
 int main()
 {
     char n1[101],n2[101];
     int num;
     cin.getline(n1,101);  //getline 的使用 
     cin.getline(n2,101);
	 cin>>num;
     
     if(strcmp(n1,"-1")==0&&strcmp(n2,"-1")==0&&num==-1)
	{
         Book b1;
		 b1.print();
    }
    else if(strcmp(n1,"0")==0&&strcmp(n2,"0")==0&&num==0)
    {
		Book b1;
		Book b2(b1);
		b2.print();
    }
   else 
    {
         Book b1(n1,n2,num); 
		 b1.print();  
    }
   return 0;
}

实例五(宗)

#include<iostream>
#include <iomanip>
#include<cstring>
using namespace std;
class Book
{
private:
   char *name;                //书名
   char *author;              //作者
   int sale;                  //销售量
public:
   Book();                               //无参构造函数
   Book(char *a, char *b, int c);         //有参构造函数
   Book(const Book &);                   //拷贝构造函数
   void print();                          //显示数据
   ~Book();                              //析构函数
};
Book::Book(){
	name=new char[100];
	strcpy(name,"No name");
	author=new char[100];
	strcpy(author,"No author");
	sale=0;
}
Book::Book(char *a,char *b,int c){
	name=new char[strlen(a)];
	strcpy(name,a);
	author=new char[strlen(b)];
	strcpy(author,b);
	sale=c;
}
Book::Book(const Book &m){
	name=new char[strlen(m.name)];
	strcpy(name,m.name);
	author=new char[strlen(m.author)];
	strcpy(author,m.author);
	sale=m.sale;
	
}
void Book::print(){
	cout<<"Name: "<<name<<"\t"<<"Author: "<<author<<"\t"<<"Sale: "<<sale<<"\n";
}
Book::~Book(){
	delete[] name;
	delete[] author;
}
int main(){
	char x[100];
	char y[100];
	int z;
	cin.getline(x,100);
	cin.getline(y,100);
	cin>>z;
	if(strcmp(x,"-1")==0 && strcmp(y,"-1")==0 && z==-1){
		Book bk1;
		bk1.print();
	}
	else if(strcmp(x,"0")==0 && strcmp(y,"0")==0 && z==0){
		Book bk1;
  		Book bk2(bk1);
  		bk2.print();
	}
	else{
		Book bk1(x,y,z);
		bk1.print();
	}
	return 0;
	
}

 实例六(monitor)、

#include<iostream>
#include<cstring>
using namespace std;
class Book
{
	private:
		char *name;                //书名
		char *author;              //作者
		int sale;                  //销售量
	public:
		Book();                               //无参构造函数
 		Book(char *a, char *b, int c);         //有参构造函数
 		Book(const Book &);                   //拷贝构造函数
 		void print();                          //显示数据
 		~Book();                              //析构函数
 };
 
 Book::Book() 
 {
     name=new char[100];
     author= new char[100];
     strcpy(name,"No name");
     strcpy(author,"No author");        
     sale=0;
 }
 
 Book::Book(char *n,char*a,int c) 
 {
    name=new char[strlen(n)];
    author=new char[strlen(a)];
//	cin.getline(n, sizeof(n));
//	cin.getline(a, sizeof(a));
//  不知道为什么不行,没解决,还不会,先换别的
	strcpy(name,n);
    strcpy(author,a);
    sale=c;
 }
 
 Book::Book(const Book &x) 
 {
 	/*name = x.name;
	author = x.author;
	sale = x.sale;
	不对啊!这是指针又不是字符串不能一个样子写啊*/
     name=new char[strlen(x.name)];
     author=new char[strlen(x.author)];
     strcpy(name,x.name);
     strcpy(author,x.author);
     sale=x.sale;
 }
 void Book::print()
 {
     cout<<"Name: "<<name<<"\t"<<"Author: "<<author<<"\t"<<"Sale: "<<sale<<endl;
 }
 
 Book::~Book()
 {
     delete[] name;
     delete[] author;
 }
 
 int main()
 {
     char n[100];
	 char a[100];
     int num;
     cin.getline(n,100);
     cin.getline(a,100);
	 cin>>num;
     
     if(strcmp(n,"-1")==0&&strcmp(a,"-1")==0&&num==-1)
	{
         Book bk1;
		 bk1.print();
    }
    else if(strcmp(n,"0")==0&&strcmp(a,"0")==0&&num==0)
    {
		Book bk1;
		Book bk2(bk1);
		bk2.print();
    }
   else 
    {
         Book bk1(n,a,num); 
		 bk1.print();  
    }
   return 0;
}

实例七(班草)、

	#include<iostream>
#include <string.h>  
 
using namespace std;
 
class Book
{
private:
   char *name;                //书名
   char *author;              //作者
   int sale;                  //销售量
public:
   Book();                               //无参构造函数
   Book(char *a, char *b, int c);         //有参构造函数
   Book(const Book &t);                   //拷贝构造函数
   void print();                          //显示数据
   ~Book();                              //析构函数
};
Book::Book()
{
	name=new char[100];
    strcpy(name,"No name");
    author= new char[100];
    strcpy(author,"No author");        
    sale=0;
}
 
Book::Book(char *a, char *b, int c){
	name=new char[strlen(a)+1];
	strcpy(name,a);
	author=new char[strlen(b)+1];
	strcpy(author,b);
	sale=c;
}
 
Book::Book(const Book &t)
{
	name=new char[strlen(t.name)+1];
	strcpy(name,t.name);
	author=new char[strlen(t.author)+1];
	strcpy(author,t.author) ;
	sale=t.sale;
}
 
void Book::print(){
   	cout<<"Name: "<<name<<"\t"<<"Author: "<<author<<"\t"<<"Sale: "<<sale<<"\n";
   }
   
Book::~Book(){
	delete[] name;
	delete[] author;
} 
   
int main(){
	char name[100];
	char author[100];
	int sale;
	
	cin.getline(name,sizeof(name));
	cin.getline(author,sizeof(author));
	cin>>sale;
	
	if( strcmp(name, "-1") == 0 && strcmp(author, "-1") == 0 && sale == -1 ){
		Book bk1;
		bk1.print();
	}
	else if( strcmp(name,"0") == 0 && strcmp(author,"0") == 0 && sale== 0){
		Book bk1; 
  		Book bk2(bk1);
 		bk2.print();
	}else{
		Book bk1(name,author,sale);
		bk1.print();
	}
	
	return 0;
}

实例八(子桓)、

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Book
{ 
private:
   char *name;                //书名
   char *author;              //作者
   int sale;                  //销售量
public:
   Book();                               //无参构造函数
   Book(char *a, char *b, int c);         //有参构造函数
   Book(const Book &t);                   //拷贝构造函数
   void print();                          //显示数据
   ~Book();                              //析构函数
};
Book::Book(){
	name=new char[100];
	strcpy(name,"No name");
	author=new char[100];
	strcpy(author,"No author");
	sale=0;
} 
Book::Book(char *a, char *b, int c){
	name=new char[100];
	strcpy(name,a);
	author=new char[100];
	strcpy(author,b);
	sale=c;
}
Book::Book(const Book &t){
	name=new char[100];
	strcpy(name,t.name);
	author=new char[100];
	strcpy(author,t.author);
	sale=t.sale;
}
void Book::print(){
	cout<<"Name: "<<name<<"\t"<<"Author: "<<author<<"\t"<<"Sale: "<<sale<<"\n";
}
Book::~Book(){
	delete[] name;
	delete[] author;
}
int main()
{
	char A[100];
	char N[100];
	int s;
	cin.getline(N,sizeof(N));
	cin.getline(A,sizeof(A));
	cin>>s;
	if(strcmp(N, "-1")==0&&strcmp(A, "-1")==0&&s==-1){
		Book bK1;
		bK1.print();
	}
	else if(strcmp(N, "0")==0&&strcmp(A, "0")==0&&s==0){
		Book bK1;
		Book bK2(bK1);
		bK2.print();
	}
	else{
		Book bK1(N,A,s);
		bK1.print();
	}
    return 0;
}

2021/6/29

186717

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值