First of all, I not quite sure I understand your last sentence.
Does C++ allow for a third class Z to
subclass Y in such a way that Y's
implementation of x1 and y are
privately available to it, while the
outside world only sees it inheriting
X publicly, i.e. having only a single
public method x2?
If Z
inherits publicly from X
, then both x1
and x2
will be available : although accessibility of x2
may be changed in Z
, nothing prevents the outside world from manipulating a Z
through a X
pointer and call x2
.
That being said, you might as well have Z
inherit privately from Y
and publicly from X
though, as pointed out by Johannes, you should look into virtual inheritance as Z
will thus inherit twice from X
.
Depending on your needs, you might also want to look into the decorator pattern (maybe it's completely unrelated, but for some reason, I feel through reading your question that it's what you want to achieve) :
class X
{
public:
virtual void x1();
virtual void x2();
};
class Y : public X
{
public:
virtual void y();
virtual void x1();
};
class Z : public X
{
public:
explicit Z(X *x) : x_(x) {}
virtual void x1() { x_->x1(); }
virtual void x2() { x_->x2(); }
private:
X *x_;
};
int main()
{
Y y;
Z z(&y);
}
In this quick and dirty code sample, Z
is a X
(public inheritance), yet is reuses Y
implementation.