Jump to content
  • Advertisement
Sign in to follow this  
Renegader_bj

OOP help

This topic is 3662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, a quick question. I am fairly new to programming(I have been doing some on and off for a year or two), but I am brand new writing classes. I and a some friends are working on a game to gain experience, and I am finally getting to write a few classes on my own, but I ran into something that made me think. In all the examples of class I have seen, people use private variables like m_health, m_speed, etc. to describe their classes. So like this: ***SomeClass.h*** Class SomeClass { public: //Constructor SomeClass(); bool LoadSome(); void DrawSome(); ... private: int m_health; int m_speed; ... } Then they would go like this: class1 = new SomeClass(); class2 = new SomeClass(); class1->m_health=10; class2->m_health=15; But I wrote my class like so: ***MyClass.h*** struct VAR { int m_health int m_speed ... }; class MyClass { public: //Constructor MyClass(); bool LoadMy(); void DrawMy(); ... private: //some other variables here ... } In my eyes, by making a struct, I could optimize the whole thing by being able to create multiple objects from one class like so: class = new MyClass(); VAR object1; VAR object2; object1.m_health=10; object2.m_health=10; I tried both approaches and both work, but it seems to me that my way is more optimized, though could ultimately be more confusing. What do you guys think? -Brandon P.S. Obviously, in my game, the class is not called "MyClass" ;). These are just examples.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Renegader_bj
Then they would go like this:

class1 = new SomeClass();
class2 = new SomeClass();

class1->m_health=10;
class2->m_health=15;

You can't access private variables from outside. Also, initialization should be done in the constructor - that's what it is for. Look up on constructors and initialization lists for more information.

Quote:
I tried both approaches and both work, but it seems to me that my way is more optimized, though could ultimately be more confusing. What do you guys think?

I don't see how these two approaches are related, nor how your approach would be more optimal. What is the exact situation you're dealing with? What are you trying to do in the first place? Please give some actual code, so we can give specific feedback.

Share this post


Link to post
Share on other sites
I don't know if this helps with your question much, but the point of declaring m_health as private is so that you cannot do this: m_health = 10. What if you accidentally set it to the wrong thing? This should be initialized to a value in the constructor and changed with a helper function such as myClass.setHealth(int health_value).

I don't even understand how your class and your struct are related in your example...What functionality does your class add? It doesn't do anything in your example.

EDIT: Beaten to the punch...

Share this post


Link to post
Share on other sites
I'm having a hard time understanding what you mean. Structs and Classes are almost identical. The only difference is that Structs are by default public, while classes are private. In C, its different because there are no classes.

So, as I understand it, you created a class that will store two variables in it, speed and health.

class MyObject
{
int m_health;
int m_speed;
public:

// Member functions go here

void setHealth(int h);
void setSpeed(int s);

};




That is the same (memory wise and architecture wise) as creating a struct with the same member variables. You would access it the same way but through member functions (usually called Setters and Getters) that modify the member variables rather then by modifying it directly. Modifying the member variables breaks encapsulation, which is the major component of classes.

Using a struct:

MyStruct *data = new MyStruct();

data->m_Health = 100;
data->m_Speed = 50;




Using classes:

MyClass *data = new MyClass();

data->setHealth(100);
data->setSpeed(50);




You are right to say that these setters and getters are slower because of the overhead required to create and pass values into function headers. But in the defense of the compiler, it is smart enough to inline those simple setters and getters so that, at run time, you don't encounter any slow down. You can manually tell the compiler to inline code by adding the inline keyword before a function call. Also, when you use setters and getters, you can add extra code in there to manipulate the data being passed in. A common practice would be to clean the data so that it does not cause a crash. For example, checking for NULL pointers being passed in, or values that are outside of range, etc.

If there is something I didn't quite answer, just point it out and I or someone else will be happy to help you out.

EDIT: Beaten to the Punch x2.

Share this post


Link to post
Share on other sites
People seem to miss part of the reasons that you want to use OOP. It is to protect your data.

So first you either set your health in the constructor, or a ReadCharacter method. Then you make functions like; LevelUp, TakeDamage, Heal, ect. You really do not want to have direct access to health like SetHealth. This makes sure that you are doing what you should and not just setting the value.

In most projects it will not just be you working on the code. So allowing direct access to your private variables will just lead to having strange bugs that take a long time to hunt down.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by bschneid
I don't even understand how your class and your struct are related in your example...What functionality does your class add? It doesn't do anything in your example.


Sorry you guys. I'm so new to OOP that I'm confusing even myself! The functionality my class adds is the functions encapsulated within. To be exact my enemies are the struct:

struct ENEMY
{
int health;
int speed;
...
}

Then the class has functions that I thought could be used universally with that struct. By declaring a class Enemies(MyClass) that has functions like LoadEnemies and DrawEnemies, I could declare that only once, and use the struct to initialize multiple enemies. Does that make things any clearer?

-Brandon

Share this post


Link to post
Share on other sites
What are you worried about - memory? A function only exists once, no matter how many objects you create of that type. Keeping the functions inside the class makes more sense than what you are proposing.

Can you tell us what you think you are gaining by using your idea?

Share this post


Link to post
Share on other sites
Let's say you have a class containing both function and data members.
Let's also say that the amount of memory required to store the functions is X and the amount of memory that is required to store the data members is Y.

If you create two instances of such a class, i.e.

SomeClass instance1;
SomeClass * instance2 = new SomeClass;


Then the amount of memory consumed would be calculated something like this:

X + (2 * Y)

So, in general, when you create N number of instances of a class then the memory allocated is:

X + NY

Simply put, as rip-off said, the functions are only allocated memory once whereas the data attributes are allocated for each instantiation. The reasoning here is that the functions belong to the class not the instances so only the data is allocated per-instance.

As far as speed efficiency goes: you're not likely to beat an intrinsic language feature with lots of potential for optimisation by the compiler.

All in all, the functions belong with the data they manipulate; all of which belong in the same class [smile].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!