【发布时间】:2016-12-08 19:02:41
【问题描述】:
这是我的第一个问题!我试图在 Stroustrup 的《使用 C++ 的原理和实践》一书中解决问题。第9章有三个问题:
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_H
2) 为图书馆创建类 Patron。这个类必须有以下成员:客户的名字,他的卡号和费用的大小。还创建一些函数来设置和从类中获取数据。 我也这样做过:
#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) 创建类库。在其中包括书籍和赞助人的向量。还包括一个结构 Transaction 并创建它的 Vector。创建函数,可以添加有关书籍和客户的数据。如果一个人拿了一本书,图书馆必须知道这个人是否是客户,这本书是否属于它的基金会。
这里有个问题... 这是我的尝试:
#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
我不明白 Patron、Book 和 Library 之间的连接应该是什么样子,以及如何解决这里的最终问题.. 如果有人可以向我解释并编写基本代码,将不胜感激。
【问题讨论】:
-
QString和QVector是什么?确定std::string和std::vector就足够了吗? -
@Bathsheba 也许不是,他可能想要一个 GUI,然后他会使用 Qt
-
是的,QString 和 QVector 等价于 std::Vector 和 std::String。我正在使用 Qt
-
然而,恕我直言,实际的逻辑代码(为了可重用性)不应该依赖于 Qt。我通常尽可能将 GUI 和逻辑分开,并在逻辑中使用 std::* 将 QString 仅用于 GUI
-
感谢您的回复,但我认为 GUI 与否无关紧要,我使用的是 Qt,std 和反之亦然是绝对可以接受的,实际上没有区别,使用哪个标准和无论哪种方式,架构问题仍然存在..
标签: c++ qt architecture