Z
zippy747
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
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