Advertisement

Data Structures, Classes, and all that Funk(tm)

Started by July 08, 2000 01:59 AM
21 comments, last by Goblin 24 years, 6 months ago
    class pc {public:  void defend(int dmg)  {    ...    if((health -= dmg) =< 0) { gameover(); }    ...  }protected: // same as private but you can inherit these (in public and protected inheritence)  int strength;  int helth;  ...};    


If you have a very good understanding of programming, this should be _very_ obvious.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Just making sure, but doesn''t

    if((health -= dmg) =< 0) { gameover(); }[/source]... does a lot of bad things. Shouldn''t it be?[source]if( (health-damage)<=0 ) gameover();    


Oh well.. sorry to point that out. Thanks for the help!




"Death to all who oppose me."
- The Goblin (madgob@aol.com)
Advertisement
There is no difference between these two:

    // first wayif ( condition )   action;// second wayif ( condition ){   action;}    


However, if you want to have more than one action performed, then you have to use the second method. Also, to answer your questions about structs vs. classes: there is not a single different between the two in a C++ compiler! . They are both stored and accessed exactly the same way. The only difference is that in a struct the default access case is public, while in a class it''s private. That''s just a matter of semantics though, and the code actually generated for the two is completely identical.

Just to re-iterate:
there are no differences between structs and classes in a C++ compiler.

-RWarden (roberte@maui.net)
I wasn''t responding to the brackets in the if statements, I just re-wrote that because I could...

I was noting the fact that the poster used the ''=<'' operator which would generate a compile-error; it should be ''<='' and that in the if statement itself, the poster used ''-='' rather than ''-'', and that they would have inadvertantly done some operations inside the if statement that they shouldn''t of.

Thanks anyway!


"Death to all who oppose me."
- The Goblin (madgob@aol.com)
Actually I think the -= was intentional, as you want to actually subtract the damage from the health permanently rather than just see if health - damage is less than 0, as that would then immediately forget the damage after the test.

-Neophyte
Actually, looking back at it, you''re right, though I usually prefer to just have comparitive syntax in an if statement.

Though I''m stil right about the ''=<'' being wrong, which is a common mistake. I''ve fixed my friend''s making that mistake more times than I can count

Hehe




"Death to all who oppose me."
- The Goblin (madgob@aol.com)
Advertisement
If you are using pure C then you can only use structures and they are different than classes. If you are using C++ than you can use both, but they arte exactly the same.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
I just wanted to see if could confuse Goblin just another bit hehe No really, typos do happend - even to me. Didn''t you ever put a colon instead of a semi-colon after your statement?

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
I know it is but, why? Now I''m in Doubt! Why is it better to use classes rather than structures? Is it just nicer? Or more structured? And with a struct you don''t need to make all that methods...

Thanks, Arthur(rockslave)
import money.*;#include "cas.h"uses bucks;
First the confusing answer:
Structs and Classes are functionally the same. Structs default to ''public:'' content, classes default to ''private:'' content, but at least with classes you should explicitly state what''s public and private etc., so they''re pretty much interchangeable.

Second, the useful answer:
Use a struct when all you have is grouped data. Use a class when you want grouped data, and some functions that operate on that grouped data. With a little forethought, classes can make code look a lot cleaner.

This topic is closed to new replies.

Advertisement