473,563 Members | 2,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1739
"bobsled" <sl*****@sands. com> wrote in message
news:%4******** ************@bg tnsc05-news.ops.worldn et.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 GenerateThaiStr ing(void);
};

class KB
{
public:

unsigned long numbr;

private:

wchar_t szBuffer[30];

void GenerateThaiStr ing(void);
};
KA::GenerateTha iString AND KB::GenerateTha iString
{

//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.at t.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
19453
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
3233
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 the 'iherit' keyword for td, tr, is not supported? Attached is the stylesheet in question. Let me preface this by saying, I have {...
0
2404
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 preferences for table border width, colour, style etc . . . I don't have access to the main stylesheet from the PHP, so the border for the table is going...
4
27429
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 one more class.
6
2898
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 to Inherit I must examine its parent RightToLeft property(if i`m wrong please tell me) but in this component that inherits from...
2
1453
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
1748
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. Possible? How? Thanks in advance Frank
3
8920
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 System;
2
3873
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
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.