First time doing OO

This is a discussion on First time doing OO within the C++ Programming forums, part of the General Programming Boards category; Hey guys, this is my first time using object oriented programming and I'm having a little bit of trouble. I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    First time doing OO

    Hey guys, this is my first time using object oriented programming and I'm having a little bit of trouble. I'm trying to create a "Store" class that feeds off my "inventory" class which I have already created. I have to make four member functions: 1) add an inventory item 2) search for an inventory item 3) delete and inventory item 4) show quantity, cost of each inventory item 5) modify an inventory item. I have started it but am stuck on how to modify the item. I have attached all of the programs. The prog6.cpp is the driver for my inventory class.
    Attached Files Attached Files

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    store.h
    Code:
    #ifndef Store_H
    #define Store_H
    
    #include "inventory.h"
    #include <string>
    
    using namespace std;
    
    class Store
    {
          public:
                 static const unsigned SIZE = 500;
                 
          //  Constructors
            Store();
            Store(string ID2 , unsigned quantity , double cost);
            
          // Modification Member Functions
            void loadInventory ( const inventory &a );
            string searchInventory ( string ID2 );
            void modInventory ( string ID2, unsigned quantity, double cost );
            void removeInventory ( string ID2 , inventory &a );
            double showSum () const ;
            
            Private:
                    
          //  Member Variables 
            inventory store;
            inventory item[SIZE];
            unsigned size;
    };
    
    #endif 
    I'll assume those last couple lines being missing from the file is a typo. Also, Private should be private. SIZE cannot be initialized in the manner you have. As a static member, you would need to create and initialize the member variable in the store.cpp file. You would also need a change to how your inventory array was declared. You'd need to make it a pointer instead of an array and dynamically allocate space for it in the constructor and free it in the destructor. I would therefore strongly suggest using an STL container class such as list or vector which would make adding and deleting items easier to deal with and far more flexible than any standard array or pointer. You'd also be able to get rid of the SIZE and size data members since the container would be able to tell you itself how many items it was holding at any given time.




    store.cpp
    Code:
    #include <iostream>
    #include <iomanip>
    #include <ctype.h>
    #include "Store.h"
    
    using namespace std;
    
    // Constructors
    
    Store::Store() { size=0;}
    
    Store::Store(string ID2, unsigned quantity, double cost)
    {
         store.setInventory(ID2, quantity, cost);
           size=0;
    }
    
    //Constant Member Functions
    
    void Store::loadInventory (const inventory &a )
    {
         item[size++] = a;
    }
    
    string Store::searchInventory () const
    {
           string search;
           cout << "Which product are you searching for?" << endl;
           cin >> search ;
           while (search != ID2)
           {
                 item[size++];
           }return search;
    }
    
    void Store::modInventory ( unsigned quantity, double cost );
    {
         char choice;
         unsigned quantity = 0;
         double cost = 0;
         
         cout << "Do you want to change to price of item? Y/N" << endl;
         cin >> choice;
         if choice = 'Y' or 'y'
         (
    Again, is the fact you seem to be chopping off source code at the bottom merely a typo? There is a difference between = and ==. The above if test should be if( choice == 'y' || choice == 'Y' ). In the above while loop where you do search != ID2 there is no ID2 variable in scope. The inclusion of a second constructor doesn't seem to do anything. You initialize the store member variable but don't add it to the array (size is still 0)? A better way to initialize the store member would be to use an initialization list. The second constructor could be rewritten (if it really was necessary) as this:
    Code:
    Store::Store(string ID2, unsigned quantity, double cost) : store(ID2,quantity,cost), size(0)
    {
         // Don't need to do anything here since it's already been done in the initialization list.
    }


    inventory.cpp
    Code:
    #include "inventory.h"
    #include <iostream>
    using namespace std;
    
    
    
        
        inventory::inventory()
        {
             ID = "UNDEFINED" ;
             quantity = 0;
             cost = 0;
        }
        void inventory::setString(string ID2)
        {
             ID2 = "A";
             
        }
        void inventory::setInventory(string ID2, unsigned quantity, double cost)
        {
             setString(ID2);
             setQuantity(quantity);
             setCost(cost);
        }
    Did you mean ID = ID2;? The above line as it stands does nothing. It will modify the local copy of ID2 to "A" and then when the function exits, the value will be thrown away as the variable is popped off the stack and nothing will have changed in the inventory object itself. A better name for the function might be setID. Also, you're still missing the rest of the functions in the source file, where are they? Is this another typo or simple omission of code?

    Quote Originally Posted by Kross7
    I have started it but am stuck on how to modify the item.
    Based on the code attached, it would seem you're stuck on just about everything since I see nothing to get user input for new inventory items and add it to a store's inventory list. Start with that, show us some code that gets user input and adds an inventory item to the store. Then we can move on from there to removing an item.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Getting there I think

    Ok I think I fixed all of those little things you mentioned except for the whole pointer thing that had to do with me declaring my SIZE constant. The one major thing I am having trouble with is my searchInventory, removeInventory and modInventory because I'm not quite sure how to sort through the array searching for a string. Does that make sense? I have also added a new file to act as my driver for the store class.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    88
    Code:
    //Instead of this
    static const unsigned SIZE = 500;
    //Try this
    enum {SIZE = 500};

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    OK

    Ok I tried the
    Code:
     enum { SIZE = 500};
    but I still ned help on sortingt through my array using strings. Any ideas or help would be greatly appreciated.

    Thanks

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
       static const unsigned SIZE = 500;
    SIZE cannot be initialized in the manner you have. As a static member, you would need to create and initialize the member variable in the store.cpp file.
    Not true. Static constant integral members can be initialised in the header for this very reason.
    Quote Originally Posted by c++ standard 9.4.2.4
    If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression. In that case, the member can appear in integral constant expressions.
    Quote Originally Posted by UMR_Student View Post
    Code:
    //Instead of this
    static const unsigned SIZE = 500;
    //Try this
    enum {SIZE = 500};
    you can do this, but it's not necessary. Personally, I think it's bad style (it was originally a hack to get around the lack of static constant integrals), but it works.

    Quote Originally Posted by Kross7
    but I still ned help on sortingt through my array using strings. Any ideas or help would be greatly appreciated.
    Can you be a bit more specific? What do you mean by "sortingt through my array using strings"?
    Last edited by ChaosEngine; 05-09-2007 at 05:44 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Yea, the project I am doing calls for three functions. One to search through an array of objects, one to remove items from the array and one to modify items in the array. I'm wondering how to get to the different items in an array using a for loop, but I don't know what to label the item in the loop. The only real way to search for the item is by its ID which I think is a variable named ID2 which is a string.

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    You probably want a std::map. check out the stl link in my sig
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Can't use those...

    The class im taking does not let us use functions we havent learned yet so i can't use that. We are only supposed to use the functions i have labeled in my store.h file, if that makes sense. Any other suggestions?

  10. #10
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    the best thing about OO is that it pushed you (C++ programmer) to your limits, cuz u have to do extra work like assignment operator and copy constructor if you have dataon the heap and stuff.
    keep up the great work
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

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