A little help with member functions

This is a discussion on A little help with member functions within the C++ Programming forums, part of the General Programming Boards category; Okay, I am working on a text based RPG and this is my first time using functions within classes. I ...

  1. #1
    Me pwn you.
    Join Date
    Feb 2006
    Location
    At my computer or in my bed.
    Posts
    46

    A little help with member functions

    Okay, I am working on a text based RPG and this is my
    first time using functions within classes. I want to know how to do this because someone said something about declareing
    the function inside the class and then defineing it outside.
    Am I suposed to define it inside or outside? I went by what they
    said an I got me a buch of soandso undeclared first use this function.
    I don't care if you guys need this info or not I'm going to provide it anyway:

    Compiler: Dev-CPP V: 4.9.8.0
    OS: Windows ME

    Any help would be apricated...

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    first declare the function inside the class
    Code:
    class myclass
    {
       int foo();
    };
    next, write the implementation code in a *.cpp file
    Code:
    int myclass::foo()
    {
       cout << "this is function foo" << endl;
       return 0;
    }
    If the function is short enough, you can combine the above two (called inline code)
    Code:
    class myclass
    {
       int foo()
      {
         cout << "this is function foo" << endl;
         return 0;
      }
    };
    >>I don't care if you guys need this info or not I'm going to provide it anyway

    Yes, please always provide that info when you make an initial post to avoid any possibiliity of confusion. The answer to some questions depend on it.
    Last edited by Ancient Dragon; 02-22-2006 at 12:26 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Am I suposed to define it inside or outside?
    Generally, you will define the functions outside the class and in a separate file. However, when you are learning, the first step is to define them inside the class.

  4. #4
    Me pwn you.
    Join Date
    Feb 2006
    Location
    At my computer or in my bed.
    Posts
    46
    Okay, I usualy write all of my code inside ONE FILE... Seeing how it's okay to do that
    I don't see any reason to split my project into multiple files when It will probalby only
    tike like 1,500 lines anyway. Thanks for the help... That's exactly what I needed.
    Last edited by Dagger; 02-22-2006 at 01:14 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    doing is all in one file with such a small program is ok. But that doesn't teach you how to use multiple files like its done on huge programming projects in the real world. You can bet your boots that Microsoft Word was not written in just one file!

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    M$ Word was done in 2 files! I_DONT_CARE.CPP and SCREW_THE_CUSTOMER_OUT_OF_HUNDREDS_FOR_THIS.H

    In reality though. You should take this project to learn how to use includes to include other source files that you are writing. You will run into the errors you don't expect, and it will teach you. I try and keep my files under 1000 lines if I can. Makes for easier reading and debugging.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    first time using functions within classes.
    In reality though. You should take this project to learn how to use includes to include other source files that you are writing.
    Wrong.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    For experimenting with functions in classes that is one thing, but if you read his post, he wants to make a text based game, in that project he will do better if he splits up his code.

  9. #9
    Me pwn you.
    Join Date
    Feb 2006
    Location
    At my computer or in my bed.
    Posts
    46
    Hmmmm I will do better if I split up my code? Why, it's a console project.
    One would think that a little text based game is not worthy of more than one file....
    Am I incorrect?

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Well a text based rpg is going to need to keep track of where the player is on the map, read in the map(probably from a file), the player's stats, the enemy's stats, combat, where the enemies are, information about each room, gear, classes, and if you want them races. I am probably missing some stuff.

    Somehow I don't think that will look very nice in one file. I split even my console programs up in order to debug them easier and make them more modular.

  11. #11
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Code:
    class myclass
    {
       int foo();
    };
    One thing: When you use the above code, the default is private. This means that the function will be inaccessible. If you want to call it from a function not inside myclass, use this:

    Code:
    class myclass {
    public:
         int foo();
    };

  12. #12
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    my opinion for your first game, especially if you're just learning classes, is to just use the one file. you'll have PLENTY of problems trying to get everything to work right without the hassle of learning how to include multiple files at the same time. not that it's hard, just don't overwhelm yourself with everything at once.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  13. #13
    Me pwn you.
    Join Date
    Feb 2006
    Location
    At my computer or in my bed.
    Posts
    46
    I never said that it's my first game. It's not. It's not my first game with calsses.
    I know about classes, I've make like three games with classes before. It's my first game
    with FUNCTIONS THAT ARE MEMBERS OF CLASSES.

    But anyway, I have all of that crap working now. I have one request for advice:

    In this game, you may only load one character at a time. Should I have a class for the
    character, or have 465446545645 global variables? The class version seems more
    neat, clean, organized, but it seems to have no perpose other than that.
    Like items, I will have a lot of items, so it makes sense to have a class for items, but
    if there will be only one character at a time, should I use a class or not?

  14. #14
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Generally the monster's stats have something in common with the player's stats. So you can use a common parent class and then using inheritance make a class derived from that. The derived classes will have a couple more things to keep track off for both monsters and players, but by using inheritance you can make your spells able to be used by the enemy or by the player and not need to worry about stats and/or otherthings lining up, since the base of the player is the same as the base of the monsters.

  15. #15
    Me pwn you.
    Join Date
    Feb 2006
    Location
    At my computer or in my bed.
    Posts
    46
    Well, I don't intend on having my monsters have 16 stats slots and be able to hold 18
    items or cast 10 spells... The monster/player class was a good idea, but I already started coding and I would have to change a lot to make that work. But I did find out, Wraithan,
    that you were right about the files, there is no way I'm going to be able to have all of this
    code in one file. Am I going to have to do it all through headers, or what? On my system
    All the headers have to be in a certan spot (C : \dev-cpp\include) or they won't work
    when I #include <header.h>. Is there a way to get dev-Cpp to include them from another dir?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member functions can't see private data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2006, 02:36 PM
  2. issue with member functions
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2006, 10:18 PM
  3. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 08:38 AM
  4. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 04:52 PM
  5. trouble defining member functions
    By dP munky in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2003, 09:52 PM

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