473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

To inherit or not to

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
7 1725
"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
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
"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
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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Fabian Neumann | last post by:
Hi! I got a problem with font-family inheritance. Let's say I have CSS definitions like: p { font:normal 10pt Verdana; } strong { font:normal 14pt inherit;
1
by: Jeff Schmidt | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, ~ Ok, I've got some tables, and a style sheet that contains stuff to format the table. The problem I'm having is, it appears that using...
0
by: Phil Evans | last post by:
URL: http://philevans.com/tabletest.html A boring table, but one which illustrates my problem. I'm writing a large PHP app which outputs data tables. Users of the app can configure their...
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
6
by: Mohammad-Reza | last post by:
I wrote a component using class library wizard. In my component i want to in order to RightToLeft property do some works. I can find out if user set this property to Yes or No, But if He/She set it...
2
by: ad | last post by:
We can inherit a class. But if the class is the code behind, when we inherit it , can we inherit it's aspx ahead together ?
7
by: Frank | last post by:
Hi, a question probably asked before, but I can't find the answers. Base class X, classes A, B and C inherit class X. In class A I do not want to inherit property (or function or method) P1....
3
by: J | last post by:
I tried to inherit 'Shot' class from 'Image' class, only to fail. It gives me the CS0122 error, which says that it can't access 'System.Drawing.Image.Image()'. What am I missing? using...
2
by: GTalbot | last post by:
Hello fellow comp.infosystems.www.authoring.stylesheets colleagues, Imagine this situation: #grand-parent-abs-pos { height: 400px; position: absolute; width: 600px; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.