CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 22

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    11

    Help needed with class BookType

    I have the following question:

    A. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication.
    B. Design a class bookType that defines the book as an ADT.
    i. Each object of the class bookType can hold the following information about a book:
    1. title,
    2. up to four authors,
    3. publisher,
    4. ISBN,
    5. price,
    6. and number of copies in stock.
    7. To keep track of the number of authors, add another member variable.
    ii. Include the member functions to perform the various operations on objects of type bookType.
    iii. For example, the usual operations that can be preformed on the title are:
     to show the title,
     set the title,
     and check whether the title is the same as the actual title of the book.
    iv. Similarly, the typical operations that can be preformed on the number of copies in stock are :
     to show the number of copies in stock,
     set the number of copies in stock,
     update the number of copies in stock,
     and return the number of copies in stock.

    v. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and destructor (if one is needed).

    B. Write the definitions of the member functions of the class bookType.

    C. Write a program that uses the class bookType and tests various operations on the objects of the class bookType.

    D. Declare an array of 100 components of the type bookType. Some of the operations that you should perform are to search for a book by title, search by ISBN, and update the number of copies of a book.



    I have complete all except the search book by title and search by ISBN. I want to try using the vector function to store all the information for the books.

    Anyway can help with me that? From the class template to implementing the vector function on how to push the data, pop the data from the vector class.

    thanks. Appreciate very much.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help needed with class BookType

    Your assignment calls for you to declare a fixed length array, not a vector.

  3. #3
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    ya.. i know. but that just part of the question. The assignment requirements are to use vectors, no STL.

    I pasted the requirements of the assignment below.

    Customer lists are stored using a vector class. Book lists are also stored using a vector class. You must write your own vector class to be used in exercise 7. There must be only one vector class. Note that means you can have as many vector object as you need but there is only one vector class. STL data strctures cannot be used in this assignment.


    So right now I am trying to do the book list to store using a vector class. And the question I pasted earlier is for the book lists.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help needed with class BookType

    vector is an STL container. Sounds weird that they'd give it the same name. Do they want you to mimic the STL vector's functionality?

    Seems that the "vector" requirement contradicts the array requirement in D above.

  5. #5
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    The array was just to test whether the code is working. I tested that already.

    So perhaps just need to ignore the array part.

    The main focus will be the vector class, on how to store the book list data inside the vector class.

    thanks.. =)

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help needed with class BookType

    Quote Originally Posted by owllie View Post
    The array was just to test whether the code is working. I tested that already.

    So perhaps just need to ignore the array part.

    The main focus will be the vector class, on how to store the book list data inside the vector class.

    thanks.. =)
    So your assignment calls on you to make a dynamic array class? If so, then what's the issue? Just create a vector class and have a push_back() function, just like std::vector. Forget about the book class -- just write a dynamic array class, regardless of what it's going to hold, whether it will be int, double, a Book, a Widget, doesn't matter.

    If you don't know how to do that or where to start, then bring that issue up with your teacher. My opinion is that these types of assignments should really not be given to beginners in C++ (not just my opinion, but also the opinion of the inventor of C++, Bjarne Stroustrup). You need to know dynamically allocated memory, ciopy constructors, destructors, pointer manipulation, I could go on and on. Or you can use std::vector<T>, as GCDEF mentioned. If you are supposed to do the former, you are now entering the realm of C++ that is way beyond beginner stage. It's these types of assignments and requirements that are given to beginners that gives C++ a bad name (at least in the academic world).

    If the name of the class you're taking is not something similar to "data structures", but its more of "Programming in C++", then it begs the question of why this assignment was given with these requirements. The assignment as posted would have benn an excellent opportunity to actually learn C++ and use the various aspects of the library (vectors, algorithms such as find(), etc.). Instead, it becomes a big waste by sidetracking you into doing something that you may not be ready for (and possibly not even need to do, given that real-world C++ programmers use std::vector).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 18th, 2011 at 10:15 AM.

  7. #7
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    im taking data structures and abstractions. what the teacher told us was to just read the textbook. Which is really not doing us good. Tried googling to read up more and see the examples. But what I can learn are really limited.

    I have created a class template. Wonder if you could tell me am I correct or any part that I should amend. I don't mind putting in double effort to learn, but I just hope someone could give me a head start. My class template is as pasted below.

    //************************************************************************
    //
    //vBookType.H vector BookType class definition
    //class BookInfo
    // this class defines the vector class
    //************************************************************************

    #include <iostream>
    #ifndef VBOOKTYPE_H
    #define VBOOKTYPE_H

    using namespace std;


    template <class T>

    Class BookInfo
    {
    //vector <BookType> BookInfo; //declare vector of book
    public:

    int isEmpty(); {return top == -1;}
    int isFull();{return top == size -1;}
    void push(T&);
    void pop(T&);

    BookInfo(); //constructor
    ~BookInfo(); //{delete [] Ptr;}

    private:
    int size[1000]// number of elements
    //int top;
    T*Ptr;

    };

    template <class T>
    BookInfo<T>::BookInfo
    {
    }
    template <class T>
    void BookInfo<T>:ush(T& data)
    {
    if (!isFull())
    {
    Ptr[++top] = data;
    return 1; //push successfully
    }
    else
    {
    return 0;
    }//push unsuccessfully
    }

    template <class T>
    void BookInfo<T>:op(T& PopData)
    {
    if (!Empty())
    {
    PopData = Ptr[top--];
    return 1; //pop successfully
    }
    else
    {
    return 0;
    }//pop unsuccessfully

    }


    if im wrong, could someone please give me a head start by rectifying my mistakes?

    thanks a lot.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help needed with class BookType

    Quote Originally Posted by owllie View Post
    im taking data structures and abstractions. what the teacher told us was to just read the textbook. Which is really not doing us good. Tried googling to read up more and see the examples. But what I can learn are really limited.
    OK. But here is the problem with using C++ to create such structures:

    You have two learning curves -- how to use C++ properly and then after that, how to code a dynamic array using the book you're reading. That is the big problem with using C++ as a data structures language -- if you don't know C++ to know enough how to handle pointers, dynamically allocated memory, and a whole host of other language related issues, that data structures book is basically useless in terms of learning C++ properly.

    That's why a language like Java, where you don't have to worry about these language-related issues, is a much better language to learn data structures.
    I have created a class template. Wonder if you could tell me am I correct or any part that I should amend. I don't mind putting in double effort to learn, but I just hope someone could give me a head start. My class template is as pasted below.
    You need to use code tags when posting code, so that the formatting is preserved.

    Second, look at the public interface for std::vector. That is a much better place to start.

    As to your code, there are a lot of issues with it, again, most are language related ones and not ones that are related to the actual data structure of a dynamic array.
    Code:
    #include <iostream>
    #ifndef VBOOKTYPE_H
    #define VBOOKTYPE_H
    
    using namespace std;
    Do not specify "using namespace std" in a header file. Google or search these forums as to why you shouldn't do this (I won't explain it here -- it's been explained many times before).

    Instead, prepend your identifiers with the namespace name, or specify a "using" clause with the actual type you're using:
    Code:
    template <class T>
    
    Class BookInfo
    {
    	//vector <BookType> BookInfo; //declare vector of book
    public:
    	
    	int isEmpty(); {return top == -1;}
    	int isFull();{return top == size -1;}
                    void push(T&);
    	void pop(T&);
    Why does your class have a "pop" method? It's not a stack, it's just a dynamic array.
    Code:
    	BookInfo(); //constructor
    	~BookInfo(); //{delete [] Ptr;}
    You are missing a copy constructor and assignment operator.
    Code:
    int size[1000]// number of elements
    Sorry, but this is not a dynamic array if you start doing this. This is a fixed-sized array of 1,000 elements. If you were going to hand this in, you may have been deducted major points.
    Code:
    private:
         T* Ptr;
    That is what it should be. Then you have to write code to allocate and manipulate Ptr correctly.

    if im wrong, could someone please give me a head start by rectifying my mistakes?
    To be brutally honest, these are not just simple mistakes that take a couple of keystrokes to correct. You're missing whole parts of how to use C++ properly to create such a class you're saying you've been assigned to create.

    Regards,

    Paul McKenzie

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help needed with class BookType

    Let's tie this back to the requirements for a moment----

    Quote Originally Posted by owllie View Post
    A. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication.
    B. Design a class bookType that defines the book as an ADT.
    i. Each object of the class bookType can hold the following information about a book:
    1. title,
    2. up to four authors,
    3. publisher,
    4. ISBN,
    5. price,
    6. and number of copies in stock.
    7. To keep track of the number of authors, add another member variable.
    ii. Include the member functions to perform the various operations on objects of type bookType.
    iii. For example, the usual operations that can be preformed on the title are:
     to show the title,
     set the title,
     and check whether the title is the same as the actual title of the book.
    iv. Similarly, the typical operations that can be preformed on the number of copies in stock are :
     to show the number of copies in stock,
     set the number of copies in stock,
     update the number of copies in stock,
     and return the number of copies in stock.

    v. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and destructor (if one is needed).

    B. Write the definitions of the member functions of the class bookType.

    C. Write a program that uses the class bookType and tests various operations on the objects of the class bookType.

    D. Declare an array of 100 components of the type bookType. Some of the operations that you should perform are to search for a book by title, search by ISBN, and update the number of copies of a book.
    NONE of this says anything about dynamic arrays or vectors. Where is the requirement for doing things that way?

  10. #10
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    Quote Originally Posted by Lindley View Post
    Let's tie this back to the requirements for a moment----



    NONE of this says anything about dynamic arrays or vectors. Where is the requirement for doing things that way?
    Hi Lindley,

    I wrote the requirements for the question in a separate post.

    "Customer lists are stored using a vector class. Book lists are also stored using a vector class. You must write your own vector class to be used in exercise 7. There must be only one vector class. Note that means you can have as many vector object as you need but there is only one vector class. STL data strctures cannot be used in this assignment."


    thanks

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help needed with class BookType

    Quote Originally Posted by owllie View Post
    Hi Lindley,

    I wrote the requirements for the question in a separate post.

    "Customer lists are stored using a vector class. Book lists are also stored using a vector class. You must write your own vector class to be used in exercise 7. There must be only one vector class. Note that means you can have as many vector object as you need but there is only one vector class. STL data strctures cannot be used in this assignment."


    thanks
    Okay, so basically they're asking you to reinvent the std::vector class rather than using the existing one, so that you learn how it works internally.

    Therefore, your class should have an interface which is largely similar to std::vector. Take a look at the interface here:
    http://www.cplusplus.com/reference/stl/vector/

    Of course you do not need to support every method shown there, but a decent subset of them will be necessary.

    For the purposes of creating this vector class, you can forget all about Book objects; they aren't relevant.
    Last edited by Lindley; October 18th, 2011 at 01:32 PM.

  12. #12
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    Sorry for being blunt.. In order to store the data how should I go about it?

    Using the insert method?

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help needed with class BookType

    Quote Originally Posted by owllie View Post
    Sorry for being blunt.. In order to store the data how should I go about it?

    Using the insert method?
    Objects are usually copied into a vector using either insert() or push_back(). You can implement one of these or both. The push_back method is simpler but less flexible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured