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

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 1364
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.********@comAcast.net> wrote in message
news:_7****************@newsread1.dllstx09.us.to.v erio.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.********@comAcast.net> wrote in message
news:_7****************@newsread1.dllstx09.us.to.v erio.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******************************@localhost.ta lkaboutprogramming.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
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


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
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...
4
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...
0
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...
3
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...
5
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. ...
0
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...
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; };
7
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...
0
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.