/* * A simple C++ program */ #include #include const int NAME_LENGTH = 64; const int NUMBER_CARDS = 32; // List of classes in this program class Author; class Book; class CatalogCard; // Declaration of the classes class Author { public: Author(char *, char *, char *, char *); void addCatalogCard(CatalogCard *); void showAuthor(void); private: char real_name[NAME_LENGTH]; char pen_name[NAME_LENGTH]; char dob[NAME_LENGTH]; char dod[NAME_LENGTH]; CatalogCard *cards[NUMBER_CARDS]; int nbooks; } class Book { public: Book(char *, Author *, int); void showBook(void); private: char title[NAME_LENGTH]; Author *by_author; int pages; }; class CatalogCard : public Book { public: CatalogCard(char *, Author *, int, char *, int); private: void showCard(void); char catalog[NAME_LENGTH]; // true(1) if checked out, otherwise false(0) int checked_out; }; // Implementation of member functions Author::Author(char *real, char *pen, char *dob, char *dod) { strcpy(real_name, real); strcpy(pen_name, pen); strcpy(this->dob, dob); strcpy(this->dod, dod); nbooks = 0; } void Author::addCatalogCard(CatalogCard *card) { cards[nbooks++] = card; } void Author::showAuthor(void) { cout << " Real Name: " << real_name << endl; if (*pen_name != '\0') { cout << " Pen Name: " << pen_name << endl; } cout << " Lived " << dob << " to " << dod << endl; if (nbook == 1) { // in C++ the single "=" sign // does NOT test equality cout << " This is the only book by this author in the library." << endl; } else { cout << " There are " << nbooks << " books by this author in the library." << endl; /* * Need to print the titles of other books written by this * author. How? Add an accessor function showTitle() in the * Book class to print the title of a book, then we can go * through the cards here and call showTitle() on the card * objects (which are also Book objects). */ } } Book::Book(char *title, Author *by_author, int pages) { strcpy(this->title, title); this->by_author = by_author; this->pages = pages; } void Book::showBook(void) { cout << "--------------------------------------------------" << endl; cout << " Title: " << title << endl; cout << " Author: " << endl; by_author->showAuthor(); cout << " Pages: " << pages << endl; } CatalogCard::CatalogCard(char *title, Author *by_author, int pages, char * catalog, int checked_out) : Book(title, by_author, pages) { strcpy(this->catalog, catalog); this->checked_out = checked_out; } void CatalogCard::showCard(void) { showBook(); cout << " Catalog: " << catalog << endl; if (checked_out) { cout << " Status: Checked out" << endl; } else { cout << " Status: Available" << endl; } } // main function of the program to demonstrate the classes void main(void) { Author *authors[25]; CatalogCard *cards[100]; int nauth = 0; int ncard = 0; int i; authors[nauth++] = new Author("Clemens, Samuel L.", "Mark Twain", "1835", "1910"); cards[ncard++] = new CatalogCard("A Connecticut Yankee", authors[0], 352, "Fiction Row 7", true); cards[ncard++] = new CatalogCard("What Is Man?", authors[0], 154, "Fiction Row 7", false); cards[ncard++] = new CatalogCard("Tom Sawyer Detective", authors[0], 371, "Fiction Row 7", false); cards[ncard++] = new CatalogCard("Adventures of Huckleberry Finn", authors[0], 371, "Fiction Row 7", true); authors[0]->addCatalogCard(cards[0]); authors[0]->addCatalogCard(cards[1]); authors[0]->addCatalogCard(cards[2]); authors[0]->addCatalogCard(cards[3]); authors[nauth++] = new Author("Burroughs, Edgar R.", "", "1875", "1950"); cards[ncard++] = new CatalogCard("Tarzan, Jewels of Opar", authors[1], 317, "Fiction Row 2", false); cards[ncard++] = new CatalogCard("Return of Tarzan", authors[1], 423, "Fiction Row 2", false); cards[ncard++] = new CatalogCard("Son of Tarzan", authors[1], 296, "Fiction Row 2", false); cards[ncard++] = new CatalogCard("Tarzan of the Apes", authors[1], 317, "Fiction Row 2", true); authors[1]->addCatalogCard(cards[4]); authors[1]->addCatalogCard(cards[5]); authors[1]->addCatalogCard(cards[6]); authors[1]->addCatalogCard(cards[7]); authors[nauth++] = new Author("Schartau, Winn", "", "1953", "present"); cards[ncard++] = new CatalogCard("Terminal Compromise", authors[2], 429, "Fiction Row 42", true); authors[2]->addCatalogCard(cards[8]); for (i = 0; i < ncard; i++) { cards[i]->showCard(); } }