Advertisement

What is inheritance?

Started by July 05, 2000 02:43 AM
4 comments, last by Zipster 24 years, 5 months ago
This post is dedicated to JonatanHedborg , since he asked this question off-topic in another post, and i felt we could help him out (yes he coulda done it himself,, but hell i felt like it ). First heres my say: Inheritance has to do with classes , which you can think of as "advanced structures" if you also know C. Inheritance is when functions and variables of one class are "copied" into a derived class automatically, thereby allowing child classes to call functions that were in the base class. This is very useful for things where many objects have related funtions (drawing, moving, shooting, etc.), but each object has slightly different specifics (i.e weapons, armor, etc.). The cool part is that if the function isn''t declared in a child class, it''ll aruromatiicaly call the parents version of the function, and if THAT is undefined, it keeps going up the heiarchy. Very convenient. Thats what OOP is all about. THats what i have to say. A few facts might be wrong since its been a while since ive done any programming and im rusty. ======================================= A man with no head is still a man. A head with no man is plain freaky.
Thanx for the help Zip (can i call u that? )

Anyone got a short example?




========================
Game project(s):
www.fiend.cjb.net

Edited by - JonatanHedborg on July 5, 2000 9:21:47 AM
=======================Game project(s):www.fiend.cjb.net
Advertisement
Here is a short C++ example.

class MyClass
{
int item;
void foo();
};

class MySubClass:public MyClass
{

};

I am fairly sure that is it in C++ (i''m a little rusty). OO principles are easier to see in Java.

class MySubClass extends MyClass { }

One thing with inheritence is that any private functions or variables cannot be accessed by the subclass unless you specify public access.
                        // Class.hclass CBase{public:  CBase(int iCount, double dPercent)  { m_int = iCount; m_double = dPercent; }  virtual ~CBase() {};  virtual int GetCount() { return m_int; }  double GetPercent() { return m_double; }protected:  void SetCount(int iCount) { m_int = iCount; }  void SetPercent(double dPercent) { m_double = dPercent; }  int m_int;private:  int m_double;};CDerived : public CBase{public:  CDerived(int iCount);  virtual ~CDerived() {};  virtual int GetCount();protected:  int m_iVar;};// Class.cppCDerived::CDerived(int iCount) : CBase(iCount, 23){  m_iVar = 2;}int CDerived::GetCount(){  int iCount = CBase::GetCount();  SetCount(iCount*2);  return iCount;}// main.cpp#include "stdio.h"#include "Class.h"int main(int argc, char* argv[]){  CBase myBase(2, 10);  CDerived myObject(12);  CBase *pDerived = NULL;  printf("Base: %d, %lf\n", myBase.GetCount(), myBase.GetPercent());  printf("Derived: %d, %lf\n", myObject.GetCount(), myObject.GetPercent());  // Here's an example of the use of polymorphism, as an aside.  pDerived = (CBase*)&myObject  printf("Ahaha: %d, %lf\n", pDerived->GetCount(), pDerived->GetPercent());}            


This demonstrates inheritance - CDerivedClass inherits from (or, is derived from, or is a subclass of. Hmm. Too many terms) CBaseClass.

As can be seen from main(), both instances have GetCount and GetPercent functions - although CDerivedClass doesn't declare/implement GetPercent itself, it uses the CBaseClass function.

Both function have a virtual GetCount function. What happens here is that the compiler (yes, the compiler. It shouldn't affect performance at all, under most circumstances) looks at the type of the object, and calls the CDerivedClass version, which in turn calls the base class version explicitly (it doesn't have to, of course), along with SetCount (in CBaseClass, since CDerivedClass doesn't have one of it's own).

It's important to note that CDerivedClass' GetCount calls CBaseClass::GetCount rather than GetCount, because doing the latter would result in an infinite-until-the-stack-overflows loop.

So, that's inheritance. CDerivedClass gets all of CBaseClass' member functions and variables (I'm supposed to use 'methods' and 'properties', but sod that ), even though it might not be able to access all of them.

Oh, and the CDerivedClass constructor? The (...) : CBaseClass(...) bit? That means that immediately before CDerivedClass::CDerivedClass runs, the CBaseClass constructor runs. This is well worth it, because otherwise the base class constructor isn't run.

Also, always declare your destructors virtual, so that when you delete CDerivedClass, all destructors in the inheritance tree are called one after the other, and cleanup can be done at each inheritance level, properly.

As an aside, let's look at the last little bit of jiggery-pokery in main() - casting a pointer to the CDerivedClass instance, and storing it as a pointer to a CBaseClass.

What happens when you call GetCount? Well, thanks to the joys of polymorphism, the CDerivedClass version will be called.

If, on the other hand, we hadn't used the virtual keyword when declaring GetCount, then the first two calls to GetCount would work as expected, but the pointer-casted call (the third and last, in main) would call CBaseClass::GetCount.

I hope that makes sense.

(Edited a bunch of times, because the source tag doesn't work properly - it seems you get one per post, and that's yer lot. Grr. If anyone's interested, put two blank lines in to get one in the output)

TheTwistedOne
http://www.angrycake.com

Edited by - TheTwistedOne on July 5, 2000 10:34:19 AM
TheTwistedOnehttp://www.angrycake.com
OOP can be done in most languages, not just C++. It is possible to do inheritance in C so do not limit OOP to C++.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
saying polymorphism is a good way to impress your QBASIC friends.

The world is your message board.

This topic is closed to new replies.

Advertisement