这是我的第一个问题!我试图解决Stroustrup的“使用C++的原则和实践”一书中的问题。在第九章中有三个问题:
1)创建类书,这将是图书馆程序软件的一部分。下列成员必须是: ISBN号码、作者姓氏、作者姓名和注册日期。创造功能..。
我已经这样做了,头文件:
#ifndef BOOK_H
#define BOOK_H
#include <QString>
#include <iostream>
using std::ostream;
class Book
{
public:
Book();
/*
prevent datacheck
generate constructor*/
bool isGiven() const;
int get_ISBN() const;
void set_ISBN(const int &isbn_); // ? right format ?
void give_in_hands(const bool &given_);
void set_surname_author(const QString &value);
void set_name_author(const QString &value);
QString get_surname_author() const;
QString get_name_author()const;
void set_date(const struct Date a);
int get_date(int y);
bool operator==( const Book& b);
// ostream& operator<<(ostream& os);
enum Genre{
novel,ski,horror,comedy
};
Genre get_genre( );
private:
int ISBN;
QString surname_author;
QString name_author;
/* // declare a structure for date ?
int year;
int day;
enum months{
jan=1,feb,mar,apr,may,jun,jul,avg,sen,okt,nov,dec
};*/
bool given;
int year;
int day;
int month;
};
struct Date{
int year;
int day;
/* enum months{
jan=1,feb,mar,apr,may,jun,jul,avg,sen,okt,nov,dec
};*/
int month;
Date(int year_,int day_, int month_){
year=year_;
day=day_;
month = month_;
}
};
#endif // BOOK_H2)为图书馆创建班级赞助人。这个班必须有以下成员:客户姓名、卡号和费用大小。还创建一些函数来设置和获取类中的数据。我也这样做了:
#ifndef PATRON_H
#define PATRON_H
#include <QString>
class Patron
{
QString name;
int cardNumber;
int fee;
bool Paid_a_fee;
public:
Patron();
//did a person pay a fee or not
void set_Pay_or_not(const bool& a);
bool did_pay_or_not() const;
int getCardNumber() const;
void setCardNumber(int value);
int getFee() const;
void setFee(int value);
};
#endif // PATRON_H问题在第三个。
3)创建类库。其中包括书籍的载体和赞助者。还包括一个结构事务和创建its的向量。创建函数,它可以添加有关书籍和客户端的数据。如果一个人拿走了一本书,图书馆就必须知道,是否有人是客户,这本书是否属于它的基础。
我有个问题..。这是我的尝试:
#ifndef LIBRARY_H
#define LIBRARY_H
#include <patron.h>
#include <book.h>
#include <QVector>
class Transaction{
Book book_one;
Patron person_one;
};
class Library
{
public:
Library();
QVector <Book> books;
QVector <Patron> patrons;
};
#endif // LIBRARY_H我不明白赞助人、图书和图书馆之间的联系应该是什么样子,以及如何解决这里的最后一个问题。如果有人能向我解释并编写基本代码,这将是非常感谢的。
发布于 2016-08-03 11:52:59
首先,您需要创建一个struct事务,而不是其他类。事务将很难与库链接,因此代码必须如下所示:
class Library
{
public:
Library();
struct Transaction
{
Book book_one;
Patron person_one;
};
QVector <Book> books;
QVector <Patron> patrons;
QVector <Transaction> transaction; // here your transaction vector
};关于您的书籍和用户的所有信息都是私有的,所以如果是您的图书馆必须管理它,您必须添加设置函数到您的另一个类。Set函数允许其他类修改相关类的数据。例如,在name_author的图书类中:
void Set_name_author(QString name)
{
this.name_author=name;
}对于将要管理的每个变量,您必须这样做。
在库类中,您现在可以添加类似的内容。
Void manage_book(Book myBook; QString name, QString IBSR, QString surname)
{
if(name!=NULL)
{
myBook.set_Name(name);
}
if(IBSR!=NULL)
{
myBook.set_IBSR(IBSR);
}
if(surname!=NULL)
{
myBook.set_Surname(Surname);
}
}稍后,您可以像这样管理现有的书。
myLibrary.manage_book(book1,NULL,NULL,"bob");如果不想更改参数,并且可以从向量中提取book1,则请将Put NULL。赞助人的管理也是一样的。
要知道某人是否是客户端,可以使用与getPaid_a_Fee()相同的逻辑。
如果库需要知道客户端是否拥有(或不拥有)一本书,则向客户端类中添加一个名为book_purchase的布尔值。当他拿起一本书时,用set_.更新布尔值。
希望我能帮你!
发布于 2016-08-03 16:52:22
如果你想要有关这本书的信息,你可以用一个可变的书来代替布尔值,例如"book_purchase“。默认情况下,您可以使用一本名为no_book的书来设置变量,并进行一次由谁来决定一本新书的设置。
在你的课堂客户端
private book_purchase 在你的书中构造函数
this.book_purchase = new book();
this.book_purchase.set_Name("no_book");当您将一本书($here myBook$)归因于客户端时,请使用类似的内容
this.set_book_purchase(mybook.get_Name());当图书馆需要知道客户是否拥有一本书时:
client.get_book_purchase();https://stackoverflow.com/questions/38737128
复制相似问题