Good Programming Practices

This is a discussion on Good Programming Practices within the C++ Programming forums, part of the General Programming Boards category; OK, I've been reading a couple different c++ books and I started making a console based text adventure game for ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Good Programming Practices

    OK, I've been reading a couple different c++ books and I started making a console based text adventure game for my kids. As I learn new things from the books I add/update my code, like when I learned about classes I made a class for the player, one for the monster, one for items you can get, etc.
    My program has gotten much bigger now and I would like to make sure that I am structuring it correctly.

    What are some good programming practices that I should follow in regards to where I should put various functions, etc. For example, I made a Player.h file where I declared my cPlayer class. Then I made a Player.cpp file where I defined all of it's members. I made a Chest.h file to create a class for chest armor, but I defined everything right where I declared it (string GetName(){return chestName;}), etc., no .cpp file for Chest.

    My main cpp file, where the main() function is located, has like 12 #include statements, a bunch of function prototypes, constant declarations, and class instance statements. Do people usually make a separate header file to hold all of the stuff before main() and then just include that just to keep things neat and tidy? I put most of my functions in this main cpp file below the main() function, but I did create a couple separate files for other functions, like I made a Wait.cpp file that pauses the game for a number of milliseconds.

    How do I know when I should put a function under the main() function or put it in it's own cpp file? What about classes?

    Also, I have a Location.cpp file where I can call functions in it from the main.cpp file, but I can't call main.cpp functions from the Location.cpp file, or I can't call the wait() function from within Locations.cpp. What do I need to add to do that?

    Thanks!
    Last edited by mark909; 01-17-2011 at 09:33 AM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Tip: for any member functions which do not alter the object on which you're calling it, declare the function as const, i.e.,

    Code:
    string GetName() const  { return chestName; }

  3. #3
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    I normally separate the executable from the rest, and with the rest I call a library. However, in my line of work, I make libraries that other people will use (hopefully). What this mean is that I don't put anything in the main other than a call to the game.

    I define in the .h only if it is very little code (like the case you showed). And I create a mygame.h which call the necessary libraries for my game to be played (like players.h, chest.h, etc.). This way I only call mygame.h.

    One suggestion is to use makefile too, if not already. unless you're in windows, in which case my suggestion is to use linux

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by nepper271 View Post
    unless you're in windows, in which case my suggestion is to use linux
    Linux is awesome. but you don't have to change the entire OS just to become a good programmer. It wouldn't make a different to use the makefile if you have an IDE to manage the project for you.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    I suggest using all lower case file names; makes it easier to build on mixed Linux and Windows boxes.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  2. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 04:38 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 03:23 PM
  4. what is good for gaphics in dos
    By datainjector in forum Game Programming
    Replies: 2
    Last Post: 07-15-2002, 03:48 PM
  5. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM

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