运用C++语言描述书籍类、图书类型类、图书类,每一个类应包含数据成员和成员函数。设计基类和派生类,并运用多态性和虚函数的知识。本课程设计要实现的主要功能如下: 1)建立书籍类、图书类型类、图书类 2)书籍、图书类型、图书信息的初始化 3)书籍、图书类型、图书信息的添加、修改、删除、查找 4)图书信息的输出 5)对图书信息按编号、书名排序 6)将图书信息保存为文件
时间: 2023-08-09 14:11:35 浏览: 104
C++图书馆管理程序.docx(带有完整源代码,类的声明和定义分开,非常清晰)
好的,我可以为您解答这个问题。首先,我们需要定义三个类:书籍类(Book)、图书类型类(BookType)和图书类(Library)。其中,Book类和BookType类都是Library类的派生类。
Book类的数据成员包括:书名、作者、出版社、出版日期、价格和编号。成员函数包括:构造函数、析构函数、设置和获取书名、作者、出版社、出版日期、价格和编号的函数。
BookType类的数据成员包括:类型名称和类型编号。成员函数包括:构造函数、析构函数、设置和获取类型名称和类型编号的函数。
Library类的数据成员包括:书籍的数量、书籍数组、图书类型数组。成员函数包括:构造函数、析构函数、添加、修改、删除、查找图书信息的函数、按编号、书名排序的函数以及将图书信息保存为文件的函数。
在Book和BookType类中,我们可以使用虚函数来实现多态性。例如,在Book类中定义一个虚函数PrintInfo(),在Book的派生类中重写这个函数以输出不同的信息。
下面是一个示例代码:
```c++
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
class BookType
{
public:
BookType(string name, int id);
virtual ~BookType();
void SetName(string name);
string GetName();
void SetId(int id);
int GetId();
private:
string m_name;
int m_id;
};
BookType::BookType(string name, int id)
{
m_name = name;
m_id = id;
}
BookType::~BookType()
{
}
void BookType::SetName(string name)
{
m_name = name;
}
string BookType::GetName()
{
return m_name;
}
void BookType::SetId(int id)
{
m_id = id;
}
int BookType::GetId()
{
return m_id;
}
class Book
{
public:
Book(string name, string author, string publisher, string date, double price, int id, BookType* type);
virtual ~Book();
void SetName(string name);
string GetName();
void SetAuthor(string author);
string GetAuthor();
void SetPublisher(string publisher);
string GetPublisher();
void SetDate(string date);
string GetDate();
void SetPrice(double price);
double GetPrice();
void SetId(int id);
int GetId();
void SetType(BookType* type);
BookType* GetType();
virtual void PrintInfo();
private:
string m_name;
string m_author;
string m_publisher;
string m_date;
double m_price;
int m_id;
BookType* m_type;
};
Book::Book(string name, string author, string publisher, string date, double price, int id, BookType* type)
{
m_name = name;
m_author = author;
m_publisher = publisher;
m_date = date;
m_price = price;
m_id = id;
m_type = type;
}
Book::~Book()
{
}
void Book::SetName(string name)
{
m_name = name;
}
string Book::GetName()
{
return m_name;
}
void Book::SetAuthor(string author)
{
m_author = author;
}
string Book::GetAuthor()
{
return m_author;
}
void Book::SetPublisher(string publisher)
{
m_publisher = publisher;
}
string Book::GetPublisher()
{
return m_publisher;
}
void Book::SetDate(string date)
{
m_date = date;
}
string Book::GetDate()
{
return m_date;
}
void Book::SetPrice(double price)
{
m_price = price;
}
double Book::GetPrice()
{
return m_price;
}
void Book::SetId(int id)
{
m_id = id;
}
int Book::GetId()
{
return m_id;
}
void Book::SetType(BookType* type)
{
m_type = type;
}
BookType* Book::GetType()
{
return m_type;
}
void Book::PrintInfo()
{
cout << "书名:" << m_name << endl;
cout << "作者:" << m_author << endl;
cout << "出版社:" << m_publisher << endl;
cout << "出版日期:" << m_date << endl;
cout << "价格:" << m_price << endl;
cout << "编号:" << m_id << endl;
cout << "类型:" << m_type->GetName() << endl;
}
class Library
{
public:
Library();
~Library();
void AddBook(Book* book);
void ModifyBook(int id);
void DeleteBook(int id);
Book* FindBook(int id);
void SortById();
void SortByName();
void SaveToFile(string filename);
private:
int m_numBooks;
Book* m_books[100];
BookType* m_types[10];
int m_numTypes;
};
Library::Library()
{
m_numBooks = 0;
m_numTypes = 0;
}
Library::~Library()
{
for (int i = 0; i < m_numBooks; i++)
{
delete m_books[i];
}
for (int i = 0; i < m_numTypes; i++)
{
delete m_types[i];
}
}
void Library::AddBook(Book* book)
{
m_books[m_numBooks] = book;
m_numBooks++;
}
void Library::ModifyBook(int id)
{
Book* book = FindBook(id);
if (book != NULL)
{
string name, author, publisher, date;
double price;
BookType* type;
cout << "请输入书名:";
cin >> name;
book->SetName(name);
cout << "请输入作者:";
cin >> author;
book->SetAuthor(author);
cout << "请输入出版社:";
cin >> publisher;
book->SetPublisher(publisher);
cout << "请输入出版日期:";
cin >> date;
book->SetDate(date);
cout << "请输入价格:";
cin >> price;
book->SetPrice(price);
cout << "请选择类型:" << endl;
for (int i = 0; i < m_numTypes; i++)
{
cout << i + 1 << "、" << m_types[i]->GetName() << endl;
}
int choice;
cin >> choice;
if (choice >= 1 && choice <= m_numTypes)
{
type = m_types[choice - 1];
book->SetType(type);
}
else
{
cout << "选择的类型不存在!" << endl;
}
}
else
{
cout << "未找到编号为" << id << "的图书!" << endl;
}
}
void Library::DeleteBook(int id)
{
Book* book = FindBook(id);
if (book != NULL)
{
int index = -1;
for (int i = 0; i < m_numBooks; i++)
{
if (m_books[i]->GetId() == id)
{
index = i;
break;
}
}
if (index != -1)
{
delete m_books[index];
for (int i = index; i < m_numBooks - 1; i++)
{
m_books[i] = m_books[i + 1];
}
m_numBooks--;
}
}
else
{
cout << "未找到编号为" << id << "的图书!" << endl;
}
}
Book* Library::FindBook(int id)
{
for (int i = 0; i < m_numBooks; i++)
{
if (m_books[i]->GetId() == id)
{
return m_books[i];
}
}
return NULL;
}
void Library::SortById()
{
sort(m_books, m_books + m_numBooks, [](Book* a, Book* b) {return a->GetId() < b->GetId(); });
}
void Library::SortByName()
{
sort(m_books, m_books + m_numBooks, [](Book* a, Book* b) {return a->GetName() < b->GetName(); });
}
void Library::SaveToFile(string filename)
{
ofstream fout(filename);
for (int i = 0; i < m_numBooks; i++)
{
fout << m_books[i]->GetName() << " ";
fout << m_books[i]->GetAuthor() << " ";
fout << m_books[i]->GetPublisher() << " ";
fout << m_books[i]->GetDate() << " ";
fout << m_books[i]->GetPrice() << " ";
fout << m_books[i]->GetId() << " ";
fout << m_books[i]->GetType()->GetName() << endl;
}
fout.close();
}
int main()
{
Library lib;
BookType* type1 = new BookType("计算机科学", 1);
BookType* type2 = new BookType("物理学", 2);
BookType* type3 = new BookType("历史", 3);
lib.m_types[0] = type1;
lib.m_types[1] = type2;
lib.m_types[2] = type3;
lib.m_numTypes = 3;
Book* book1 = new Book("C++程序设计", "谭浩强", "清华大学出版社", "2012-09-01", 45.0, 1, type1);
Book* book2 = new Book("计算物理学", "周加村", "高等教育出版社", "2015-06-01", 38.0, 2, type2);
Book* book3 = new Book("明朝那些事儿", "当年明月", "作家出版社", "2005-06-01", 20.0, 3, type3);
lib.AddBook(book1);
lib.AddBook(book2);
lib.AddBook(book3);
while (true)
{
cout << "请选择操作:" << endl;
cout << "1、添加图书" << endl;
cout << "2、修改图书" << endl;
cout << "3、删除图书" << endl;
cout << "4、查找图书" << endl;
cout << "5、按编号排序" << endl;
cout << "6、按书名排序" << endl;
cout << "7、保存为文件" << endl;
cout << "0、退出" << endl;
int choice;
cin >> choice;
if (choice == 1)
{
string name, author, publisher, date;
double price;
BookType* type;
cout << "请输入书名:";
cin >> name;
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入出版日期:";
cin >> date;
cout << "请输入价格:";
cin >> price;
cout << "请选择类型:" << endl;
for (int i = 0; i < lib.m_numTypes; i++)
{
cout << i + 1 << "、" << lib.m_types[i]->GetName() << endl;
}
int typeChoice;
cin >> typeChoice;
if (typeChoice >= 1 && typeChoice <= lib.m_numTypes)
{
type = lib.m_types[typeChoice - 1];
Book* book = new Book(name, author, publisher, date, price, lib.m_numBooks + 1, type);
lib.AddBook(book);
cout << "添加成功!" << endl;
}
else
{
cout << "选择的类型不存在!" << endl;
}
}
else if (choice == 2)
{
int id;
cout << "请输入要修改的图书编号:";
cin >> id;
lib.ModifyBook(id);
}
else if (choice == 3)
{
int id;
cout << "请输入要删除的图书编号:";
cin >> id;
lib.DeleteBook(id);
}
else if (choice == 4)
{
int id;
cout << "请输入要查找的图书编号:";
cin >> id;
Book* book = lib.FindBook(id);
if (book != NULL)
{
book->PrintInfo();
}
else
{
cout << "未找到编号为" << id << "的图书!" << endl;
}
}
else if (choice == 5)
{
lib.SortById();
cout << "按编号排序成功!" << endl;
}
else if (choice == 6)
{
lib.SortByName();
cout << "按书名排序成功!" << endl;
}
else if (choice == 7)
{
string filename;
cout << "请输入要保存的文件名:";
cin >> filename;
lib.SaveToFile(filename);
cout << "保存成功!" << endl;
}
else if (choice == 0)
{
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
这个代码实现了您提出的所有功能,您可以根据自己的需要对其进行修改和扩展。
阅读全文