Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Question about Classes

Author
Message
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 24th Feb 2010 05:43
I am having a little trouble grasping how to derive from a base class. I don't really understand what is taken from the base class and how it will help me write less code. Here is what I am trying to do with classes:

I have a base class box which will have an update and an initialize. Also my constructor for this class is going to be the same for every class that derives from this base class. basically in all my derived classes the code in Update and Initialize change but they only change a few lines of code.

Here is my base class:



Now say I put some code into the update and initialize functions, when I derive from this base class how can I make it so I take that code from update and initialize, keep it, and then add on to it with the new class.

For example:

I know this code will be ran in every initialize. But every class that derives from the base class will have more than just this code or will at least have a few lines different than the base class. Is this possible or am I just thinking of it the wrong way?

Also my constructor is going to do the same thing each time which is set its position, scale, and ID numbers. Do I have to create a new constructor for every class that I make? If so I feel its kind of pointless to derive my classes from a base class because I'm still overriding all of the code inside each function.

Sorry for the loaded question I will appreciate any suggestions and if this isn't very clear I'm sorry I didn't really know how to explain my problem.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 24th Feb 2010 07:43 Edited at: 24th Feb 2010 07:46
Quote: "Now say I put some code into the update and initialize functions, when I derive from this base class how can I make it so I take that code from update and initialize, keep it, and then add on to it with the new class."


You can do this by calling the base class method from your derived class, the syntax for that would be: void Derived::someMethod(){ Base::someMethod(); }


Quote: "Also my constructor is going to do the same thing each time which is set its position, scale, and ID numbers. Do I have to create a new constructor for every class that I make? If so I feel its kind of pointless to derive my classes from a base class because I'm still overriding all of the code inside each function."


Of course you do, just remember that your base class still has a constructor, if it has no default constructor then you must explicitly call one of its constructors from your derived class' constructor initialization list, like so: Derived:erived( int arg1 ) : Base( arg1 ){}

Inheriting classes can be used to only cut down on lines(such as inheriting a bunch of common attributes), but (at least in my code) I mainly use it for the polymorphism, that is, I create a base class that many other classes inherit from, these derived classes all share something in common. For example, I may have a base class called 'Entity', all entities in my program will probably have a name, a position, health and what not, my base class could have default behaviour in the form of a 'damage( float amount )' which would decrement the health of the entity when I shoot it. I could have a derived class called Player that inherits Entity and overrides this 'damage' method to reduce the amount of damage dealt based on my armour level(which is an attribute only found in Player), this may look something like: Player::damage( float amount ){ Entity::damage( Max( 0.0f, amount - armour ); }

If I later wanted to make it so a particle effect spawned when an entity gets hit, I only have to modify 'Entity::damage', because it's called either way. But most importantly, it means I can just maintain a list of 'Entity*' instances and handle them all the same and their specific implementations would get handled due to polymorphism. Without using a base class I'd have to maintain several lists, one for each type of entity, this would lead to lots of redundant code, and adding particle effects upon hits would be quite tedious.

Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 24th Feb 2010 23:37 Edited at: 25th Feb 2010 00:16
Thanks for the reply,
I have tried what you said in my project and have ran into a few problems. The first is I am still a little confused on the constructor:

Now basically like I said in the post before I want the constructor for each derived class to do the exact same thing as the base class: so here is the base class:



So I made a derived class and tried what you said in your post so it looked something like this:


So obviously since I am getting errors I am not grasping exactly what I need to do to be able to use the code from the base class.

Could you explain a little more on the constructor.

As for my functions I ran into one problem (which your explanation helped a lot on) In my function Initialize in my base class I need to take a pointer to a class like so:



Now if I derive from this class do I need my Initialize function in the derived class to take the same parameters?
Like this:



Now this gives me errors but I don't really know how this would work. I was thinking I wouldn't need to have a pointer to a class since my base class does it but either way I get errors.

thanks again for the reply really appreciate the detailed explanation. I'm still a little new to programming so I hope you can understand.

EDIT:

I got my code to work after some playing around. I decided to not call b2World in my base class and rather call it in my derived classes.

Thanks again for the explanation.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 25th Feb 2010 00:32


should be:



Though you may find this neater:



Quote: "Now if I derive from this class do I need my Initialize function in the derived class to take the same parameters?"


No, but if you want to use polymorphism then you'll have to make the one in the base class virtual and override it in your derived class.

Anyway, I recommend you read the tutorials HERE, they probably explain things better than I can.

Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 25th Feb 2010 03:53 Edited at: 26th Feb 2010 00:23
So I was looking over the site that you posted and I didn't really find a solution to my problem.

Here are my two different classes:



In my next class I want to take the code from Initialize in StaticBox and basically just add a few more lines. Problem comes with the the pointer to b2World. I kind of asked you in my last question but essentially this is how I imagine it working:



Problem is I am getting errors with this.

Again thanks for stepping me through this I have been playing with it for most of the day.

EDIT:
I figured out why it wasn't working.
I just needed to pass the pointer world through the initialize of the derived class. like so:


EDIT:
Got it to work.
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 25th Feb 2010 23:19
I ran into another problem as I was converting all my code so that it derives off a base class. Basically I made a rigid body that derives from a class box. Now I am trying to make a character class derive from rigid body but I am getting an error I haven't really seen before.



here are my two classes:



that is my character class and I only added in the functions that I actually want to derive from Rigid Box.
Here is the rigid box class:



So I'm not to sure what I am doing wrong. Anyone see something that I am doing wrong?
Aldur
16
Years of Service
User Offline
Joined: 8th Oct 2007
Location: Melbourne, Australia
Posted: 26th Feb 2010 01:31
I think because you are declaring a public function, you need to actually write the function for it.

Isocadia
15
Years of Service
User Offline
Joined: 24th Jul 2009
Location:
Posted: 26th Feb 2010 09:18
I had the same problem and Aldur is right, the moment you declare a function it wil give error if it doesn't exist, so or write an empty function for it or comment it, in your case it would be better to write an empty function.

Isocadia

Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 26th Feb 2010 11:28
Yeah I edited the wrong one. But thanks for the response that was exactly the problem. I feel a little stupid but thanks for the help.

Login to post a reply

Server time is: 2024-10-05 16:18:19
Your offset time is: 2024-10-05 16:18:19