473,786 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance Trouble

I would like to have a base class that does some work and can be
instantiated.

class a
{
public:
virtual int foo() { return 0; };
virtual int bar() { return 0; };
};

I would like to have a derived class that does some slightly different
work and can be instantiated.

class b : public a
{
public:
virtual int foo() { return 1; };
};

I would like to have another derived class that does some slightly
different work in a different area and can be instantiated.

class c : public a
{
public:
virtual int bar() { return 2; };
};

I would like to have another derived class that takes some
functionality from b and some from c:

class d : public b, public c
{

};

I would like the output of the following code

int main()
{
a theA;
printf("%i\n", theA.foo());
printf("%i\n", theA.bar());

b theB;
printf("%i\n", theB.foo());
printf("%i\n", theB.bar());

c theC;
printf("%i\n", theC.foo());
printf("%i\n", theC.bar());

d theD;
printf("%i\n", theD.foo());
printf("%i\n", theD.bar());

return 0;
}

to be:

0
0
1
0
0
2
1
2

Obviously, it isn't since theD doesn't know if it should use
foo()/bar() from the a->b->d inheritance tree or the a->c->d
inheritance tree.
What is the proper way to meet these requirements?

Thanks,
-zip

Jul 22 '05 #1
9 1379
zippy747 wrote:
I would like to have a base class that does some work and can be
instantiated.

[...]
What is the proper way to meet these requirements?


Read about virtual inheritance.

V
Jul 22 '05 #2

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:_7******** ********@newsre ad1.dllstx09.us .to.verio.net.. .
zippy747 wrote:
I would like to have a base class that does some work and can be
instantiated.

[...]
What is the proper way to meet these requirements?


Read about virtual inheritance.


I don't think that will solve his problem, because virtual inheritance relates
to state, and he is trying to define behavior, correct?
Jul 22 '05 #3
jeffc wrote:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:_7******** ********@newsre ad1.dllstx09.us .to.verio.net.. .
zippy747 wrote:
I would like to have a base class that does some work and can be
instantiated .

[...]
What is the proper way to meet these requirements?


Read about virtual inheritance.

I don't think that will solve his problem, because virtual inheritance relates
to state, and he is trying to define behavior, correct?


Probably. I didn't spend enough time to understand the OP's problem.
Perhaps it can be solved with casting the 'theD' object to 'b' or 'c'
or 'a', explicitly.

V
Jul 22 '05 #4
zippy747 wrote:
I would like to have a base class that does some work and can be
instantiated.
[SNIP]
I would like to have another derived class that takes some
functionality from b and some from c:

class d : public b, public c
{

};
[SNIP] Obviously, it isn't since theD doesn't know if it should use
foo()/bar() from the a->b->d inheritance tree or the a->c->d
inheritance tree.
What is the proper way to meet these requirements?


Maybe you can try using the "using" directive to specify which foo and
bar you want class d to use.

Jul 22 '05 #5
On Wed, 15 Dec 2004 07:49:39 -0800, zippy747 wrote:
I would like to have a base class that does some work and can be
instantiated.
<snip>
I would like to have another derived class that takes some
functionality from b and some from c:

class d : public b, public c
{ public:
virtual int foo() { return b::foo(); }
virtual int bar() { return c::bar(); } };


Works?

- Jay

Jul 22 '05 #6
class b : public virtual a

class c : public virtual a

Jul 22 '05 #7

"marieddu78 " <ma************ @vivacity.it> wrote in message
news:74******** *************** *******@localho st.talkaboutpro gramming.com...
class b : public virtual a

class c : public virtual a


This is virtual inheritance. How exactly does this solve the OP's problem?
Virtual inheritance exists to avoid making 2 copies of the base class. This
doesn't have anything to do with the fact that his D object doesn't know which
function to invoke.
Jul 22 '05 #8
Jay Nabonne wrote:
On Wed, 15 Dec 2004 07:49:39 -0800, zippy747 wrote:

I would like to have a base class that does some work and can be
instantiate d.


<snip>
I would like to have another derived class that takes some
functionali ty from b and some from c:

class d : public b, public c
{


public:
virtual int foo() { return b::foo(); }
virtual int bar() { return c::bar(); }
};

Works?

- Jay


What about

class d : public b, public c
{
public:
using b::foo;
using c::bar;
};

?
Jul 22 '05 #9
On Thu, 16 Dec 2004 18:48:49 +0100, Olivier Azeau wrote:
Jay Nabonne wrote:

What about

class d : public b, public c
{
public:
using b::foo;
using c::bar;
};

?


One question is: how should D behave when used in the context of a B (that
is, polymorphically )? That's one thing unclear from the OP. I don't think
the "using" strategy will affect polymorphic behavior. So the correct
approach is determined by the unstated needs... :)

- Jay
Jul 22 '05 #10

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

Similar topics

0
1464
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing works fine in that the derived classes can use the base method get, but if I try and add a method only available to Derived2, eg, get2, I get a compiler error on the call to add_varargs_method("get2",&Derived2::get2, "get2()\n"); in...
4
1842
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector contains pointers to functions of types above.
0
1456
by: apb18 | last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree such that the columns 'ID' and 'field' appear in the top level table, call that table XXX. tables YYY and ZZZ both inherit XXX. Now suppose there exists some query that returns a set of IDs that match some criteria (that query may involve various tests/joins/etc on other arbitrary tables). Executing that query alone produces an optimal plan and the exact result set...
3
41538
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but am not clear on the disadvantages. I have had a look on the Web and in newsgroups but couldn't find much. As time is running out, I thought I would post here and hope that someone would reply. Thanks Rob
5
2886
by: jao | last post by:
My company's product uses Postgres 7.4.3. Postgres is working well for us, and we've worked through many performance issues by tweaking the schema, indexes, and posgresql.conf settings. Inheritance would be useful for our application, but we did not use this feature initially. We're about to revise part of our application, and this would be a good time to introduce inheritance -- it's a good fit for our data model, and it would greatly...
0
1196
by: Terry Hancock | last post by:
I've been discussing PyProtocols with a a friend collaborating with me on a SF game project, about the benefits and design concept of "component architecture", and I'm a little confused by what I'm learning. I learned the concepts of "component architecture" from the Zope project, where if I'm not mistaken, "components" are essentially python "mix-in" classes, and you make a class from components by using multiple inheritance and...
60
4942
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
23
4616
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; };
7
3741
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
0
198
by: Scott David Daniels | last post by:
Here are some tweaks on both bits of code: Paul McGuire wrote: .... m = for b in bases: if hasattr(b, '__mro__'): if MetaHocObject.ho in b.__mro__: m.append(b) if m:
0
9497
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
10363
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
10164
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
10110
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
9962
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.