User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 430,102 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,173 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 456 | Replies: 2
Reply
Join Date: Jan 2008
Posts: 1
Reputation: jobluz06 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jobluz06 jobluz06 is offline Offline
Newbie Poster

adding & subtracting objects from a list

  #1  
Jan 6th, 2008
I am trying to write a short RPG like program to teach myself C++. I would only like the program to allow the user to by items (sword, shield, etc.) from a store and add them to their list of items. I'd also like for these items to be objects of a class so that I can give them data members such as name and price. But I can't figure out how to put objects into something like a vector or an array. Is there a way to do that? Or is there some other way?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: adding & subtracting objects from a list

  #2  
Jan 6th, 2008
You want the user to be able to buy items. (You accidentally misspelled that.)

Your best bet is to use one of the STL collection classes with a base item type.

That is, create an abstract class for your items, and derive specific items from it. Then use something like std::set to store the items.

Hope this helps. If you get stuck post again.
Reply With Quote  
Join Date: Apr 2007
Posts: 77
Reputation: phalaris_trip is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Re: adding & subtracting objects from a list

  #3  
Jan 6th, 2008
I ran into a similar situation as yours, so I'll tell you what difficulties I encountered and how I chose to deal with them. I'm still learning though, same as yourself, so you're better off asking one of the gurus here..

Anyway, any sort of array or STL container is restricted to storing things of the same size as far as I understand. In practice this means storing things of the same type. i.e. you can have a std::vector<CSword> or std::deque<CShield>. But what you really want is to store them all in the same container, for convenience.

Well with polymorphism you can create a container of base class pointers and have those pointers point to appropriate derived classes if you wish. This seems ideal because for example you can do something like:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class CItem
  5. {
  6. public:
  7. virtual void Use() { cout << "using item\n"; }
  8. };
  9.  
  10. class CSword : public CItem
  11. {
  12. public:
  13. void Use() { cout << "using sword\n"; }
  14. };
  15.  
  16. class CShield : public CItem
  17. {
  18. public:
  19. void Use() { cout << "using shield\n"; }
  20. };
  21.  
  22. int main()
  23. {
  24. int numItems (2);
  25. CItem** items = new CItem*[numItems];
  26.  
  27. items[0] = new CSword;
  28. items[1] = new CShield;
  29.  
  30. items[0]->Use();
  31. items[1]->Use();
  32.  
  33. return 0;
  34. }

Or alternatively you can use an STL container such a map to easily identify the items by name.

However the limitations of this are that you are restricted to calling base class functions.
So consider the following modification:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class CItem
  5. {
  6. public:
  7. virtual void Use() { cout << "using item\n"; }
  8. };
  9.  
  10. class CSword : public CItem
  11. {
  12. public:
  13. void Use() { cout << "using sword\n"; }
  14. void Attack() { cout << "swoosh...\n"; }
  15. };
  16.  
  17. class CShield : public CItem
  18. {
  19. public:
  20. void Use() { cout << "using shield\n"; }
  21. };
  22.  
  23. int main()
  24. {
  25. int numItems (2);
  26. CItem** items = new CItem*[numItems];
  27.  
  28. items[0] = new CSword;
  29. items[1] = new CShield;
  30.  
  31. // This is ok because they're polymorphic calls
  32. items[0]->Use();
  33. items[1]->Use();
  34.  
  35. // This will give an error
  36. items[0]->Attack();
  37.  
  38. return 0;
  39. }

So you may not use any of the derived class' special abilities as it were, unless they are polymorphic variants of a defined base class function.
This means that either you are restricted in that you either have to constantly update your base class (bad OOP) or you have to limit your use of the objects in the containers (kind of useless in this context).

So what I ended up doing instead was making a bunch of containers of each item type and something like a CItemManager class which managed this overall item collection.
Last edited by phalaris_trip : Jan 6th, 2008 at 10:33 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC