Example 3 -
Create a class called card that maintains a library card catalog
entry. Have the class store a book's title, author, and number of copies
on hand. Store the title and author as strings and the number on hand
as an integer. Use a public member function called store() to store a
book's information and a public member function called show() to display
the information. Include a short main() function to demonstrate the class.
- (a) The question clearly indicates that we need a class. This is not
always the case. You can tell if a class will be convenient if you need
to store several properties that are connected somehow. This question
has the properties of a book title, author and number of copies on hand.
Since they are all related to the books in a library, that can be grouped
in a class with those three properties (variables).
Since the class should control access to these variables, we will make
them private. Then we need to make member functions that access and/or
modify the variables. The member functions should be public so that
we can use them outside of the class (when we create an instance of
Fortunately, this question lays out exactly what is needed. We will
have 2 member functions. store() will save the library card catalog
entry information and show() will print the information to the screen.
(b) Next, we will need a main() function. This is also called a driver
A driver simply instantiates the class and uses all the member variables
and member functions. In this way, the entire class is tested and demonstrated.
Once you know your class is functioning properly, you can re-use it
for any other program you are creating. (One of the benefits to the
- Designing the class:
- We need 3 member variables:
(1) string book_title
(2) string author_name
(3) integer copies_on_hand
We need 2 member functions:
(1) store() - this will set the three member variables
(2) show() - this will display the values for the three member variables
to the screen
It hasn't been requested directly by the question, but we're going to
create 2 versions of the store() function (Function Overloading).
(1) store() - the parameterless version will prompt the user for the
(2) store(string, string, integer) - the version with parameters will
be available in the event that the information is known and can be hard-coded
into the program.
Sometimes it is useful to overload a function in this manner so that
it can be used in different ways. This can make your class more user-friendly.
This class happens to be quite simple. Rather than show the pseudocode,
I leave this to you as an exercise. Try it before you look at the source
View the class definition
View the function
The main() function is just a small program that instantiates the class
and tries each member function to show that it works.
Again, I leave the pseudocode to you as an exercise.
View the driver program
Note: To compile this code, type the following at the hercules prompt:
CC -LANG:std example3.cpp catalogCard.cpp