Lab 1: Principles of Programming

Purpose:

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.

Prerequisites:

You should have taken CS 1 before you attempt this lab.

Introduction:

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:

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.

Procedure:

  1. Copy the project files from I:\CSC122\public\Lab1 into your account.

  2. 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.

  3. 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:

    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.

  4. 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:

    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:

    1. Add a boolean variable to the book class to represent if the book is checked out or not.

    2. Add a function to the book class that allows you to check out the book, and another that lets you check in the book.

    3. Add a function to the library class that prints out all the books that are not checked out.

    4. Alter the printBooksIn function of the library class so that it only prints checked in books.

    5. Alter the main program to make all the menu options work correctly.

  5. That's it--congratulations on completing a library inventory program.