Derrived Classes
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.
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
"If there is a God, he is a malign thug."
-- Mark Twain
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
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
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!!! =)
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.
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement