Thread: Passing an array of a class object into a function

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Passing an array of a class object into a function

    As of above but i want to pass tat array of class object into a function by Reference.

    Oh yea, I'm on an assignment which i've to write a program to store and manage books in a library system.

    Here's my code:

    Code:
    class Book{
          public:
                 Book(){};
                 void setBookInfo(string, string, float, string);
                 void getBookInfo(string&, string&, float&, string&);
                 static int maxBooks, bookCounter;
                 
          private:
                  string title, author, category;
                  float price;
    };
    
    void addNewBook(Book[]);
    
    int main (void){
        Book libraryBooks[Book::maxBooks];
        char choice = ' ';
        do{
             cout << "\t--->\tWelcome to eLibrary!\t<---\t" << endl;
             cout << "Press:" << endl;
             cout << "\tA --> to Add New Book" << endl;
             cout << "\tB --> to Update a Book Info" << endl;
             cout << "\tC --> to Delete a Book" << endl;
             cout << "\tD --> to Search for a Book" << endl;
             cout << "\tE --> to Display All Books" << endl;
             cout << "\tF --> to Display Library Statistics" << endl;
             cout << "\tQ --> to Quit the program" << endl;
             choice = getch();
             system("cls");
             
             switch(choice){
                      case 'a':
                           addNewBook(libraryBooks);
                           break;
                      case 'b':
                           updateBook(libraryBooks);
                           break;
                      case 'c':
                           break;
                      case 'd':
                           break;
                      case 'e':
                           break;
                      case 'f':
                           break;
                      case 'q':
                           break;
             }
        } while (choice != 'q');
        system("pause");
        return 0;
    }
    
    void Book::setBookInfo(string setTitle, string setAuthor, float setPrice, string setCategory){
         title = setTitle;
         author = setAuthor;
         price = setPrice;
         category = setCategory;
    }
    
    void Book::getBookInfo(string& getTitle, string& getAuthor, float& getPrice, string& getCategory){
         getTitle = title;
         getAuthor = author;
         getPrice = price;
         getCategory = category;
    }
    
    void addNewBook(Book libraryBooks[]){
         string strTitle = "", strAuthor = "", strCategory = "";
         float fltPrice = 0.0;
         
         cout << "Enter the Book Title: ";
         fflush(stdin);
         getline(cin, strTitle);
         for (int i = 0; i < sizeof(libraryBooks) ; i++){
             string strTempTitle, strTempAuthor, strTempCategory;
             float fltTempPrice;
             libraryBooks[i].getBookInfo(strTempTitle, strTempAuthor, fltTempPrice, strTempCategory);
             if (strTitle == strTempTitle){
                          cout << "This book " << strTitle << " has already been stored into the library." << endl;
                          cout << "Please enter the Book Title again: ";
                          fflush(stdin);
                          getline(cin, strTitle);
                          break;
             }
         }
         cout << "Enter the Book Author: ";
         fflush(stdin);
         getline(cin, strAuthor);
         cout << "Enter the Book Price: ";
         fflush(stdin);
         cin >> fltPrice;
         cout << "Enter the Book Category: ";
         fflush(stdin);
         getline(cin, strCategory);
         
         libraryBooks[Book::bookCounter].setBookInfo(strTitle, strAuthor, fltPrice, strCategory);
         Book::bookCounter++;
         
         cout << "\n" << endl;
         cout << "Book added successfully!" << endl;
         system("cls");
    }
    so how do i pass tat array of Book object into addNewBook() by reference?

    feel free to give any comments! thanks~

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably don't need to pass it as a reference, since an array is automatically converted to the address of the data - which means that the array is a pointer, and any modification to the books array will appear in the original array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fflush(stdin);
    As well as being undefined (see the FAQ), you don't even need it if you stick to using getline().
    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.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Talking

    thanks for all the replies! really appreciate it.
    i think i get it already. thanks alot!
    pardon me. i'm a newbie in C++ programming...
    migrating from java to C++ is a little difficult for me.
    so might need some more help for you guys.
    thank again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM