Lab 1: Principles of Programming
This lab will get you back into the swing of programming with C++. It
will refamiliarize you with the C++ programming language, the Borland
C++ for Windows programming environment, and writing using C++
classes.
You should have taken CS 1 before you attempt this lab.
It may have been a while since you have programmed with C++, so this lab
is intended to reintroduce you to the core C++ concepts that we will be
using throughout the semester. In this lab we will create a C++ project
that will model a library inventory system. To do this, we will write a
main program and a couple of classes.
When writing a program, you should try to plan things out in advance. We
will plan our library inventory program to have eight main commands:
- Add a book to the library
- Remove a book from the library
- Check out a book
- Check in a book
- Print all books owned by the library
- Print all books available for check out
- Save the inventory to a file
- Load the inventory from a file
We will plan our program to use two classes--a library class,
and a book class. This will allow us to separate the functions
and data required for the library as a whole, and the functions and data
used for individual books.
- Copy the project files from
I:\CSC122\public\Lab1
into your
account.
- The project file contains four source code
files--
lab1.cpp
, library.h
, library.cpp
, and
book.h
. The main file contains the skeleton of the program, and
is complete--you won't be changing lab1.cpp
in this lab. The
others are in various states of completeness.
- As it is currently written, the program starts with an empty
library, and you can add books to the library, view the books that are
currently in the library, and load and save the inventory from a file,
but none of the other functions will work correctly. We need to
implement the other parts of the program--we'll start with the ``remove
a book'' part of the program.
The first problem with removing a book is how to determine which book
you want to remove. Do we need to exactly match the book--meaning
title, author, and copyright? That seems a little excessive. We will
assume that all you need to remove a book is the title.
Removing a book from the library is more difficult than adding a book.
If you look at the code, the library object stores all its books at the
front of the array. Adding a book is easy--we just add it in the first
open slot. Removing a book will leave a ``hole'' in the list. We'll have
to fix that somehow. There are a number of ways to approach filling the
hole--here are two:
- Slide every book past the hole one space to the left, or
- Move the last book into the hole.
Which of these methods do you think is best? Why?
Write the code for the removeBook
member function, and test it to
make sure it is working correctly.
- Now, we need to implement the check-out system for the books.
There is nothing in the code at this point that will allow us to handle
checking out a book. Notice that this is different than removing a book
from the library--a book that is checked out is still owned by the
library, it just isn't available for check out. We need to be able to
check out books, check them back in, and print out a list of all books
that are available to be checked out. How might we do that?
There are a number of possible solutions. Two solutions could be:
- In the library class, make a second array of books to hold
the books that are checked out. Then, when a book is checked out, move
it to the checked out array. When it is checked back in, move it back.
Printing the books that are available would involve simply printing the
books in the first array.
- Or, we could add a data element to the book class to tell
us whether the book is checked out or not. Then, when we check in or out
a book, we just change the data in the book. Printing out the available
books involves simply running through all the books and printing the
ones that are not checked out.
Each solution has its pros and cons, but I think that the second
solution is the most elegant and the most efficient.
So, for this section of the lab, alter the program so that it will allow
you to check out a book, check in a book, and print out the books that
are checked out. You should follow these steps:
- Add a boolean variable to the book class to represent if the
book is checked out or not.
- Add a function to the book class that allows you to check out
the book, and another that lets you check in the book.
- Add a function to the library class that prints out all the
books that are not checked out.
- Alter the
printBooksIn
function of the library class
so that it only prints checked in books.
- Alter the main program to make all the menu options work correctly.
- That's it--congratulations on completing a library inventory
program.