Archived

This topic is now archived and is closed to further replies.

Derrived Classes

This topic is 5367 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

You are a lot more likely to get a decent explanation of this in a book than by asking on here. Read through your favorite C++ textbook on inheritance and polymorphism, then come back if you have any more specific questions.



"If there is a God, he is a malign thug."
-- Mark Twain

Share this post


Link to post
Share on other sites
a derived class is a class that inherits variables and funtions from the parent class.

I''ll assume you know about access levels in a class, so to make a derived class just write (brackets mean replave it with what it says):

class [derived class name]: [access level to be passed] [name of parent class]
{/* class code */}


If anyone sees an error here, feel free to point it out...

I should have a tutorial about that online soon.

_________________________________________
"Why, why why does everyone ask ''''why'''' when ''''how'''' is so much more fun"
-Spawn 1997

Share this post


Link to post
Share on other sites
quote:
Original post by Infuscare
class [derived class name]: [access level to be passed] [name of parent class]
{/* class code */}


If anyone sees an error here, feel free to point it out...




  
class [derived class name]: [access level to be passed] [name of parent class]
{
/* class code */
};



you forgot your semicolon!!! =)

Share this post


Link to post
Share on other sites
quote:
Original post by MARS_999
you forgot your semicolon!!! =)



Actually it is a comma seperated list of instances of the class, terminated by a semicolon, but yeah, the semicolon is very important.

Share this post


Link to post
Share on other sites
Cool.

You can check my game code at www.angelfire.com/wizard/qotf.

I''m wondering if I can use a derrived class for Enemy because it''s almost the same as Character, but has a few changed.

Share this post


Link to post
Share on other sites
You can liken inheritance and derived classes to inheritance in the real world.

Say my grandfather dies, and leaves me money and property. When I die, I can then leave that money and property to my grandchildren - *plus* money and property that I have acquired myself.

In the same way, a class can ''Inherit'' functions and data from a ''parent'' class, and then add additional functions and data to it. Then a third class can ''inherit'' from that class, till you get the 47''th inherited class which contains all the functions and data in the known universe.

When class A inherits from class B, class A is said to be ''derived'' from B.

For your situation, you''re probably looking at not just *adding* to inherited functionality, but actually *replacing* it. For that, you need to make the functions ''virtual'' in the parent (Character) class - when you derive Enemy from Character and add functions with the same name as the virtual ones in the parent class, it replaces those functions. The parent class calls the functions normally, but gets ''diverted'' to the child class.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Share this post


Link to post
Share on other sites