Thread: New to C++ Classes

  1. #91
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    This is the constructor where I placed the variables.
    Code:
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}
    I'm going to be trying different things on my own, but tell me if you know that I did something wrong.

  2. #92
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to show a lot more of your code for people to get a good feel for the problem. Shoe the header file. Show the functions in the source file. Show the errors. show what progress you have made so far. That single line you posted looks correct if placed in a source file and if the four member variables can take integers for their intializers.

  3. #93
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Header File
    Code:
    #ifndef GameManager_H_GUARD
    #define GameManager_H_GUARD
    
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    using namespace std;
    
    class GameManager
    {
    private:
           int money;
           int lives;
           int attack;
           int defense;
           string battle;
           string shop;
           char restart;
           string ShopBattle;
    
    public:
           GameManager();
           void MainManager();
           int GetAttack();
           int GetDefense();
           int GetLives();
           void Lives();
           int GetMoney();
           void AddAttack(int amount);
           void AddDefense(int amount);
           void SetMoney(int newmoney);
           void SpendMoney(int amount);
           void AddMoney(int amount);
    };
    #endif
    Source file
    Code:
    #include "GameManager.h"
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}
    
    void GameManager::MainManager()
    {
         ShopClass shop;
         BattleClass battle;
         cout << "Current Status:\n";
         cout << "Money: $" << money << "\n";
         cout << "Lives: " << lives << "\n";
         cout << "Attack Points: " << attack << "\n";
         cout << "Defense Points: " << defense << "\n";
         cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
         cin >> ShopBattle;
         
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                        shop.ShopMain();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                battle.BattleMain();
         } else {
                cout << "Error: Invalid Input";
         }
    }
    int GameManager::GetAttack()
    {
        return attack;
    }
    int GameManager::GetDefense()
    {
        return defense;
    }
    int GameManager::GetLives()
    {
        return lives;
    }
    void GameManager::Lives()
    {
         lives -= 1;
    }
    int GameManager::GetMoney()
    {
        return money;
    }
    void GameManager::AddAttack(int amount)
    {
         attack += amount;
    }
    
    void GameManager::AddDefense(int amount)
    {
         defense += amount;
    }
    void GameManager::SetMoney(int newmoney)
    {
         money = newmoney;
    }
    
    void GameManager::SpendMoney(int amount)
    {
        SetMoney(money - amount);
    }
    
    void GameManager::AddMoney(int amount)
    {
         SetMoney(money + amount);
    }

  4. #94
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's the latest error message?

  5. #95
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    There is no error message
    ..the variables seem to be resetting themselves according to the last action taken. i.e. I'll buy some armour and the defense/money variables will go up/down accordingly. Then, I will battle a robot and I will lose, like I should, but then the money is back at $200, the amount you start with, and the defense is at 0 but the lives is at 2, one less. What do I need to do with the variables to make this stop?

  6. #96
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you calling MainManager() over and over again from main()?

    Inside MainManager, you have a ShopClass object and a BattleClass object. Each time you call MainManager, a new object will be created for each and initialized with the initial values. If you want these items to remember their previous values, they should be member variables of the GameManager class.

  7. #97
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    So can I just take out the constructor in GameManager and declare the variables in the header file?

  8. #98
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure what you mean. This doesn't have anything to do with the constructor.

    The ShopClass and BattleClass declarations inside the MainManager function should be removed and placed as member variables inside the GameManager class.

  9. #99
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    When you say member variables do I just put them in with the "private" variables? I really don't know what member variables are and tutorials on Google aren't very helpful with that.

    The program ran but the variables are still messed up. Here is the header and the source files:
    It wont let me attach .exe files so I can't show you what the program is doing.

    Code:
    #ifndef GameManager_H_GUARD
    #define GameManager_H_GUARD
    
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    using namespace std;
    
    class GameManager
    {
    private:
           int money;
           int lives;
           int attack;
           int defense;
           string battle;
           string shop;
           char restart;
           string ShopBattle;
           ShopClass Shop;
           BattleClass Battle;
    
    public:
           GameManager();
           void MainManager();
           int GetAttack();
           int GetDefense();
           int GetLives();
           void Lives();
           int GetMoney();
           void AddAttack(int amount);
           void AddDefense(int amount);
           void SetMoney(int newmoney);
           void SpendMoney(int amount);
           void AddMoney(int amount);
    };
    #endif
    Code:
    #include "GameManager.h"
    #include "ShopClass.h"
    #include "BattleClass.h"
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    GameManager::GameManager() : money(200), lives(3), attack(0), defense(0) {}
    
    void GameManager::MainManager()
    {
         int money;
         int lives;
         int attack;
         int defense;
         cout << "Current Status:\n";
         cout << "Money: $" << money << "\n";
         cout << "Lives: " << lives << "\n";
         cout << "Attack Points: " << attack << "\n";
         cout << "Defense Points: " << defense << "\n";
         cout << "Would you like to visit the shop or battle robots?\n(Please enter either Shop or Battle)\n";
         cin >> ShopBattle;
         
         if (ShopBattle == "Shop" || ShopBattle == "shop") {
                        Shop.ShopMain();
         } else if (ShopBattle == "Battle" || ShopBattle == "battle") {
                Battle.BattleMain();
         } else {
                cout << "Error: Invalid Input";
         }
    }
    int GameManager::GetAttack()
    {
        return attack;
    }
    int GameManager::GetDefense()
    {
        return defense;
    }
    int GameManager::GetLives()
    {
        return lives;
    }
    void GameManager::Lives()
    {
         lives -= 1;
    }
    int GameManager::GetMoney()
    {
        return money;
    }
    void GameManager::AddAttack(int amount)
    {
         attack += amount;
    }
    
    void GameManager::AddDefense(int amount)
    {
         defense += amount;
    }
    void GameManager::SetMoney(int newmoney)
    {
         money = newmoney;
    }
    
    void GameManager::SpendMoney(int amount)
    {
        SetMoney(money - amount);
    }
    
    void GameManager::AddMoney(int amount)
    {
         SetMoney(money + amount);
    }
    Here's the .exe file
    http://z02.zupload.com/download.php?...filepath=36613

  10. #100
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> When you say member variables do I just put them in with the "private" variables?
    Yes, exactly. The variables inside the class declaration (which should usually be under private) are members of that class. Your changes to this class look good.

    Where is the code that changes the money? For example, you call ShopMain and BattleMain, but what do those things do? You have AddMoney and SpendMoney, but where is the code that calls that to make the changes?

  11. #101
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    I have a battle and shop class and ShopMain and BattleMain are functions in the classes and they call on the AddMoney and other functions in GameManager.

    Did you try the game? The numbers are all screwed up.

  12. #102
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    more files

  13. #103
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Did you try the game?
    Sorry, I only have time to glance at posted code... pressing deadline.

    Try looking for a similar mistake that you just fixed. Maybe one of the other classes changes a money variable but that doesn't actually get saved. Try and pay attention to objects and how long they stay alive. In the previous bug, your ShopClass object that was declared inside MainManager only had a lifetime as long as that function was running. When the function returned, all the data held in the ShopClass was lost and the next time you ran MainManager you started fresh with new data. Same for BattleClass.

    Look at your variables in those classes to see if something similar is happening.

  14. #104
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    Alright, I fixed that problem. I had the variables in the MainManager function too, I took them out and it fixed that. I'm still getting that problem where every time the program goes back to the MainManager, it must run the constructor and it sees the variables there and it sets the variables to what they are declared in in the constructor. How can I fix this? Where do I declare the variables?

  15. #105
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm still getting that problem where every time the program goes back to the MainManager, it must run the constructor and it sees the variables there and it sets the variables to what they are declared in in the constructor.

    I don't understand why you are saying this. Can you post the snippet of code that you think is running the constructor inside MainManager?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM