(E) Library book¶
Goal: I will learn how to implement a small class in C++ and how to use an instance of a class, i.e. an object as an attribute of another class.
Instructions:
Retrieve the code template: templates/03/simple_library/
->
student/03/simple_library/
.
The final program has two simple classes: Date
and Book
.
The former one describes a date, and it is already completed.
Each date has three numeric values: day, month, and year.
These numbers get their values, when a date object is created.
A date can be printed as dd.mm.yyyy
.
A date can be advanced with the method advance
.
The class Date
has a private method is_leap_year
,
telling if the year of the date is a leap year or not.
The method is needed in advancing the date.
It is not meant to be used outside of the class, and thus, it is
declared in the private part.
Your task is to implement the class Book
, which describes a library book.
Each book has a title and an author, as well as the information if the
book has been loaned or not.
If a book is loaned, it has a loaning date and a returning date (due date).
A book can be loaned (unless it is already loaned), it can also be renewed (if it has been previously loaned) and returned. If a book can be loaned, the loaning method creates a loaning date (given as a parameter) and a due date that is 28 days after the loaning date. If a book can be renewed, the renewing method moves the due date 28 days later. (Note that here renewing works in a way, different from that of a real library. In a real library, the new due date is counted by starting from the renewing date, but here it is counted by starting from the old due date.)
The information about a book can be printed in the form: author : title
.
If the book is not loaned, the printing method prints the text available
.
If the book is loaned, the printing method prints the dates when the book has
been loaned and when it should be returned.
More precise printing form can be seen in the example below.
The code template has a main program (in the file main.cpp
).
There you can see how the methods of Book
have been called.
Implement the class Book
which includes the information about
a single book, and all the methods that are called in the main program.
In the main program, there is only one book object, but there could
be more of them.
There is no need to change the class Date
, but you need it in
implementing the class Book
.
Moreover, you can study it to see how a header and implementation files
of a class look like.
After you have implemented the class Book
, the program will work
with the given main program in the following way:
Kivi : Seitseman veljesta
- available
Already loaned: cannot be loaned
Kivi : Seitseman veljesta
- loaned: 05.05.2020
- to be returned: 02.06.2020
Kivi : Seitseman veljesta
- loaned: 05.05.2020
- to be returned: 30.06.2020
Not loaned: cannot be renewed
Kivi : Seitseman veljesta
- available
Kivi : Seitseman veljesta
- loaned: 19.05.2020
- to be returned: 16.06.2020
Tips for completing the assignment:
- Please remember to create the new files in Qt Creator by choosing ”New File or Project.” In the next window, choose ”C++” and ”C++ Class”. Then Qt Creator will automatically take care of including all of the essential files in the compiling process correctly.
- After you have inserted the declaration of the method in the file
book.hh
, go and test the property ”Refactor” -> ”Add Definition in book.cpp”. You can find it by right-clicking the method declaration. - The program will not compile in case the function
main
includes calls for methods you have not yet defined. If you want to be smart and implement your program in parts, you should first create a so-called stub of the class interface that only contains empty definitions of the methods with thereturn
statements you might need. After doing this, you will naturally do a commit to the version control (the stub of the class interface is ready).
A+ presents the exercise submission form here.