OO Programming and Data Structures | CS 241

06 Prepare : Checkpoint A

Overview

This checkpoint is intended to help you practice the syntax of basic inheritance.

In looking at the collection of books next to your bed, you notice that they fall into a few different categories. First, you have your regular books and novels, second, your textbooks, and finally, your collection of picture books :-).

You quickly recognize that they all have many things in common, but there are some slight differences in the properties of these different kinds of books.

In particular, all books seem to have the following:

That information captures your regular books and novels fairly well, and yet your textbooks also have a subject they belong to, such as math, physics, computer science, etc.:

Finally, your picture books contain all the information of a regular book, but in addition to an author, they also have an illustrator.

You decide this sounds like a perfect opportunity to practice your new inheritance skills, because you could create a base class for a Book, a derived class TextBook that extends a Book, and another derived class PictureBook that also extends Book.

Instructions

For this assignment, because the classes we are going to create are so small and simplistic, and to help you focus on just the elements of inheritance, rather than the mechanics of creating lots of files, you will put all of your classes at the top of the main file, rather than creating separate files for each class, like we would for larger, real-world projects.

  1. Create a class for a Book that has the following member variables:

    • title : string

    • author : string

    • publication_year : int

  2. Next, create a class for a TextBook that extends a Book and adds the following member variable:

    • subject : string

  3. Finally, create a class for a PictureBook that that extends a Book and adds the following member variable:

    • illustrator : string

  4. In the base Book class, create a method prompt_book_info that prompts the user for the title, author, and publication_year, and also a method display_book_info that displays the title, author, and publication year in the format: "Title (publication_year) by Author".

  5. In the TextBook class, create methods prompt_subject and display_subject that prompt and display the subject of the book.

  6. In the PictureBook class, create methods prompt_illustrator and display_illustrator that prompt and display the Illustrator of the book.

  7. In main, first create a Book object and call the following methods:

    1. prompt_book_info

    2. display_book_info

  8. Next, create a TextBook object and call the following methods:

    1. prompt_book_info

    2. prompt_subject

    3. display_book_info

    4. display_subject

  9. Finally, create a PictureBook object and call the following methods:

    1. prompt_book_info

    2. prompt_illustrator

    3. display_book_info

    4. display_illustrator

Sample Output

The following is an example of output for this program:


Title: The Miracle of Forgiveness
Author: Spencer W. Kimball
Publication Year: 1969

The Miracle of Forgiveness (1969) by Spencer W. Kimball

Title: Introduction to C++
Author: James Helfrich
Publication Year: 2012
Subject: Computer Science

Introduction to C++ (2012) by James Helfrich
Subject: Computer Science

Title: Click, Clack, Moo
Author: Doreen Cronin
Publication Year: 2000
Illustrator: Betsy Lewin

Click, Clack, Moo (2000) by Doreen Cronin
Illustrated by Betsy Lewin

Automatic Grading Script (TestBed)

This assignment is pass/fail. In order to receive credit, it must pass the auto-grading test script (TestBed). You can run this script as many times as you like while you are working on the assignment. The same test script will be run by the instructor for credit, so you should not be surprised at the result.

In addition, because the point of this exercise is to help you practice the use of inheritance, you must use inheritance to receive credit for this assignment.

To run the test script, log in to the CS Department Linux system and run the testBed command, providing the class and test script to run as well as the Python program to use, for example:


testBed cs241/check06a check06a.py

Submission

Submit your program by logging into the CS Department Linux system, and running the submit command with your filename, for example:


submit check06a.py

You should then select the appropriate course (CS 241), instructor, and assignment from the list.