473,320 Members | 1,821 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.

Using Inheritance -- clarification needed ?

Hi,

Have this following hierarchy which am implementing for a networking
program. The base class 'ASocket' is the base class from which
'AListener' and 'ATalker' inherit . None of the functions in the
derived classes would override functions from the base class. The
derived classes would extend the base class. A couple of doubts :

1. Should the functions in the base class be declared virtual ?

2. Is this a good way to go about things or should one make ASocket an
abstract base class ? ( thinking in terms of interface based
programming )

3. The Inheritance topic in C++ FAQ slightly confuses me .. It talks a
lot about protected inheritance .. should that be a concern for such a
situation.

Thanks in advance.

Apr 8 '06 #1
4 1437
* vivekian:

Have this following hierarchy which am implementing for a networking
program. The base class 'ASocket' is the base class from which
'AListener' and 'ATalker' inherit . None of the functions in the
derived classes would override functions from the base class. The
derived classes would extend the base class. A couple of doubts :

1. Should the functions in the base class be declared virtual ?
When you're not overriding anything there's no apparent reason.

But are you sure you're not overriding anything?

E.g., if you're deleting an object via an ASocket* pointer, then you
should be overriding the destructor, which should then be virtual.

2. Is this a good way to go about things or should one make ASocket an
abstract base class ? ( thinking in terms of interface based
programming )
AFAICS you don't provide enough information to answer that.

3. The Inheritance topic in C++ FAQ slightly confuses me .. It talks a
lot about protected inheritance .. should that be a concern for such a
situation.


AFAICS you don't provide enough information to answer that.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 8 '06 #2

Alf P. Steinbach wrote:
2. Is this a good way to go about things or should one make ASocket an
abstract base class ? ( thinking in terms of interface based
programming )


AFAICS you don't provide enough information to answer that.

3. The Inheritance topic in C++ FAQ slightly confuses me .. It talks a
lot about protected inheritance .. should that be a concern for such a
situation.


AFAICS you don't provide enough information to answer that.


okay, my bad.
ASocket has the following methods -- openSocket () , closeSocket () ,
bind() which are common to both the derived classes and would remain
the same for both of them. AListener would have specific methods to
listen to incoming UDP packets and ATalker would have the ability to
broadcast such packets.

Though this is probably a one time coding effort, but was thinking if
lets say some time later the class needs to add TCP functionality , so
then requirements would change, the openSocket and closeSocket function
would require different arguments. In what way should my thought
process and design be to forsee future extensibility ? ( Thinking in
terms of direction inversion principle ) and thus asking about abstract
class ?

Also the Atalker and Alistener class would be used by other classes as
in a composition relationship ? This is where the c++ FAQ confuses me
e.g. "I've been told to never use protected data, and instead to always
use private data with protected access functions. Is that a good
rule?". Am not sure if all the FAQ are relevant here ?

thanks again .

Apr 8 '06 #3

vivekian wrote:
In what way should my thought
process and design be to forsee future extensibility ?


It shouldn't. You should design and implement what you need in the
most abstract way you can think of with the least amount of
dependencies between modules. That is all. Let the future establish
itself. Reasons:

1) you spend all your time in analysis paralisys and never get anything
done if you try to think of every possibly future need.

2) you can't think of every possibly future need so why try?

3) Just following some guidelines and avoiding pitfalls makes it easy
to make changes later.

All you need to do is prepare yourself for the possibility of having to
make changes to the code base by keeping the smells to a minimum.
Learn how to refactor...recognize bad code and learn how to turn it
into good code and how to take good code and change it to match new
needs.

With that in mind...inheritance is the strongest binding you can create
between two modules short of friendship. Avoid when not needed.

Yes, I got all that from books but it also applies very well in the
real world.

Apr 8 '06 #4

Noah Roberts wrote:
vivekian wrote:
In what way should my thought
process and design be to forsee future extensibility ?
It shouldn't. You should design and implement what you need in the
most abstract way you can think of with the least amount of
dependencies between modules. That is all. Let the future establish
itself.


Yes , makes sense. So does that translate into using abstract classes
to minimize dependencies. ? e.g. in my case .. there would be an
abstract base class called ASocket from which both UDPSocket and
TCPSocket could derive ?
With that in mind...inheritance is the strongest binding you can create
between two modules short of friendship. Avoid when not needed.


This is the part which confuses me most. When do i use inheritance ?
while coding when there are two classes which share common
functionality it makes so much sense to put this common functionality
in a base class and then specialize with derived classes. But then
everyone says 'use inheritance with caution' and makes me think twice..
so lets say in the above case , when ATalker and AListener have
functions in common , is it not right for them to derive from ASocket ?

Apr 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
6
by: Martin | last post by:
Hi, Suppose I have 3 interfaces : IStats IEngine ICreateCar each derives from IUnknown
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
3
by: Hazz | last post by:
I am just beginning to design a Treeview display (winforms) for wine regions. Problem. Some wine growing regions belong to two counties. Eg. Carneros is in both Napa and Sonoma Counties. Although...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
3
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: 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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.