By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,139 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,139 IT Pros & Developers. It's quick & easy.

To inherit or not to

P: n/a
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!
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"bobsled" <sl*****@sands.com> wrote in message
news:%4********************@bgtnsc05-news.ops.worldnet.att.net...
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
"JKop" <NU**@NULL.NULL> wrote in message
news:dn******************@news.indigo.ie...
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #4

P: n/a
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
--
jb

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

Jul 22 '05 #5

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

There is no keyword, but C++ implementor BS has this to say about preventing
derivation...

http://www.research.att.com/~bs/bs_faq2.html#no-derivation>
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #6

P: n/a
"JKop" <NU**@NULL.NULL> wrote in message
news:jq******************@news.indigo.ie...
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.

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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #7

P: n/a
"bobsled" <sl*****@sands.com> wrote:
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.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.