im just starting to learn classes i understand some of them now but im still confused by alot..
for example im trying to create a player class but would i put the players stat variables in private public or protected?
im just starting to learn classes i understand some of them now but im still confused by alot..
for example im trying to create a player class but would i put the players stat variables in private public or protected?
It depends. If you intend to create derived classes of the player class, then you might want to make the variables protected; otherwise, you'd probably make them private. (Making them public wouldn't be a good idea because it bypasses encapsulation.)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>would i put the players stat variables in private public or protected?
Private. Unless you have a very good reason and can defend that reason against people who are way smarter than you, all data members should be made private and all access controlled through public or protected member functions.
My best code is written with the delete key.
thanks ive been using zeuscmd and it put them in public so i wasnt sure thanks yall
It really depends on you how to create ur player class. The general trend among programmers is that they declare datatypes in the private section of the class and provide functions in the public section of the class to access these datatypes but this is not the rule to do it.
Look if u want that your datatypes must only be accessed only by the functions belonging to the player class then declare them as private. If you r going to derive some new class from the player class and u intend to use the variables defined in the player class in ur newly created class then declare them as protected and if u want that everybody should access those variabels then declare them as public.
Making variables public is like using a structure. It defeats the purpose of a class. Admittedly, sometimes you need to use public variables, but "players stat variables" should almost definitely be private.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>> If you r going to derive some new class from the player class and u intend to use the variables defined in the player class in ur newly created class then declare them as protected and if u want that everybody should access those variabels then declare them as public.
You can do those things if you'd like, but it is generally bad design.
now im confused about constructors
in that stupid zeus tutorial it shows
then it goesCode:player() player(int h) player(int h,int m) int health; int mana;
theres no possible way you could do this it doesnt make any senseCode:player() { health = 100; mana = 50; } player(int h) { health = h; mana = 50 } player(int h,int m) { health(h); mana(m); }
player()
player(int h)
player(int h,int m)
what u see above in the first three lines is called function overloading i think u must have read about it before if not i would suggest u to read about it from a good book. It is one of the features of C++ and is of great help. Try putting urself in a situation where u have to initialize the variable/variables of player class differently ( as shown in ur code) and u will appreciate function overloading.
I have trouble believing that's what's in the tutorials because of the random missing semicolons. I'm sure they don't miss semicolons in the tutorial, which means that's not an exact copy of the tutorials, which leads me to believe you've made other mistakes, specifically the lack of the scope resolution on your constructor definitions. Now other than those syntax errors there isn't much wrong with those constructors that I can see. They could be simplified with some initialization lists or you could use default values in your arguments rather than the void argument constructor (though, I prefer a void argument constructor), but that's about it. What do you see wrong with them?
Sent from my iPadŽ
But what's this? It's not an initializer list . . .
And why the missing semicolons? And why the lack of class qualifiers (such as theclass::theclass) for the constructor definitions?Code:health(h);
You might want to try a different tutorial.
[edit] Sly beat me to it. [/edit]
[edit=2] Sorry, my apologies to whoever wrote the tutorial; I thought the code above was exactly what it contained. [/edit]
Last edited by dwks; 08-29-2006 at 08:21 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
ive heard of operator over loading but thought it was too advanced to get into so fast but ill take a look at it
sly
no its not an exact copy this is
http://www.zeuscmd.com/tutorials/cpl...nstructors.php
This is why you copy the code exactly. This is an initialization list... what you had was just an obscure use of parentheses to initialize when the tutorial would appear to be using the equal sign the whole time. You seem to think code you don't understand must automatically be incorrect and you try to fix it. What you seem to not realize is that you don't yet understand all that much. Don't ignore what appears new to you, try to understand it... that's how you learn.Originally Posted by http://www.zeuscmd.com/tutorials/cplusplus/47-Constructors.php
Last edited by SlyMaelstrom; 08-29-2006 at 08:25 PM.
Sent from my iPadŽ
If I was you I would use inherientence and make a base class that declares all the player and enemy data members, make them all "protected" and then create a new class called Player that can acsess the variables of the base class
eg:
// player classCode:class Base { public: Base() {}; ~Base{}; protected: int m_hp; long m_score; short m_mp; private: string m_pNm; };
Just an idea. If you have trouble understanding inherientence, read up on it or google itCode:class Player : public Base // player class can use Base class members { public: Player() {}; ~Player() {}; // player class functions here };
good luck in your game! - pete
>If I was you I would use inherientence and make a base class that declares all
>the player and enemy data members, make them all "protected" and then
>create a new class called Player that can acsess the variables of the base class
Why? What reason do you have for a single base class and a single child? This strikes me as an inappropriate use of inheritance, or as I like to call it, inheritance for the sake of inheritance. If you really want to inherit something, a much better solution would be to abstract away the behavior of players in general to an abstract interface which concrete player classes derive from and override. That way you can define a class (haha! I made a funny) of players with common behavior that may or may not have the same data and may or may not extend the base functionality. Consider using the behavior for both user controlled players and AI controlled players. They both will likely have the same base behavior though each class will extend that behavior beyond the base for AI or user-specific needs. Yet with the abstract base design, they can both be used as players in a generic fashion.
My best code is written with the delete key.