I'm trying to prepare a library managament system for my school homework.
(我正在尝试为我的学校作业准备一个图书馆管理系统。)
I wrote the code but in "bool Library::add_book(const Book&)" function, I can not initialize book to dynamic array. (我编写了代码,但是在“ bool Library :: add_book(const Book&)”函数中,我无法将book初始化为动态数组。)
I overloaded = operator to initialize "Book" class and when i declare a "Book" object in main, it works. (我重载了=运算符以初始化“ Book”类,当我在main中声明“ Book”对象时,它起作用了。)
But for dynamic allocated array, it is not working. (但是对于动态分配的数组,它不起作用。)
Thanks everyone for help... (谢谢大家的帮助...)
#include <iostream>
class Author{
public:
std::string name;
Author(){}
Author(const std::string& name){
this -> name = name;
}
};
class Book{
private:
std::string _title, _isbn;
int _edition;
Author *_author = new Author;
public:
Book(){
*_author = Author("NULL");
_isbn = "INVALID";
}
Book(const std::string& title,
const std::string& isbn,
const std::string& author,
int edition){
_title = title;
_isbn = isbn;
_edition = edition;
*_author = Author(author);
}
bool query_isbn(const std::string&);
Book& operator=(const Book&);
friend std::ostream &operator <<(std::ostream &, Book&);
};
class Library{
private:
Book *_book = nullptr;
int *_numberOfBorrows = nullptr;
int _numberOfBook, _maxNumberBook;
public:
Library(int maxNumberBook){
_maxNumberBook = maxNumberBook;
_book = new Book[_maxNumberBook];
_numberOfBorrows = new int[_maxNumberBook];
for(int i = 0; i < _maxNumberBook; i++){
_book[i] = Book();
_numberOfBorrows[i] = 0;
}
}
~Library(){
delete []_book;
_book = NULL;
delete []_numberOfBorrows;
_numberOfBorrows = NULL;
}
bool add_book(const Book&);
void operator+= (const Book&);
const Book& borrow_book(const std::string&);
friend std::ostream& operator <<(std::ostream&, Library&);
};
bool Book::query_isbn(const std::string& isbn){
if(_isbn == isbn)
return true;
else
return false;
}
Book& Book::operator=(const Book& book){
Book ret;
ret._author -> name = book._author -> name;
ret._edition = book._edition;
ret._isbn = book._isbn;
ret._title = book._title;
return ret;
}
std::ostream &operator << (std::ostream& output, Book& obj){
output << obj._author -> name << ", " << obj._title << ", " << obj._isbn << std::endl;
return output;
}
bool Library::add_book(const Book& book){
for(int i = 0; i < _maxNumberBook; i++){
if(_book[i].query_isbn("INVALID")){
_book[i] = book;
_numberOfBook++;
return true;
}
}
return false;
}
void Library::operator+= (const Book& book){
add_book(book);
}
const Book& Library::borrow_book(const std::string& isbn){
for(int i = 0; i < _maxNumberBook; i++){
if(_book[i].query_isbn(isbn)){
_numberOfBorrows[i] = _numberOfBorrows[i] + 1;
return _book[i];
}
}
std::cout << "ISBN with " << isbn << " does not exist" << std::endl;
}
std::ostream& operator <<(std::ostream& output, Library& lib){
for(int i = 0; i < lib._maxNumberBook; i++){
output << lib._book[i];
output << "Borrowed: " << lib._numberOfBorrows[i] << std::endl;
for(int i = 0; i < 38; i++)
output << '=';
std::cout << std::endl;
}
return output;
}
int main(int argc, char** argv){
Library lib(3);
Book obj1("The 8051 Microcontroller & Embedded Systems", "1234-456789123", "Mazidi", 1),
obj2("Fundamentals of Database Systems", "7899-456456123", "Elmasri", 2),
obj3("Electric Circuits", "1478-258963258", "Nilsson", 3);
lib.add_book(obj1);
lib.add_book(obj2);
lib.add_book(obj3);
lib.borrow_book("6584-258963258");
lib.borrow_book("1234-456789123");
lib.borrow_book("7899-456456123");
lib.borrow_book("1478-258963258");
std::cout << lib;
return 0;
}
I want it to output book properties, but it outputs same as below...
(我希望它输出书籍属性,但其输出与以下相同...)
ISBN with 6584-258963258 does not exist
ISBN with 1234-456789123 does not exist
ISBN with 7899-456456123 does not exist
ISBN with 1478-258963258 does not exist
NULL, , INVALID
Borrowed: 0
======================================
NULL, , INVALID
Borrowed: 0
======================================
NULL, , INVALID
Borrowed: 0
======================================
ask by bacimen translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…