The goal of this lab is to introduce you to basic
concepts in the C++ program language. Concepts you will be familiar with
after this lab include:
- object-oriented programming in C++
- inheritance and polymorphism
- defensive programming (e.g., detecting improper input)
- designing and compiling multi-file programs
In addition, you will become somewhat familiar with making system
calls from within your C++ program.
A skeleton version the program will appear in your
cs35/labs/02 directory when you run update35. The
program handin35 will only submit files in this directory. You may
choose one partner for this lab. Both partners should be present and working
on the code together. You will both be responsible for understanding
all concepts, so dividing and conquering is not an option. The academic
integrity policy applies to the entire pair;
you cannot work or share code with anyone outside
of your partner. Also, be sure to indicate who your partner is by including
both names in the header and by also selecting the 'p' option when
using handin35. Only one student in the pair should submit the lab.
Introduction
In this lab, you will create several classes representing different
types of media, including pictures, books, and video. All of these
classes will be subclasses of an abstract Media class. This
Media class will encapsulate all information and
functionality that is common to all types of media. Your subclasses
will add additional unique data and methods as well as implement
inherited methods to be specific. For instance, "opening" a book should be
different than "opening" a picture or video.
In addition, you will create a MediaLibrary class, which acts as a
container for several Media objects (think of how iTunes stores
all of your eBooks, music, video, etc.). You will also build capability
in the MediaLibrary class for obtaining information about your
different media objects and for playing the media itself. This will
be different than most projects you have completed in that the main user
interface will be handled by an object. Below is an overview of the files
required
for submitting this week's lab. Those highlighted in blue will require
implementation on your part. Those in black are complete and should not
be modified except for comments.
- Makefile - contains scripts for compiling your program.
You will only need to modify lines 4 and 6 as you finish portions of the lab.
- testLibrary.txt - small test file of media items.
- library.txt - input file for loading all media items.
You do not need to modify this except to add more examples (optional).
- media.h/.cpp - declares and defines the Media class,
an abstract parent class of all media types.
- book.h/cpp - declares and
defines the Book class, a subclass of Media.
- picture.h/cpp - declares and
defines the Picture class, a subclass of Media.
- video.h/cpp - declares and
defines the Video class, a subclass of Media.
- testMedia.cpp - use this file to design
simple but thorough tests for your classes.
- mediaLibrary.h and mediaLibrary.cpp - declares and defines the MediaLibrary class. This class
acts as the overall container for all Media items and also serves
as the main interface for user interaction.
- main.cpp - the main program which creates an instance of the
MediaLibrary. You do not need to modify this file.
- README - complete this when your lab
is finished and you are ready to hand in the program.
Program Requirements
I recommend implementing your program in 3 phases:
- Media and Picture classes -- be able to view a picture.
- Book and Video classes -- be able to open a book or play a video.
- Media Library -- build and test a MediaLibrary class that
holds several media objects and lets the user get information and/or
open each object.
Your program should use proper design, and you should use defensive
programming as you develop your code. Your program will be tested on
your use of modular functions and understanding of how to define C++
classes and use inheritance. In addition, you should implement small
test programs in between each phase.
Do not use your MediaLibrary to test and debug your Media objects. You will assuredly go
insane tracking down errors if you wait that long to test.
I. Media and Picture classes
Your program will need to handle several types of media. To get
started, first review the Media and Picture
classes.
The Media class has been implemented for you. But you must start but
understanding this class.
The Media class encapsulates all information
and methods common to all media types. In particular, it contains:
- data for a media file's name, type,
and location. The
name is just the name of the media object. The type
should be either picture, book, or video. The location is
where the media file is actually accessed; this could be a local
location, or a remote location like a url address.
- accessor functions for all of the above data.
- an open method. This is the method with which users
of Media objects will open a book, view a picture, or play a
video. open is a pure virtual function, since you
cannot play a general Media object itself.
- a default printInfo method. This will print out values
for the data members. Subclasses should also implement this method and
be sure to call the parent version (since the data members are private).
We have provided skeleton code for both
media.h
and
media.cpp. Note that since there is no definition for how
to
open() a
Media object, you cannot actually create
an instance of the
Media class. This is what is known as
an
abstract class - it forms the template for subclasses you want
to define later. In this way, it is a
contract: anything that
inherits the
Media class
must complete a unique
open
method. And any user who wants to create
Media objects must
chose a specific subclass (e.g.,
Picture, Book, Video).
The
Picture class should specialize (i.e., inherit)
Media
and implement any information particular to pictures. In particular:
- add a data member that stores the format of the image
as a string (e.g., "JPG","RAW","GIF"). You should
add an accessor method as well.
- provide a default destructor that does nothing.
- implement the constructor to set data members.
Set the type to be "picture". Remember to
invoke the parent constructor to establish inherited data members.
- implement the open method, which makes a system call to use
the viewnior program to open this picture. You should know how
to make this system call from Lab 01.
- Implement printInfo() method. This should utilize
the parent version to print out inherited data members and provide
additional output for the format.
Before moving onto the second phase of this lab, modify
testMedia.cpp to test your class. This program should create
a
Picture object and then open the picture. This picture can
be one of the images from Lab 01, or one of your own images. You should
test, at a minimum, all methods whether inherited or not, before moving forward.
To compile incrementally, first compile the Picture class by
itself to make sure you have no errors:
$ g++ -c picture.cpp
We have provided a
Makefile that automates the process. To compile
one of the classes, simple follow this example:
$ make picture.o
This executes the command given above. When you are ready to use
testMedia, run:
$ make testMedia
II. Book and Video classes
Once you have successfully created and opened a Picture
object, you can begin creating other media objects. Note that there are
no files provided for you, you need to create them from scratch.
The Video class should specialize Media,
just like the Picture class. You should define your header and implementation
file in video.h and video.cpp, respectively.
In addition to what it inherits from Media, your Video class should comply with the following:
An example creation of a
Video object will be similar to the following:
Video testVideo("Hello Heisman","http://www.youtube.com/watch?v=YxpoqP9PkqM", 123);
After creating the video class, write a
a new function in
testMedia.cpp.
This function should create
a
Video object and play the video to thoroughly test its capabilities.
NOTE: the
Makefile currently does not have the capability to compile
the
Video class. You will need to edit lines 4 and 6. This is as
simple as moving the comment mark after the terms
video.o and
video.h.
Next, create a Book class in book.h and book.cpp.
We have given a starting point for these files.
This should also specialize
the Media class. This class will be the most intricate of the three
as you will turn your computer into an eBook reader when loading the book.
In addition to the inherited methods/data, your class should:
- Create data members for author (string) and year (int) as well as
accessor methods. In addition, you should
maintain the current line (as an int) the reader left off at.
- The constructor should take in the book title to set the name field,
file name to set location, in addition to the author's name
(a string) and year of publication (an int). In addition to using these
parameters to initialize the respective data members, you should initialize
the current line to 0 and set type to "book".
- A destructor that does nothing.
- printInfo should print out all data member values as with
other classes.
- Implement an open() method that prints out the contents
of the book. To help, we have provided you the non-member method
printPage which takes in the location of the book and
the starting line number. It returns the status of the book - whether
the end of file was reached (i.e., the reader finished the book).
open() should meet the following requirements:
- Print out one page of text at a time. If, after printing the page,
the book was completed, then reset the current line bookmark to 0 and
exit the function.
- If the book is not complete, be sure to change the current line
bookmark (HINT: one page output is 10 lines)
- Ask the user to choose from three options: to print the next page,
to reset the bookmark to the beginning and quit, or to quit with the
bookmark updated to the next page to read. For example:
"Select n for next; r to reset and quit; q to bookmark and quit: "
- The function should not end until the user selects to quit or finishes
the book
- Be sure to error check the user input. You should re-prompt
the choices if the user provides an illegal value.
III. The Media Library
Finally, you should build a
MediaLibrary class. This class
should store a collection of
Media objects and allow a user
to list, get details, and open the media contained in
the
MediaLibrary.
We have provided
initial header and implementation file (i.e.,
mediaPlayer.h, mediaPlayer.cpp). We have also
implemented the
run method, which gives the user a list of
commands and calls the corresponding class methods.
Here is what you need to do:
- In the constructor, populate the Media array with
different media. You should read the given file, where one line defines
one media object. The first token is the type (e.g., picture, video, or book),
the second is the location, and the third the name of the object.
Depending on the type, there are additional fields to read in. The tokens
are space delimited, so you can safely assume the >> operator
will work properly.
- You can assume that the library has at most 50 files, and also that it is correctly formatted.
- We have implemented the run method for you as well as
printMenu. In combination, these two methods
print out a menu of options and ask the user to select a command. run
calls the appropriate class method to fulfill the request and loops until
the user opts to quit.
- Implement the listMedia function. This method should
print out a numbered list of media contained in the library. For each
media object, display it's type and name
- Once that is working, implement the other list functions (listBooks(),
listVideos(), listPictures()). These should list all items of the given
type. You do not need static casting to accomplish this. You only need to
print the name of the media item.
- Implement the openMediaMenu function. This should ask
the user which media object should be opened and then fulfill
the request.
Be sure to validate the user's choice, looping until they select a valid item.
- Implement the printInfoMenu method. Like
the openMediaMenu, this menu should ask the user to select a
media file. You should validate this selection.
Once the user selects which media to display, print out
details for that media file.
Check List and Sample Output
To recap, you are required to to the following:
- Implement Picture, Video, Book classes using inheritance
and good design.
- Incrementally test these methods in testMedia (we will look
at this file to evaluate your methodology).
- Implement MediaLibrary to provide a well-design user-interface.
- No modifications should be made to media.h, media.cpp,
mediaLibrary.h, main.cpp
- Other classes implement the details required and no more. The
exception is that additional private methods are allowed. But no
additional data members or public methods.
- All user-input is validated to ensure they select correct options.
- Your code maintains good style: spacing, in-line commenting for
complicated blocks of code, function header comments, and header comments.
- One partner submits the lab, and the partnership is made clear in the
header comments for each file (plus the README and handin35 script).
- Answer questions listed in the README file.
There are two sources of expected output you can use to help ensure your program
is working. The first is a sample run using testLibrary.txt.
For obvious reasons, the test does not show an example of opening a picture
or video, but does do simple operations with a book. In addition, I have
provided an executable for a working version of the program that you can
test dynamically. Simply run the following command:
$ ~soni/public/cs35/mediaLibrary/main
Submit
Once you are satisfied with your code, hand it in by typing
handin35. This will copy the code from your
cs35/labs/02 to my grading directory. You may run
handin35 as many times as you like, and only the most recent
submission will be recorded. Please record your partner one time using the
'p' option.