473,785 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostrea m>

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 1343
ma******@pegasu s.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******@pegasu s.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 counterproducti ve. 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******@pegas us.cc.ucf.edu> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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 counterproducti ve. 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<iostre am>
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<de rived*>(b) != NULL )
dynamic_cast<de rived*>(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******@pegasu s.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<iostrea m>

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**********@Ho me.com
Sep 2 '05 #6

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

Similar topics

4
1467
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
1408
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 normal list methods, but I want a custom __init__ so that the list that is created is rather than (yes, it's a bogus example, but it does to make the point). Without bothering with inheritance, I could do:
1
3930
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. I would like to understand better why inheritance, rather than containment, is espoused. The only things I can see that inheritance provides beyond containment is the possibility of polymorphism and access to protected members. Since neither of...
14
12919
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 all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
22
23384
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 examples?
2
2168
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 my confusion: interface IControl { void Paint();
1
1340
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 my confusion: interface IControl
45
6368
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
5
2050
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, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/diforwc-ch03.asp , the article mentions using page inheritance to maintain common page layout. We currently use page inheritance approach to segment the function areas. In each function area, there is a base page to provide the common...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.