inhertance:meaning in detail

hi,
suppose i jave a base class and i go on derived class it then what is the limit said by experts?
I mean that maximun 5 level deip whatevre ,what is the limit and why?
i heard my friend say that too inheritance and code slowed many times.
pls remind that i dont want virtual dynamic now will understand that later and try  by myself.

thta is
class base
{
public :
//some code
}

class derv1:public base
{
public:
//moe code in add to class base
//some override code too
}

derv(?)

I am just starting as c++ ,java programmer ,trainign for 2/3 mths going on.can somebody psl explain?Technicaly also please,i have 155 points today and am going to native place ,come back on 3rd Jan 200 ,i see anser to question then?

thanks all!
hey_heyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hey_heyAuthor Commented:
no i have now 105 point!
0
mbormannCommented:
I think u mean that u dont want to know abt 'virtual' functions but u have said

             class derv1:public base
             {
             public:
             //moe code in add to class base
             //some override code too
             }

//some override code too
that can be dynamic too
mostly as far as I know inheritance and virtual functions r closely related.I am also learning c++ now.
see URL below

as far as the limit is concerned I personally dont think that anybody goes over 10 levels deep.I have seen some Java code which has 7

yes inheritance which is meant for code reuse does suffer a itty bitty but that's the price u have to pay for extensibility,and i dont think that it is very much slowed down.

URL's
-----
explanation on virtual functions
http://www.elj.com/eiffel/ij/inheritance/virtual-fns/
http://www.elj.com/eiffel/ij/inheritance/polymorphism/
good part C++ tutorial
http://www.deakin.edu.au/~agoodman/ctutorial/819.html
http://www.deakin.edu.au/~agoodman/ctutorial/

read marshall Cline's FAQ
http://www.faqs.org/faqs/C++-faq/part6/

take a look at a good site I found now
http://www.eiffel.com/doc/manuals/technology/oosc/inheritance-design/
0
KangaRooCommented:
The minimal number od direct and indirect baseclasses guaranteed by the C++ standard (Dec 1996 draft) is 16384.

A derived class inherits all members (data and functions) from its parent:

class A
{
   public
     void f();

};

class B : public A
{
   public:
      void g();
};

void somefunction()
{
   A a;
   B b;

   a.f(); // ok
   b.f(); // ok, inherited from A
   a.g(); // not a member of A
   b.g(); // ok, member of B
}
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

KangaRooCommented:
BTW, Code from this kind of inheritance doesn't slow down the code, not even a tiny bit.
0
yonatCommented:
Although it deep inheritance does slow down execution is other languages (eg Smalltalk), so your friend maybe heard this from someone familiar with some other language.
0
jasonclarkeCommented:
> Code from this kind of inheritance
> doesn't slow down the code, not even
> a tiny bit.

construction (and destruction) time for non-empty base classes (i.e. those with some data) is likely to be influenced by inheritance depth isn't it, because of the need to call each base class constructor?

and if you had a -deep- hierachy, I would take this as a warning sign.
It might mean that you were carrying around lots of extraneous data in your objects, and passing round and creating objects would take longer than necessary.
0
KangaRooCommented:
Its not (entirely) due to inheritance, it's function call overhead that slows down.

In another language you'd probably use seperate functions to instantiate / destroy complex 'objects', resulting in a similar overhead. Or initialize dozens of members one by one?
Ultimately this may give C++ an edge, a long hierarchy of small classes with inline constructors and destructors could even be Faster
0
jasonclarkeCommented:
true, what I my main point was, is that if you have a deep hierachy of classes, you should make sure you know why.  I don't think many applications need it.  I think normal best practise is to have a forest of small trees rather than a few big ones.
0
mbormannCommented:
In Java atleast for a custom component like say some JCMultiColumnList
the hierarchy is
available from http://www.klg.com

Class jclass.bwt.JCMultiColumnList
java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----jclass.bwt.JCContainer
                                   |
                                   +----jclass.bwt.JCScrolledWindow
                                           |
                                           +----jclass.bwt.JCMultiColumnWindow
                                                   |
                                                   +----jclass.bwt.JCMultiColumnList


--------------------------------------------------------------------------------
0
nils pipenbrinckCommented:
there can be a slowdown... calling member functions - virtual or not - won't make a problem.. even if they are overriden or inherited.

a problem might arise, when you have non-trivial constructors/destructors.
In most cases the constructors inherit all the code from it's base class. That means, that if you have a inherited class at level 8 you don't call one constructur but 8.

This is normally not a problem to worry about. However, if you don't look at your code and use a class of this kind as a stack-variable in a frequently called function it can slow down your code a lot.

Anyways.. I have never seen such code in real use..

Nils





0
sureshkumarCommented:
I am giving simple meaning to inheritance


inheritance means reusing.That means you are trying to reusing the existing program.

eg. suppose if you want to develop c++ language.that is nothing advanced of c.that means your language must support all the features of c.Instead of developing all that again you just inherit those properties and extend the features.what you want.This is simple example for inheritance..

further queries
suresh kumar
0
hey_heyAuthor Commented:
kangaroo,
>>>>>>>>>>
Its not (entirely) due to inheritance, it's function call overhead that slows down.

what fucntino call pls?
>>>>>>>>
In another language you'd probably use seperate functions to instantiate / destroy complex 'objects', resulting in a similar overhead. Or initialize dozens of members one by one?

which language pls?

yonat,
my frend was talking java

also interfaces in java are equal to pure virtual functions in cpp.

then if my class implements many interface in java code is also  slow,but why?explainaton is that

"the runtime systm need to chek that each object is of the types specicifed by the interface"???

pls tell,what is the way in C==?
0
hey_heyAuthor Commented:
sory C== be C++
0
KangaRooCommented:
>> what fucntino call pls?

We've been talking about constructors and destructors; a deep inheritance tree will result in many calls to constructors. Each class calls it's parents class constructor before executing it's own.

>> which language pls?
C for instance. When efficiency matters are concerned, C++ is usually compared to C. But the discussion can be held for C++ - Java also. Java will have the same 'problem' as C++.

>> the runtime systm need to chek that each object
>>is of the types specicifed by the interface
Yes, but that can be made very efficient. One implementation for C++ could use a table with pointers to member functions. Each class has its own table and each object has a pointer to such a table. During runtime all that is needed is to get the pointer to the table of member functions and lookup the required function pointer from this table. All this is ithout 'searches', just three memory lookups, have a dozen processor clock cycles. The overhead for a function call is several hundreds of clock cycles....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbormannCommented:
hey_hey ,
KangaRoo was talking of vtable and vptr,I had some questions in my PAQ list and they advised me to bu Scott Meyer's 2 books,which i did and in one of the chapters he talks in detail abt that,I dont think there r  many books where such a concise and to the point explanation is given.

After reading from those books I am sure that Java must have some sort of thing like this ,but will have to read up...

basically I think that for thiose methods(functions in C/C++) where u tag it 'final' are non-virtual(every method by default in Java is virtual),so they dont have entries in the table?

will let u no later when i dig it out and ofcourse someone else doesnt do it first.
0
mbormannCommented:
confirms what many people here r talking abt
http://www.glenmccl.com/tip_006.htm
see
http://www.glenmccl.com/langtips.htm
0
hey_heyAuthor Commented:
people didnot tell about inetrface /pure virtual function so I post another questoin?

thanks to everyody else for jelping me!
0
hey_heyAuthor Commented:
sorry i read now and i think jasonclarke also say same thing,so post a ques at
0
vetriCommented:
Hi hey_hey.

 referring to your original question on levels of inheritance,

1. It is upto you to decide on the levels of inheritance depending on your requirement or the actual system you are modelling subject to the maximum limit on levels of hierarchy imposed by the compiler you are using.

2. It is always advised not to inherit beyond five levels. But it is not a hard and fast rule that needs to be followed in letter and spirit.  James Rumbaugh et al in their book "Object oriented analysis and Design" suggest when you specialize a hierarchy of classes, do not go beyond five levels irrespective of a particulur language. But thatz only a suggestion. If your system insist on having 6 levels or 7 levels of inheritance, go for it. The restriction on inheritance levels is to make your code understandable. It is suggested that, as you specialize more, your code tend to lose on the understandability front.

3. Summing up the other replies, deep hierarchy also affects on performance front at runtime. Simple reason : It involves lot of function calls which takes lot of time.

For eg.
Say a class at 10th level of a inheritance need to make a minimum of 10 function calls at the time of its creation. Although such a scenario could be very rare, but not impossible.
Think of the time required, if each level of classes has an array of say 10 user defined objects which involves calling their own constructor along with above said constructor calling.

Same order of time required while destroying the 7th level object too in calling the corresponding destructors.

To have forest of small trees rather few big trees as suggested by jasonclarke is be the best way any developer  could think of to avoid long inheritance hierarchy.

vetri
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.