0

I am a virtual beginner in programming and am trying to understand a simple part of Object oriented programming. I apologize in advance if the answer to my question seems obvious, but I have tried to find similar examples online using google and youtube but sadly nothing fits the bill. With that said here is my question.

I am trying to make a simple video game inventory similar to the ones you would see in RPGs such as Final Fantasy VI or Chrono Trigger. What I am having trouble with is creating the items of the inventory. What I'd like help with is how to create one object that defines one item, for example a potion. I know it will take multiple parameters which are listed here:

  1. itemText = "Potion";
  2. itemDesc = "Regain 50 HP";
  3. itemEffect = playerHP + 50;
  4. buyCost = 50;
  5. sellCost = 25;

What I'd like help with is syntax of the connection of these parameters in one object. Later I will use the data so that I can create an array that shows only the itemText and a Quantity for the player to see. Any and all help or places would be appreciated.

Thank you in advance, Delita

2
  • Perhaps you are trying to create a constructor for the object? cplusplus.com/doc/tutorial/classes Commented Dec 24, 2013 at 19:37
  • so whats your problem exactly? Commented Dec 24, 2013 at 19:55

2 Answers 2

1

Perhaps you're looking for a design to help you get started.
Since you said you're a beginner, I'll avoid polymorphism, templates, and all that other fluff.
Here's a very basic design:

ItemProperty : Says what an item does and by how much.

struct ItemProperty
{
    enum effectType {
        PLAYER_HP,
        PLAYER_MP,
        CURE_POISON,
        REVIVE,
        ERROR
    } ;

    ItemProperty (const effectType effect = ERROR, int magnitude = 0) ;

private:
    int magnitude ;
    effectType effect ;
};

ItemProperty::ItemProperty (const effectType effect, int magnitude) : effect (effect), magnitude (magnitude)
{
}


Item: The stuff you wanted in an item.

class Item
{
public:
    Item () ;
    Item (const std::string &name, const std::string &description, const ItemProperty &itemProperty, 
        const int buyValue, const int sellValue) ; 

private:
    std::string name ;
    std::string desciption ;
    ItemProperty itemProperty ;
    int buyValue ;
    int sellValue ;
};

Item::Item () : buyValue (0), sellValue (0)
{
}

Item::Item (const std::string &name, const std::string &description, const ItemProperty &itemProperty, 
    const int buyValue, const int sellValue)
    : name (name), desciption (description), itemProperty (itemProperty), 
        buyValue (buyValue), sellValue (sellValue)
{
}


Main: Creating a potion

int main (void)
{
    Item potion ("Potion", "Regain 50 HP", ItemProperty (ItemProperty::PLAYER_HP, 50), 50, 25) ; 

    return 0 ;
}

Once you have a better grasp on programming, you'll be able to figure out better designs than this. But for now, keep it simple.

Sign up to request clarification or add additional context in comments.

Comments

0

There are a few levels to this question, so I will give you just the simplist part. What you want to use is something called a class, which defines the data structure and functionality of your objects. You then create as many instances of that class as you need.

(I have omitted various optimisations and common programming practices (such as putting classes in their own files, passing objects to functions by reference from the example code below since you've said you are new - you should google both these things when you're more experienced!)

#include <string> // include the code for using std::string
#include <iostream> // include the code for using std::cout and std::endl

// Class Declaration

class Item
{
public:
    // constructor, used when creating an instance of the object
    Item(std::string itemName, std::string itemDesc, int effect, int buyCost, int sellCost);

    // member functions, used to get copies internal data
    std::string getItemName();
    std::string getItemDesc();
private:
    // private member variables, where internal data is stored
    std::string m_itemName;
    std::string m_itemDesc;
    int m_effect;
    int m_buyCost;
    int m_sellCost;
};

// Class definition

Item::Item(std::string itemName, std::string itemDesc, int effect, int buyCost, int sellCost)
{
    m_itemName = itemName;
    m_itemDesc = itemDesc;
    m_effect = effect;
    m_buyCost = buyCost;
    m_sellCost = sellCost;
}

std::string Item::getItemName()
{
    return m_itemName;
}

std::string Item::getItemDesc()
{
    return m_itemDesc;
}

// main function
int main()
{
    // create a new instance of your Item class, using the parameters you specified in your question
    Item potion("Potion", "Regain 50 HP", 50, 50, 25);

    // output the results of your new instance's functions
    std::cout << "Item Name: " << potion.getItemName() << std::endl;
    std::cout << "Item Description: " << potion.getItemDesc() << std::endl;

    // Create a second instance of your class with different parameters
    Item superPotion("Super Potion", "Regain 100 HP", 100, 75, 50);

    // output the results of your second instance's functions
    std::cout << "Item Name: " << superPotion.getItemName() << std::endl;
    std::cout << "Item Description: " << superPotion.getItemDesc() << std::endl;

    return 0;
}

output: Item Name: Potion Item Description: Regain 50 HP

2 Comments

For the constructor, consider using const std::string&, for the accessor, consider const std::string& getItemName() const. PS: I am not the -1 person,
@XiaogeSu I said in the description - i left that out for simplicity as they're new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.