i hate Classes

Discussion in 'OT Technology' started by notwist, Apr 11, 2005.

  1. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    This program i am writing is supposed to be a library system where you can seach for books and add or remove books. I am having trouble setting up the initial book collection. I want it to initially store just 5 books and then maybe add more. each "book" is it's own structure that contains the name, ID, and number of copies of the book.

    main file
    Code:
      #include <iostream.h>
      #include <stdlib.h>
      
      #include "Library.h"
      
      int main()
      {
      
       cout << "1) Borrow a Book" << endl;
       cout << "2) Return a Book" << endl;
       cout << "3) Add new book to Library" << endl;
       cout << "4) Remove existing book from Library" << endl;
       cout << "5) Print book status" << endl;
       cout << "6) Print Library status" << endl;
       cout << "7) Exit" << endl;
       cout << "\nSelect an option 1-7" << endl;
      
      	  system("PAUSE");
      	  return 0;
      }
      
    header file
    Code:
      class Book{
      private:
      	  char BookName[40];
      	  char BookID[10];
      	  int numCopies;
      	  int numAvail;
      
      public:
      	  Book(char,char,int);
      
      	  void printBStatus();
      	  void printLStatus();
      
      	  char getBookName();
      	  char getBookID();
      	  int addBook(char *,char *,int);
      	  int removeBook(char *,char *,int);
      	  void returnBook(char *,char *);
      	  int borrowBook(char *,char *,int);
      	  int getNumCopies();
      };
      
    i tried adding:
    Book Book1={"title name","12345",5}; to the main and header files but that didn't work. it must be since the variables are private. i would remove the private command in the header file but my instructor wants us to use this template. so basically, how do i set up the initial collection books? i'm thinking of storing them in an array but then again, books are supposed to be added to the list if need be. :dunno:
     
  2. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,923
    Likes Received:
    11
    Location:
    Los Angeles
    You need to use constructors and initialize the variables inside the constructor.

    Sample header:
    Code:
    class Book {
       public: 
            Book(string inT, int inID, int inCP);
       // rest of public data
       private:
       string title;
       int id;
       int copies;
       // rest of private data.
    };
    
    Sample .cpp file which initializes input variables:
    Code:
    #include "Book.h"
    
    Book::Book(string inT, int inID, int inCP)
    :title(inT), id(inID), copies(inCP)
    {};
    
    Then, you can create multiple copies of the book in your main file
    Code:
    #include "Book.cpp"
    
    int main() {
        Book bk1("Book 1", 1492, 4);
        Book bk2("Book 2", 4244, 6);
        Book bk3("Book 3", 4324, 3);
        return 0;
    }
    
    If this isn't what you are asking, then I don't know. :dunno:
     
  3. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    thanks alot. i'm gonna try this stuff out. :wavey:
     
  4. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    alright. i started over and i keep getting this message:
    library.h: In member function `void Book::AddBook()':
    library.h:36: error: no match for 'operator>>' in 'std::cin >>
    temp->Book::bookID'

    here is my library.h
    Code:
      #include <iostream.h>
      
      using std::cout;
      using std::cin;
      using std::endl;
      
      class Book
      {
       public:
        char bookName[40];
        int bookID[10];
        int numCopies;
        Book *next;
      
      
      
        Book(int=0);
        void AddBook();
        
      
      };
      
        Book *current;
        Book *start_ptr = NULL;
      
      Book::Book(int copies) : numCopies(copies){}
      
      void Book::AddBook() //add book to end of list
      {
        Book *temp, *temp2;
        
        temp = new Book;
        cout << "\nEnter title of book: ";
        cin >> temp->bookName;
        cout << "\nEnter ID: ";
        cin >> temp->bookID;
        cout << "\nEnter number of copies: ";
        cin >> temp->numCopies;
        temp->next = NULL;
      
        if(start_ptr == NULL) //if list is empty
      	{
      	  start_ptr = temp;
      	  current = start_ptr;
      	}
        else
      	{
      	  temp2 = start_ptr;
      	  while(temp2->next != NULL)
      	temp2 = temp2->next;
      	  
      	  temp2->next = temp;
      	}
      }
        
      
      
    its just supposed to add a new book to the end of the link list
     
    Last edited: Apr 13, 2005

Share This Page