Help needed with class BookType
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Help needed with class BookType

  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
    Posts
    12,068

    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
    Posts
    12,068

    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,426

    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,426

    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
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    sorry i dont know what you meant by code tag.

    dynamic array means that you doesnt give a fixed array to it. Am I correct?
    then once i know the size of it i will allocate it with the new operator and save that address of the memory in the pointer.

    can you list the copy constructor and the assignment constructor??

    I know im missing whole parts of it, but I willing to learn.

    Really hope you could help me.

    thanks.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Help needed with class BookType

    Quote Originally Posted by owllie View Post
    sorry i dont know what you meant by code tag.

    dynamic array means that you doesnt give a fixed array to it. Am I correct?
    then once i know the size of it i will allocate it with the new operator and save that address of the memory in the pointer.

    can you list the copy constructor and the assignment constructor??

    I know im missing whole parts of it, but I willing to learn.

    Really hope you could help me.

    thanks.
    Dynamic means it grows and shrinks according to the number of elements you want in it.

    This board can't teach the fundamentals of programming such as how to write copy constructors. We can help if you get stuck somewhere, but the fundamentals you need to get from a book or a class.

  11. #11
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    is it possible to provide me with an example code for the class template for my case??

    thanks.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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?

  13. #13
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Help needed with class BookType

    Quote Originally Posted by owllie View Post
    is it possible to provide me with an example code for the class template for my case??
    Well as I stated previously, what you want is not just a couple of lines of code here and there. Writing C++ classes properly is an entire concept that needs to be learned.
    can you list the copy constructor and the assignment constructor??
    Assignment operators and copy constructor:
    Code:
    BookInfo(const BookInfo& ); // copy constructor
    BookInfo& operator=(const BookInfo& ); // assignment operator
    Those are assignment operators and copy constructor for a class called BookInfo. Just for now, create these two functions, but do not implement them. Also, make them private in your template class. This would at least prevent copying and assigning. If you attempt to copy or assign, you will get a compiler or linker error.

    If you did not implement these functions as stated above, then your program will just blow up as soon as you assign one vector to another, pass or return a vector by value, etc. That's why we declare these functions but do not implement them, so that the compiler will flag us down for any copy/assignment usage we may have inadvertantly done.

    More issues with your code:
    Code:
    Class BookInfo
    First, "class" is not spelled with a capital 'C'.

    Second, it makes no sense creating a template class called "BookInfo". It should be called MyVector, or DynArray, or something that denotes the data structure.

    Regards,

    Paul McKenzie

  14. #14
    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

  15. #15
    Join Date
    Oct 2011
    Posts
    11

    Re: Help needed with class BookType

    Quote Originally Posted by Paul McKenzie View Post
    Well as I stated previously, what you want is not just a couple of lines of code here and there. Writing C++ classes properly is an entire concept that needs to be learned.
    Assignment operators and copy constructor:
    Code:
    BookInfo(const BookInfo& ); // copy constructor
    BookInfo& operator=(const BookInfo& ); // assignment operator
    Those are assignment operators and copy constructor for a class called BookInfo. Just for now, create these two functions, but do not implement them. Also, make them private in your template class. This would at least prevent copying and assigning. If you attempt to copy or assign, you will get a compiler or linker error.

    If you did not implement these functions as stated above, then your program will just blow up as soon as you assign one vector to another, pass or return a vector by value, etc. That's why we declare these functions but do not implement them, so that the compiler will flag us down for any copy/assignment usage we may have inadvertantly done.

    More issues with your code:
    Code:
    Class BookInfo
    First, "class" is not spelled with a capital 'C'.

    Second, it makes no sense creating a template class called "BookInfo". It should be called MyVector, or DynArray, or something that denotes the data structure.

    Regards,

    Paul McKenzie
    Starting to understand it.. so to store the data, how should I write it?

    thanks for the copy constructor and the assignment constructor.

Page 1 of 2 12 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center