To inherit or not to

B

bobsled

For class A to reuse functionality of a class B, A could either contains B
or inherits from B. When should pick which?

In a book the author says that "Don't inherit from a concrete class." Does
this mean that for a concrete class B, using composition is the only choice?

Thank you for your comments!
 
J

Jakob Bieling

bobsled said:
For class A to reuse functionality of a class B, A could either contains B
or inherits from B. When should pick which?


Depends on the classes. If class B is called 'tire' and class A is
called 'car', I would go for containment. But if class B is called 'car' and
class A is called 'ford', I would use inheritance. Just a classic example,
but you get the point..

hth
 
J

JKop

bobsled posted:
For class A to reuse functionality of a class B, A could either
contains B or inherits from B. When should pick which?

In a book the author says that "Don't inherit from a concrete class."
Does this mean that for a concrete class B, using composition is the
only choice?

Thank you for your comments!

I've come across something like this before.

I wanted to have 2 completely independant classes, let's say called
KA and KB. Like so:

class KA
{


};

class KB
{


};

I wanted to give both of the classes access to a certain function,
let's say a function that turns generates a string out of a number.
But the string is going to be in the Thai number system. But the
thing is, I don't want anyone else do be able to touch this certain
function, therefore, I want it to be private. The following would be
ideal:

class KA
{
public:

unsigned long numbr;

private:

wchar_t szBuffer[30];

void GenerateThaiString(void);
};

class KB
{
public:

unsigned long numbr;

private:

wchar_t szBuffer[30];

void GenerateThaiString(void);
};


KA::GenerateThaiString AND KB::GenerateThaiString
{

//blah blah
return &szBuffer[12];
}

//See how I want to use the same definition for both! Anyone got any
suggestions. Again, I remind you that I want this function to be
strictly private and only accessible by the member functions of the
classes KA and KB.


Thanks in advance.

-JKop
 
J

Jakob Bieling

I wanted to give both of the classes access to a certain function,
suggestions. Again, I remind you that I want this function to be
strictly private and only accessible by the member functions of the
classes KA and KB.

You could create a third class, which contains the function as a static
private member. Now make that class friend to both KA and KB and you got
what you wanted.

hth
 
J

JKop

Jakob Bieling posted:

You could create a third class, which contains the function as a
static
private member. Now make that class friend to both KA and KB and you
got what you wanted.

hth


And I'll even stick a pure virtual function in there to make it
abstract. Thanks!


Anyone else think of any other methods? Comments appreciated.


Unrelatedly...

Is there anything you can put in a class declaration to specify that
it CANNOT be inherited from? So:

class Jt : public Kr


will generate a compile error saying "Can't inherit from Kr!" If this
is the first time anyone's ever suggested this, then I want to coin
the keyword, "sterile":

sterile class Kr

lol
 
J

Jakob Bieling

And I'll even stick a pure virtual function in there to make it
abstract. Thanks!

Well, why would you want to do that?
Is there anything you can put in a class declaration to specify that
it CANNOT be inherited from? So:

Not that I know of. But usually a non-virtual d'tor implies 'This class
is not supposed to be derived from'.

hth
 
D

Daniel T.

bobsled said:
For class A to reuse functionality of a class B, A could either contains B
or inherits from B. When should pick which?

In a book the author says that "Don't inherit from a concrete class." Does
this mean that for a concrete class B, using composition is the only choice?

I think the author means, don't publicly inherit from a concrete class,
but even then I'm not so sure I agree. However I will say this: don't
publicly inherit from a class that doesn't have a virtual d_tor; don't
inherit (public, protected or private) from a class unless you are going
to override at least one of the base class' virtual functions; don't
publicly inherit from a class unless objects of the new class must be
useable in the same context as the base class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top