class Class1
{
int a;
int b;
}
class Class2 : public Class1
{
int c;
int d;
}
[/source]
In my opinion Class2 should be the same as Class3 (following):
[source]
class Class3
{
int a;
int b;
int c;
int d;
}
Am i right???
Alright, what''s the point in deriving classes? What can i use it for, can someone tell me? And what''s the base class?
I know there is someone who can tell me, cuz all you experienced programmers know it.
Thanks,
René (-OneNite-)
Deriving classes???
Hi guys...
Alright, i have a question on deriving classes. I know what it means to derive classes (i think) ...Alright here is an example on what i think it means.
//// Base class for an object in a game//class CGameObject { public: CGameObject() ~CGameObject()// void SetPosition(CVector3D CPosition); void Accelerate(CVector3D CAcceleration); void SetMass(float fMass);// virtual void Update(); virtual void Draw();// CVector3D m_CPosition; CVector3D m_CVelocity; float m_fMass; };//class CGAsteroid : CGameObject { public: CGAsteroid(); ~CGAsteroid();// void Update(); // Moves the asteroid void Draw(); // Draws the asteroid}//class CGPlanet : CGameObject { ...}//class CGBullet : CGameObject { ...}
See ?
If one implements a class for an Asteroid into his game and derives it from a CGameObject, he doesn''t need to take care of the position and movement functions anymore.
And if he later adds Planets into his game, he can also derive them from the CGameObject class.
And to manage visibility and render all objects in the game, it''s enough to keep an array/linked list of CGameObjects. So you''ve just got a single, simple function handling all objects in your game, like
CGameObject *CCurObject;
CCurObject = Objects->Top;
while(CCurObject) {
CCurObject->Draw();
CCurObject = CCurObject->Next;
}
If all objects would be derived from CGameObject and contained in that list, you could just let this single loop draw all of your objects. And since the coordinates are contained in the base class, you could also use a single method on all objects to determine visibility.
Then, you could also derive a CGameActor from the CGameObject for objects being composed of polygonal 3d models and handle all collission belongings just by managing the CGameActors, instead of custom handling for CGPlayer, CGAlien, CGPredator, etc.
-Markus-
(Its just an far from complete example, I know it lacks lots of things)
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Just wanted to add a bit about how useful virtual functions are. Since I''m not about to explain it all again, the link below is to a discussion on virtual functions.
http://www.gamedev.net/community/forums/topic.asp?topic_id=23993&forum_id=11&Topic_Title=Virtual+Functions&forum_title=General+and+Game+Programming+Discussion&M=True&S=True
http://www.gamedev.net/community/forums/topic.asp?topic_id=23993&forum_id=11&Topic_Title=Virtual+Functions&forum_title=General+and+Game+Programming+Discussion&M=True&S=True
- Houdini
Ah, overseen the base class question
in
C1 would be the base class of C2
C2 would be the derived class (derived from C1)
That''s all, a base class is just the class from which another was or is to be derived. Base classes often have many virtual member functions. This means the following:
A class derived from another can have functions of the same name. If you''ve got a variable of type C1 in which a class of type C2 is placed in, then call test from the variable
C2 *myc2 = new C2();
C1 *myc1var = myc2;
myc1var->Test();
actually the Test() function in the C2 class is called.
Without the virtual keyword, the Test() function of the used variable type would be called, that means
myc1var->Test();
would call C1::Test()
myc2->Test();
would call C2::Test()
Having a function of the same name as in the base class in a derived class is called function overloading.
You can even overload operators like + - * = += -= /=, so
myvector3d1 = myvector3d2 + myvector3d3
would automatically add the x, y and z valus of myvector3d2 and myvector3d3 and copy all three x, y, z into the myvector3d1 variable.
If used well, this makes code shorter and more readable, thus easier to maintain.
-Markus-
(Whew, you should read Bjarne Stroustrup''s book "The C++ Programming Language", there really is a lot to explain about classes )
in
class C1 { int a,b; virtual void Test();};//class C2: C1 { int c,d void Test()};
C1 would be the base class of C2
C2 would be the derived class (derived from C1)
That''s all, a base class is just the class from which another was or is to be derived. Base classes often have many virtual member functions. This means the following:
A class derived from another can have functions of the same name. If you''ve got a variable of type C1 in which a class of type C2 is placed in, then call test from the variable
C2 *myc2 = new C2();
C1 *myc1var = myc2;
myc1var->Test();
actually the Test() function in the C2 class is called.
Without the virtual keyword, the Test() function of the used variable type would be called, that means
myc1var->Test();
would call C1::Test()
myc2->Test();
would call C2::Test()
Having a function of the same name as in the base class in a derived class is called function overloading.
You can even overload operators like + - * = += -= /=, so
myvector3d1 = myvector3d2 + myvector3d3
would automatically add the x, y and z valus of myvector3d2 and myvector3d3 and copy all three x, y, z into the myvector3d1 variable.
If used well, this makes code shorter and more readable, thus easier to maintain.
-Markus-
(Whew, you should read Bjarne Stroustrup''s book "The C++ Programming Language", there really is a lot to explain about classes )
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
A vague and ambiguous OOP lesson:
Say you are standing in a room full of people. You want to know everybody''s name. What do you do? Most likely you would tell everyone to do the same thing: "Say your name, please". But you will not get the same response from them. When someone says "My name is Al", surely you didn''t ask him to say "My name is Al". In fact, you probably didn''t know what he was going to say. But yet, you somehow knew how to get him to say it.
cmaker
- its not the principle. its the money.
Say you are standing in a room full of people. You want to know everybody''s name. What do you do? Most likely you would tell everyone to do the same thing: "Say your name, please". But you will not get the same response from them. When someone says "My name is Al", surely you didn''t ask him to say "My name is Al". In fact, you probably didn''t know what he was going to say. But yet, you somehow knew how to get him to say it.
cmaker
- its not the principle. its the money.
cmaker- I do not make clones.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement