can i have an example, please?

This is a discussion on can i have an example, please? within the C++ Programming forums, part of the General Programming Boards category; I have to write classes to implement an inventory list simulation for a bookstore. i don't really know how to ...

  1. #1
    Registered User
    Join Date
    Oct 2002

    Exclamation can i have an example, please?

    I have to write classes to implement an inventory list simulation for a bookstore. i don't really know how to start the store class. can some give me an example of a store class and types of functions that may go in that class?

    Here are some of the requirements for it:

    Write a class called Store (filenames are store.h and store.cpp). A Store object should contain at least the following information: the amount of money in the store cash register and a book inventory list. There is no size limit to the inventory list, so it should be implemented with a dynamically allocated array. (This means you'll need an array of Book objects). You can add any public or private functions into the Store class that you feel are helpful. In addition to the Store class itself, you will also create a menu program to manage the inventory of books. However, the Store class will be able to do much of the work, as the Store member functions will be the interface between the menu program and the internally stored data (cash register and list of books).

    Rules for the Store class:

    All member data of class Store must be private

    There should be no cin statements inside the Store class member functions. To ensure the class is more versatile, any user input (i.e. keyboard) described in the menu program below should be done in the menu program itself. Design the Store class interface so that any items of outside information are received through parameters of the public member functions.

    The list of Books must be implemented with a dynamically allocated array. There should never be more than 5 unused slots in this array (i.e. the number of allocated spaces may be at most 5 larger than the number of slots that are actually filled with real data). This means that you will need to ensure that the array allocation expands or shrinks at appropriate times. Whenever the array is resized, print a message (for testing purposes) that states that the array is being resized, and what the new size is.

    Example: "** Array being resized to 10 allocated slots".
    Since dynamic allocation is being used inside the Store class, an appropriate destructor must be defined, to clean up memory. The class must not allow any memory leaks

  2. #2
    Registered User
    Join Date
    Mar 2002
    class House
       int age;
          int rooms;
          double price;
    This is a simple class. age, rooms, and price are all data members of the class and that they are declared just like any other non class variables, except they are grouped according to access rights. age is private by default declaration of access rights in all class objects, price is private by explicit declaration of access rights, and rooms is public (public access must always be declared explicity in a class). The problem with this class is that no one but the programmer writing the program or member functions of the class itself can access age and price because they are private. That's also the good news, since you probably don't want people/functions outside the class mucking around with the data like they can with public data members like rooms. To grant access to this information you need to declare some functions to access age, price. Maybe add something like:

    int get_age();
    void set_age(int);

    to the public section. Then you need to define get_age() just like you need to define non member functions. You do that outside the class declaration as a general rule (there is always an exception of course).

    int House::get_age()
      return age;
    void House::set_age(int Age)
       age = Age;
    Note the slight twist in these declarations in that you have to indicate which class the funtions are members of, if they are member functions to begin with.

    These public member functions are the interface that the directions talk about.

    To include a data member that's an array, you just declare it in the usual way and grant it some sort of access: private by default
    private by explicit, or public (there's also protected access but let's skip that for now).

    To use dynamic memory to declare an array, declare the array as a pointer to the appropriate type, then in the constructor or one of the other public accessor methods declare memory dynamically using the new operator just like you would outside the class environment. You delete this dynamically allocated memory just like you ordinarily would, but generally you do it in the destructor of the class, although I suppose you could do it elsewhere if you have a valid reason to do so.

    To keep track of the size of and the number of elements in a dynamically declared array so you can resize the array as appropriate, you will need appropriate data members in the class. If you make these data elements private but you want them accessable outside the class then you need to provide the appropriate interface to do so.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21