Thread: C++ Library Program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    C++ Library Program

    Hi, this is my first time on these boards, so please bare with me. Im in need of some help trying to get my program running correctly. Here is the assignment that i have to follow:

    In this assignment, you will learn how to use inheritance and virtual functions by designing a set of classes to represent library holdings. Our library has two kinds of items: books and audio recordings. Each item has a title and an integer call number. In addition, books have authors, and recordings have performers and formats (LP, Cassette, Reel-to-Reel, or Compact Disk). In addition to constructors and destructors, each holding should have a print() function that displays its data to an ostream object (such as cout) that the user specifies.
    You should begin by designing a Holding class that contains all the data common to books and recordings. It should also have a pure virtual print() function. Then design the Book and Recording classes to add the details necessary for those types of items and to implement the print() function. Note that Holding will be an abstract base class, since it will have a pure virtual function. This makes sense, since there are no generic holdings, only books and recordings.
    All three classes should have both copy constructors and constructors that accept the individual data fields as arguments. Although it may seem natural to have a zero-argument default constructor that queries the user and reads in the data fields, doing I/O from within a constructor is probably unwise. If the constructor did I/O, the class implementation would be tied to a particular style of getting data. Applications that use this class may read input from a file, in which case the prompt messages would be inappropriate, or they may use a window-based interface that does not support the cin and cout I/O stream objects. Since constructors are such a basic part of the operation of a class, it is best to let the user decide where their input data comes from.
    Once you have written the code to implement the Holding, Book, and Recording classes, you should write a simple program to test them. This program should declare an array of five pointers to Holding objects and then repeatedly call a function that creates a new Holding. This function asks the user which kind of Holding is to be entered and then inputs the data needed to create either a Book or a Recording. A new object of the appropriate class is created with this data, and the resulting pointer is returned. Note that even though the function always returns either a Book pointer or a Recording pointer, the function can be declared to return a Holding pointer, and the pointer to the newly created object can be returned without conversion. This pointer is then stored in the array and the function is called again until all five objects have been created. Now there is an array to five pointers to Holding objects, and each of these objects "knows" whether it is a Book or a Recording, although the program has no direct way of querying them to get this information. However, it can run through the array and call the virtual print() function for each object, which will then use the appropriate virtual function for printing itself out.

    In short, i basically have to make 4 files that come together to form a library in which it will ask for holdings and then store it and then print out the holdings, whether its a book or record. Below is my code for my library, book, holding and record files. If anyone is interested in the errors im getting when im making the library, i can post it up. Sorry for such a big post for a first post. I dont know any other way to get my code out.

    Code:
    //library.cpp
    #include <sstream>
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include "library.h"
    #include <string>
    Holding *getInfo();
    using namespace std;
    ofstream csis;
    
    int main(){
        csis.open("csis.dat");
        
        Holding* holdPtr[5];
        
        for (int i=0;i<5;i++){
            holdPtr[i] = getInfo();
        }
        for (int i=0;i<5;i++){
            holdPtr[i]->print();
        }
        csis.close();  
    }
    
    Holding *getInfo(){
        int callNumTemp;
        char choice;
        std::string titleTemp;
        std::string authorTemp;
        std::string formatTemp;  
        std::string performerTemp;
        cout << "Enter holdings to be stored in a list: " << endl;
        cout << "Enter B for book, R for recordings: ";
        cin >> choice;
        std::cin.ignore(); //need so NOT to skip title input...
    
        if (choice == 'b' || choice == 'B') {
            cout << "Enter book title: ";
            std::getline(std::cin, titleTemp);
            cout << "Enter book author: ";
            std::getline(std::cin, authorTemp);
            cout << "Enter call number: ";
            cin >> callNumTemp;
            return new Book(titleTemp,authorTemp,callNumTemp);
        } else if (choice == 'r' || choice == 'R') {
            cout << "Enter recording title: ";
            std::getline(std::cin, titleTemp);
            cout << "Enter performer: ";
            std::getline(std::cin, performerTemp);
            cout << "Enter format: (L)P, (C)assette, (R)eel_to_reel, (D)isk: ";
            std::getline(std::cin, formatTemp);
            if (formatTemp.compare("l") == 0 || formatTemp.compare("L") == 0)
                formatTemp = "LP";
            if (formatTemp.compare("c") == 0 || formatTemp.compare("C") == 0)
                formatTemp = "Cassette";
            if (formatTemp.compare("r") == 0 || formatTemp.compare("R") == 0)
                formatTemp = "Reel_to_reel";
            if (formatTemp.compare("d") == 0 || formatTemp.compare("D") == 0)
                formatTemp = "Disk";
            cout << "Enter recording number: ";
            cin >> callNumTemp;
            return new Recording(titleTemp,performerTemp,formatTemp,callNumTemp);
        } else
            cout << "else called (testing)"<< endl;
    return NULL;
        
    }
    Code:
    //library.h
    #ifndef _LIBRARY_H
    #define _LIBRARY_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    #include "holding.h"
    #include "book.h"
    #include "record.h"
    extern	std::string titleTemp;
    extern  int callNumTemp;
    extern	char choice;
    extern  std::string authorTemp;
    Holding* getInfo(int x);
    	
    #endif
    Code:
    //book.cpp
    #include "book.h"
    using namespace std;
    
    Book::Book(std::string titleTemp, std::string authorTemp, int
    callNumtemp) :  Holding(title, callNumber) {  //seperated for screen width
        author = authorTemp;
        
    }
    
    Book::~Book(){
        delete [] author;       
    }
    void Book::print(/*ostream &out*/){
           
            cout << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
           
    }
    Code:
    //book.h
    #ifndef _BOOK_H
    #define _BOOK_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    #include "holding.h"
    
    class Book : public Holding {
         
    protected:
    
    public:
        string author;
        Book(string titleTemp, string authorTemp, int callNumTemp);
        Book(const Book &);
        ~Book();
        void print(ostream &);
    
    #endif
    Code:
    //holding.cpp
    #include "holding.h"
    using namespace std;
    extern ofstream csis;
    
    Holding::Holding(std::string tempTitle, int callNumTemp){
           title = tempTitle;
        callNumber = callNumTemp;
    }
    
    Holding::~Holding(){ 
        delete [] title;
    }
    Code:
    //holding.h
    #ifndef _HOLDING_H
    #define _HOLDING_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Holding{
    protected:
        string title;
        int callNumber;
    public:
        string title;
        int callNumber;
         Holding(string titleTemp, int callNumTemp);
         Holding();
         Holding(const Holding &);
         virtual ~Holding();
         virtual void print(ostream &) = 0; 
         ~Holding();
    };
    #endif
    Code:
    //record.cpp
    #include "record.h"
    using namespace std;
    Recording::Recording(std::string titleTemp, std::string performerTemp, std::string
     formatTemp,int callNumtemp) :  Holding(title, callNumber) { //seperated for screen width
        performer = performerTemp;
        format = formatTemp;
    }
    Recording::~Recording(){
        delete [] author;       
    }
    void Recording::print(ostream &out){
        cout << "RECORDING: " << '\"'  << title << '\"' << " "  << performer   << " " << format << callNumber << endl;
     }
    Code:
    //record.h
    #ifndef _RECORDING_H
    #define _RECORDING_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    #include "holding.h"  //commented out above since holding.h has them.
    
    class Recording : public Holding {
         
    protected:
    
    public:
        std::string performer;
        std::string format;
        Recording(string titleTemp, string performerTemp,string formatTemp, int callNumTemp);
        Book(const Book &);
        ~Recording();
        void print(ostream &);
    };
    
    #endif
    Last edited by Salem; 04-25-2010 at 10:38 PM. Reason: Too boldness removed

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, please post your error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thanks for your reply and here are the errors:

    In file included from library.h:8,
    from library.cpp:5:
    holding.h:14: error: declaration of `std::string Holding::title'
    holding.h:11: error: conflicts with previous declaration `std::string Holding::title'
    holding.h:15: error: declaration of `int Holding::callNumber'
    holding.h:12: error: conflicts with previous declaration `int Holding::callNumber'
    holding.h:21: error: `Holding::~Holding()' and `virtual Holding::~Holding()' cannot be overloaded
    In file included from library.h:10,
    from library.cpp:5:
    record.h:18: error: ISO C++ forbids declaration of `Book' with no type
    record.h:18: error: declaration of `int Book::Recording::Book(const Book&)'
    book.h:10: error: changes meaning of `Book' from `class Book'
    In file included from library.cpp:5:
    library.h:11: error: storage class specified for field `titleTemp'
    library.h:12: error: storage class specified for field `callNumTemp'
    library.h:13: error: storage class specified for field `choice'
    library.h:14: error: storage class specified for field `authorTemp'
    library.cpp:8: error: expected nested-name-specifier before "namespace"
    library.cpp:8: error: expected unqualified-id before "namespace"
    library.cpp:8: error: expected `;' before "namespace"
    library.cpp:8: error: expected unqualified-id before "namespace"
    library.cpp:25: error: `Holding* Book::getInfo()' and `Holding* Book::getInfo()' cannot be overloaded
    library.cpp:67: error: expected `}' at end of input
    library.cpp: In member function `int Book::main()':
    library.cpp:20: error: no matching function for call to `Holding:rint()'
    holding.h:20: note: candidates are: virtual void Holding:rint(std:stream&)
    library.cpp:23: warning: no return statement in function returning non-void
    library.cpp: At global scope:
    library.cpp:67: error: expected unqualified-id at end of input
    make: *** [library.o] Error 1

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    protected:
        string title;
        int callNumber;
    public:
        string title;
        int callNumber;
    
        virtual ~Holding();
    
        ~Holding();
    You're repeating yourself for one thing.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first one is this
    Code:
    protected:
        string title;
        int callNumber;
    public:
        string title;
    It can't be public and private at the same time.

    Your book.h is missing };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    wow i feel dumb... it fixed probably over half of the problems i had from before. Now it has been reduced to:

    In file included from library.h:8,
    from library.cpp:5:
    holding.h:21: error: `Holding::~Holding()' and `virtual Holding::~Holding()' cannot be overloaded
    In file included from library.h:10,
    from library.cpp:5:
    record.h:18: error: ISO C++ forbids declaration of `Book' with no type
    record.h:18: error: declaration of `int Recording::Book(const Book&)'
    book.h:10: error: changes meaning of `Book' from `class Book'
    library.cpp: In function `int main()':
    library.cpp:20: error: no matching function for call to `Holding:rint()'
    holding.h:20: note: candidates are: virtual void Holding:rint(std:stream&)
    make: *** [library.o] Error 1

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This line in record.h:
    Code:
    Book(const Book &);
    looks like a copy constructor, but if so it's in the wrong class.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    so i dont need it? i notice that i have it for record.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by the rofl waffle View Post
    so i dont need it? i notice that i have it for record.
    I think that's my point -- you cut and pasted it into Record, but you forgot to change it to reflect that fact that it's not a Book any more but a Record.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    i see... ive changed it now but still have the same errors as before.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    can anyone explain to me why im getting these errors or how to fix them, please? my brain is fried.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So over a day has passed, and you've made NO progress?

    Look at the error messages, look at where they point - READ your C++ book again around the relevant chapters. We're not going to be here permanently to fix your errors, it's something you need to learn to do yourself.

    We've picked you up off the floor - now it's time to see if you can wobble yourself forward for a step or two without falling down again.


    Next time you write a program, consider this:
    A development process
    That is, don't spend days writing some massive program, then post all the code and error messages on a forum when it shockingly doesn't compile (for someone else to fix).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    You know what? you are right. And thanks to you, i was able to fix even more of my code errors, but i am stuck at the last 2 errors i get. The last 2 errors are giving me some trouble.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by the rofl waffle View Post
    You know what? you are right. And thanks to you, i was able to fix even more of my code errors, but i am stuck at the last 2 errors i get. The last 2 errors are giving me some trouble.
    Is it the same error you posted earlier, where the compiler said "HEY you're supposed to pass a stream object to your print function!" or is it something else?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Library program - need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-08-2004, 12:09 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread