473,414 Members | 1,674 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,414 software developers and data experts.

inheritance - minor confusion.....


Prefer composition to inheritance (can't recall which text I stole that
line from) is one of the fundamental tenets thats engrained in my mind.
Having said that inheritance requires careful thought. To compound
things, when dealing with inheritance 'virtuality' can only be
experienced through base pointers to derived.
There are ocassions though that I dont need a (particulay) 'virtual'
function in base. In which case I'll have to used derived directly.

So now:

# include<iostream>

class base {
protected:
base() {}
virtual ~base() = 0 {}
public:
virtual void override_func() {}
// more
};

class derived : public base
{
public:
derived() {}
~derived() {}
void do_start() {}; // I REALLY do need a do_start in base..

void override_func() {
// implementation
}
// more
};

int main() {
derived *d = new (std::no_throw)derived(); // use derived directly
d->do_start();
d->override_func();
delete d;
}

For some reason I'm led to believe that if you're unable to access
derived members through a base pointer, something was amiss about your
design (as in the case above). Am I off on this?

As always thanks in advance.

Aug 22 '05 #1
5 1316
ma******@pegasus.cc.ucf.edu wrote:

For some reason I'm led to believe that if you're unable to access
derived members through a base pointer, something was amiss about your
design (as in the case above). Am I off on this?


There is nothing in your code example that requires inheritance. Without
an explanation of the purpose of the class base it's impossible to give
advice on whether its interface is correct.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 22 '05 #2
* Pete Becker:
ma******@pegasus.cc.ucf.edu wrote:

For some reason I'm led to believe that if you're unable to access
derived members through a base pointer, something was amiss about your
design (as in the case above). Am I off on this?


There is nothing in your code example that requires inheritance. Without
an explanation of the purpose of the class base it's impossible to give
advice on whether its interface is correct.


Agreed, but much of the general point of inheritance is often to introduce
extras, so as a general guideline (not related to any particular example) I
think the OP's belief is counter-productive and ill-adviced: stuffing every
possible public derived class member function as a virtual member function
in the top-most base class is, most often, very bad design.

--
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?
Aug 22 '05 #3
| stuffing every possible public derived class member function as a
virtual member function
| in the top-most base class is, most often, very bad design.

Alf, understood and in which case there's no need to use a base pointer
for access and that's perfectly fine. You see, often times I run
across a class that uses inheritances. A derived class needs an
additional member function. Trouble is this _new_ member function is
only necessary in said class. Adding a virtual to the base for access
via a base class pointer seems counterproductive. In my initial post
the do_start member function is one such example of a case where it's
not necessary for a virtual member function in the base class.

Aug 22 '05 #4
<ma******@pegasus.cc.ucf.edu> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
| stuffing every possible public derived class member function as a
virtual member function
| in the top-most base class is, most often, very bad design.

Alf, understood and in which case there's no need to use a base pointer
for access and that's perfectly fine. You see, often times I run
across a class that uses inheritances. A derived class needs an
additional member function. Trouble is this _new_ member function is
only necessary in said class. Adding a virtual to the base for access
via a base class pointer seems counterproductive. In my initial post
the do_start member function is one such example of a case where it's
not necessary for a virtual member function in the base class.


The following would work, with the exception of not being able to delete the
object :/

#include<iostream>
class base
{
protected:
base() {}
virtual ~base() = 0 {}
public:
virtual void override_func() {}
// more
};

class derived : public base
{
public:
derived() {}
~derived() {}
void do_start() {}; // I REALLY do need a do_start in base..
void override_func() {
// implementation
}
// more
};

int main()
{
base *b = new derived; // use through base pointer
if ( dynamic_cast<derived*>(b) != NULL )
dynamic_cast<derived*>(b)->do_start();
b->override_func();
// delete b; // Compile Error, 'base::base' : cannot access protected
member declared in class 'base'

}

Since no one else suggested this approach, this makes me think I don't
understand the issue. Did I walk into the middle of a discussion?

You made the statement:
"For some reason I'm led to believe that if you're unable to access
derived members through a base pointer, something was amiss about your
design (as in the case above). Am I off on this?"

But as I've shown above (with the exception of the protected destructor in
base) you can access derived members though a base pointer as long as you
cast it to the derived (and is an approach I used quite successfully in an
application with much polymorphism).

What am I missing here?

Sep 2 '05 #5
On 22 Aug 2005 10:37:52 -0700, ma******@pegasus.cc.ucf.edu wrote:

Prefer composition to inheritance (can't recall which text I stole that
line from) is one of the fundamental tenets thats engrained in my mind.
Having said that inheritance requires careful thought. To compound
things, when dealing with inheritance 'virtuality' can only be
experienced through base pointers to derived.
There are ocassions though that I dont need a (particulay) 'virtual'
function in base. In which case I'll have to used derived directly.

So now:

# include<iostream>

class base {
protected:
base() {}
virtual ~base() = 0 {}
public:
virtual void override_func() {}
// more
};

class derived : public base
{
public:
derived() {}
~derived() {}
void do_start() {}; // I REALLY do need a do_start in base..

void override_func() {
// implementation
}
// more
};

int main() {
derived *d = new (std::no_throw)derived(); // use derived directly
d->do_start();
d->override_func();
delete d;
}

For some reason I'm led to believe that if you're unable to access
derived members through a base pointer, something was amiss about your
design (as in the case above). Am I off on this?

As always thanks in advance.


As others have already pointed out, it doesn't make sense to discuss
whether or not inheritance is the appropriate mechanism for solving
your problem because we don't know what the problem is. I have found
inheritance useful for defining a common interface which can be used
by objects of other classes which don't need to know the derived
type(s). Here are some examples of how inheritance can be used like
this:

(a) A database query. Some queries read, some write, yet there is an
awful lot of functionality shared between the two. For example, the
database connection object need not always know what kind of query it
is just to fetch the data associated with an error the query might
have produced;

(b) You have an application which needs to read different kinds of
barcodes and print the data as output to an XML file. There is an
abstract base class "Barcode" which knows how to print its data. All
the application needs to know how to call is the barcde's virtual
"PrintMe()" function;

(c) You have a collection of different graphic shapes (circles,
squares, triangles, etc.) and all your collection needs to do is to
print out each shape's area. Only the derived shape classes know how
to do that, so there is an abstract class "Shape" which has a virtual
function "CalcArea()" which is overridden by each derived class.

You get the idea?

--
Bob Hairgrove
No**********@Home.com
Sep 2 '05 #6

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

Similar topics

4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
4
by: Matthew Bell | last post by:
I've got a conceptual problem to do with inheritance. I'd be grateful if someone could help to clear up my confusion. An example. Say I need a class that's basically a list, with all the...
1
by: Dave | last post by:
Hello all, The methodology of policy-based design states that one should inherit from policy classes and, of course, these classes must provide an agreed-upon public interface and semantics. ...
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...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
2
by: Flavian Musyoka Mwasi | last post by:
I'm a novice programmer just beginning to learn the new C# language. I'm a bit confused about the way Inheritance and Interfaces are constructed in C#. The following examples may help clarify...
1
by: Flavian Mwasi | last post by:
I'm a novice programmer just beginning to learn the new C# language. I'm a bit confused about the way Inheritance and Interfaces are constructed in C#. The following examples may help clarify...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: Invalidlastname | last post by:
Hi, I just read the pattern "Design and Implementation Guidelines for Web Clients" from MSDN. Here is my question. In chapter 3,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.