Derrived Classes

Started by
6 comments, last by gamechampionx 21 years ago
I''m fairly new to C++, but I know how to use basic classes and structures. However, I would like to know what a derrived class is and how it works.
Check out Drunken Brawl at http://www.angelfire.com/games6/drunken_brawl!
Advertisement
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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
_________________________________________"You're just jelous because the voices only talk to me"
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!!! =)
Thanks people. I catch your drift.
Check out Drunken Brawl at http://www.angelfire.com/games6/drunken_brawl!
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.
Check out Drunken Brawl at http://www.angelfire.com/games6/drunken_brawl!
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.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement