Like Tree1Likes
  • 1 Post By jwenting

Need Help With Classes For A RPG

This is a discussion on Need Help With Classes For A RPG within the C++ Programming forums, part of the General Programming Boards category; I know, I know... I can easily find hundreds of tutorials online about making classes in C++ for text-based RPG's, ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    Need Help With Classes For A RPG

    I know, I know... I can easily find hundreds of tutorials online about making classes in C++ for text-based RPG's, which I have!.. but I really need a better explanation and step-thru than the ones I've been finding..

    First up for my RPG, I need a class for my character.. what would it look like? This is all that I've done for my code... Beware, it looks hidieous, mostly because I have no idea how to set up classes..


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    int main()
    {
    
        class Player {
        private:
        int health;
        int attack;
        int defend;
    
        public:
        void move();
        void attackmonster();
        void drinkpotion() { health++;}
        };
    }
    First up, I feel like I do not have the effecient header files needed. Second, I have no idea what to do with the variables in my "public" and "private", I just got it from a forum from another site I found on google, and switched some of the variable names up...


    Can any one walk me through this plsssss =(

    I am so confused ...................

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Copy Paste won't help you much in understanding what is going on.
    This probably will :Classes (I) - C++ Documentation
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    67
    Can you please explain what is going on during the "Constructor" and "Destructor".. I have read the same article multiple times, but I don't really understand how to use it in a program. Thank you for the reply!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Just simply put, constructor is called once the object is first created. The destructor is called once the object goes out of scope. That's all.
    So we use the constructor to make the object ready for use (ie initialize values, attain resources, etc), and the destructor to clean everything up (release any allocated resources).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    tip: move your class out of your main method. In fact you should move it to a separate header and implementation file (1 set per class or closely related group of classes).

    Start organising your code properly at the start, much easier than reorganising it at a later stage
    cplusplusnoob likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 03:36 AM
  3. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  4. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  5. Accessing Classes in Classes
    By GrNxxDaY in forum C++ Programming
    Replies: 18
    Last Post: 07-30-2002, 01:54 PM

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