C + + operation, please design a program to realize the management of book inventory (dynamic array class)

[problem description]

Please design a program to realize the management of book inventory. Please complete the design according to the given main function and program output. The specific requirements are as follows.

1, Please design a Book Class:

1. Include private members:

unsigned int m_ID;//number

string m_Name;//title

string m_Introductio//brief introduction

string m_Author;//author

string m_Date;//date

unsigned int m_Page;//the number of pages

2. Design getter and setter functions of all member variables. We have introduced getter and setter in the multi file video course, and students can also learn about them in Baidu.

3. Designing construction and destructors does not require outputting information, but students can output and analyze the creation and deletion of each object by themselves:

Book();// Will m_ The ID is initialized to 0, indicating that this is an unassigned object

virtual ~Book();//No specific work

Book(const Book& other);//Copy all member variables

2, Please design a Store class, which is a dynamic array class to realize book management:

1. Include private members:

Book *m_pBook;//Points to the Book array dynamically created by the new operation

unsigned int m_Count;//Indicates the number of books in stock

2. Design M_ getter and setter functions of count member variables.

3. Design construction and destructor

1) Store();

Will m_ Set count to 0, m_pBook is set to null pointer; And output "Store default constructor called!"

2)Store(int n);

Will m_ Set count to N; Use new to create an array of size n and make m_pBook points to the array; And output "Store constructor with (int n) called!";

3)virtual ~Store();

Will m_ Set count to 0; Judge if m_pBook is not a null pointer, release m_pBook points to space; And output "Store destructor called!";

4)Store(const Store& other);

Implement the deep copy of the object array and output "Store copy constructor called!";

4. Design warehousing operation

The main function of adding a new book to the library is to add a new book to the library.

The function is declared as: void in (book &b)

Note that a new book has been put into storage, so you need to add a storage space. Tip: you can apply for a new space, copy the original data into the new space, put the new book on the last element of the array, and then release the original space, so as to realize the dynamic adjustment of the size of the array.

5. Design issue operation

The main function of stock out operation is to delete this book in the array according to the specified book name.

The function declaration is: void out(string name)

Note that one storage space needs to be reduced because a book is deleted. Tip: you can apply for a new space, copy the undeleted part into the new space, and then release the original space, so as to realize the dynamic adjustment of array size.

6. Find books by ID

It is required to find the book according to the given ID. if it is found, a Book object is returned, in which the information of the corresponding book is stored; If it cannot be found, it returns a Book object, m of the book object_ An ID of 0 indicates that it is not initialized.

The function is declared as: Book findbyID(int ID)

7. Find books by name

It is required to find the book according to the given book name. If it is found, a Book object is returned, in which the information of the corresponding book is stored; If it cannot be found, it returns a Book object, m of the book object_ An ID of 0 indicates that it is not initialized.

The function is declared as: Book findbyName(string name)

8. Design a function to print the information of all books

The function is declared as: void printList()

[input form]

nothing

[output form]

See sample output

[sample output]

Store default constructor called!

The first book is put into storage

The second book is put into storage

The third book is put into storage

Number of books in stock: 3

Find and issue books: Discrete Mathematics

Discrete mathematics has been successfully delivered

Number of books in stock: 2

Find book ID: 3

Find the book with ID 3, Title: c programming

Find book name: Discrete Mathematics

No book with name discrete mathematics was found

Output information of all stock books

There are totally 2 Books:

ID=1; Name:C + + language programming (4th Edition); Author: Zheng Li; Date:201007;

ID=3; Name:c programming; Author: Tan Haoqiang; Date:201006;

End of program operation

Store destructor called!

#include  <iostream> 
using  namespace  std; 
class  Book 
{ 
//Please supplement the definition of Book class here 
private:
	unsigned int m_ID;//number
    string m_Name;//title
    string m_Introductio;//brief introduction
    string m_Author;//author
    string m_Date;//date
    unsigned int m_Page;//the number of pages
public:
	Book();//Will m_ The ID is initialized to 0, indicating that this is an unassigned object
    virtual ~Book();//No specific work
    Book(const Book& other);//Copy all member variables
    
	void SetID(unsigned int id); 
    void SetName(string n); 
    void SetAuthor(string a); 
    void SetIntroduction(string in); 
    void SetDate(string d); 
    void SetPage(unsigned int p);
    
	unsigned int GetID(); 
    string GetName(); 
    string GetAuthor(); 
    string GetIntroduction(); 
   	string GetDate(); 
    unsigned int GetPage(); 	
}; 
//Please supplement the member function implementation of the Book class here 
Book::Book()
{
	m_ID=0;
}

Book::~Book()
{
}

Book::Book(const Book& other)
{
	m_ID=other.m_ID;
	m_Name=other.m_Name;
	m_Introductio=other.m_Introductio;
	m_Author=other.m_Author;
	m_Date=other.m_Date;
	m_Page=other.m_Page;
}
//Set
void Book::SetID(unsigned int id)
{
	m_ID=id;
}
void Book::SetName(string n)
{
	m_Name=n;
} 
void Book::SetAuthor(string a)
{
	m_Author=a;
} 
void Book::SetIntroduction(string in)
{
	m_Introductio=in;
} 
void Book::SetDate(string d)
{
	m_Date=d;
}
void Book::SetPage(unsigned int p)
{
	m_Page=p;
} 
//Get
unsigned int Book::GetID()
{
	return m_ID;
} 
string Book::GetName()
{
	return m_Name;
} 
string Book::GetAuthor()
{
	return m_Author;
}
string Book::GetIntroduction()
{
	return m_Introductio;
} 
string Book::GetDate()
{
	return m_Date;
}
unsigned int Book::GetPage()
{
	return m_Page;
} 
 
class  Store 
{ 
//Please supplement the definition of Store class here 
private:
	Book *m_pBook;//Points to the Book array dynamically created by the new operation
    unsigned int m_Count;//Indicates the number of books in stock
public:
	Store(); //Will m_ Set count to 0, m_pBook is set to null pointer; And output "Store default constructor called!"
    Store(int n);//Will m_ Set count to N; Use new to create an array of size n and make m_pBook points to the array; And output "Store constructor with (int n) called!";
    virtual ~Store();//Will m_ Set count to 0; Judge if m_pBook is not a null pointer, release m_pBook points to space; And output "Store destructor called!"
    Store(const Store& other);//Implement the deep copy of the object array and output "Store copy constructor called!";
    
    int GetCount();
    void SetCount(unsigned int a);
    
    void in(Book &b);
    void out(string name);
    Book findbyID(int ID);
    Book findbyName(string name);
    void printList();
}; 
//Please supplement the member function implementation of Store class here 
Store::Store()
{	
	m_Count=0;
	m_pBook=NULL;
	cout << "Store default constructor called!" << endl;
}
Store::Store(int n)
{
	m_Count=n;
	m_pBook=new Book[n];
	cout << "Store constructor with (int n) called!" << endl;
}
Store::~Store()
{
	m_Count=0;
	
	if(m_pBook!=NULL)
	{
		delete[] m_pBook;
		cout << "Store destructor called!" << endl;
	};
}
Store::Store(const Store& other)
{
	int i;
	m_Count=other.m_Count;
	m_pBook=new Book[m_Count];
	for(i=0;i<m_Count;i++)
	{
		m_pBook[i].SetAuthor( other.m_pBook[i].GetAuthor() );
		m_pBook[i].SetDate  ( other.m_pBook[i].GetDate() );
		m_pBook[i].SetID    ( other.m_pBook[i].GetID() );
		m_pBook[i].SetIntroduction( other.m_pBook[i].GetIntroduction() );
		m_pBook[i].SetName  ( other.m_pBook[i].GetName() );
		m_pBook[i].SetPage  ( other.m_pBook[i].GetPage() );
	}
	cout<<"Store copy constructor called!"<<endl;
}
//get&&set
int Store::GetCount()
{
	return m_Count;
}
// 
void Store::in(Book &b)
{
	int m=m_Count;
	unsigned int i;
	m_Count=m_Count+1;
	Book *Count=new Book[m+1];
	for(i=0;i<m;i++)
	{
		Count[i]=m_pBook[i];
	}  
	delete[] m_pBook;
	Count[m]=b;  
	m_pBook=Count;	
}
void Store::out(string name)
{
	int m=m_Count;
	unsigned int i,k;
	m_Count=m_Count-1;
	Book *Count=new Book[m-1];
	for(i=0,k=0;i<m;i++)
	{
		if(m_pBook[i].GetName()!=name)
		{
			k++;
			Count[k-1]=m_pBook[i];	
		}	
	}  
	delete[] m_pBook;
	m_pBook=Count;	
}
Book Store:: findbyID(int ID)
{
	int i,flag=0;
	for(i=0;i<m_Count;i++)
	{
		if(m_pBook[i].GetID()==ID)
		{
			return m_pBook[i];
			flag=1;
			break;
		}
	}
	
	if(!flag)
	{
		
		return Book();
	}	
}

Book Store:: findbyName(string name)
{
	int i,flag=0;
	for(i=0;i<m_Count;i++)
	{
		if(m_pBook[i].GetName()==name)
		{
			return m_pBook[i];
			flag=1;
			break;
		}
	}
	
	if(!flag)
	{
		Book newb1;
		return newb1;
	}	
}
void Store::printList()
{
	int i;
	cout<<"There are totally " << m_Count << " Books:"<<endl;
	
	for(i=0;i<m_Count;i++)
	{
		cout<<"ID="<<m_pBook[i].GetID()<<";  "<<"Name:"<<m_pBook[i].GetName()<<";  ";
		cout<<"Author:"<<m_pBook[i].GetAuthor()<<";  "<<"Date:"<<m_pBook[i].GetDate()<<";"<<endl;
	}	
}
int  main() 
{ 
        Store  s; 
        Book  b1,b2,b3; 
        b1.SetID(1); 
        b1.SetName("C++  Language programming(4th Edition)"); 
        b1.SetAuthor("Zheng Li"); 
        b1.SetIntroduction("introduce C++And object-oriented knowledge"); 
        b1.SetDate("201007"); 
        b1.SetPage(529); 
        b2.SetID(2); 
        b2.SetName("discrete mathematics "); 
        b2.SetAuthor("Zuo Xiaoling"); 
        b2.SetIntroduction("This paper introduces propositional logic, predicate logic, set theory, algebraic system and graph theory"); 
        b2.SetDate("198209"); 
        b2.SetPage(305); 
        b3.SetID(3); 
        b3.SetName("c Programming"); 
        b3.SetAuthor("Tan Haoqiang"); 
        b3.SetIntroduction("introduce C Basic knowledge of programming, such as sentence format, grammar, etc"); 
        b3.SetDate("201006"); 
        b3.SetPage(355); 

        cout<<"The first book is put into storage"<<endl; 
        s.in(b1); 
        cout<<"The second book is put into storage"<<endl; 
        s.in(b2); 
        cout<<"The third book is put into storage"<<endl; 
        s.in(b3); 
        cout  <<"Number of books in stock:"<<s.GetCount()  <<  endl; 
        cout  <<"Find and issue books: Discrete Mathematics"  <<  endl; 
        Book  tmpbook=s.findbyName("discrete mathematics "); 
        if(tmpbook.GetID()!=0) 
        { 
                s.out("discrete mathematics "); 
                cout  <<"Discrete mathematics has been successfully delivered"  <<  endl; 
        } 
        else 
                cout<<"Can't find name A book for Discrete Mathematics"<<endl; 
        cout  <<"Number of books in stock:"<<s.GetCount()  <<  endl; 

        cout  <<"Find books  ID: 3"  <<  endl; 
        tmpbook=s.findbyID(3); 
        if(tmpbook.GetID()!=0) 
                cout<<"find ID by"<<3<<"My book, Title:"<<tmpbook.GetName()<<endl; 
        else 
                cout<<"Can't find ID by"<<3<<"My book"<<endl; 

        cout  <<"Find books  name: discrete mathematics "  <<  endl; 
        tmpbook=s.findbyName("discrete mathematics "); 
        if(tmpbook.GetID()!=0) 
                cout<<"find name A book for discrete mathematics, ID: "<<tmpbook.GetID()<<endl; 
        else 
                cout<<"Can't find name A book for Discrete Mathematics"<<endl; 

        cout<<"Output information of all stock books"<<endl; 
        s.printList(); 
        cout<<"End of program operation"<<endl; 
        return  0; 
} 

Keywords: C++

Added by NovaHaCker on Sun, 20 Feb 2022 04:28:42 +0200