A private base

Hello all,

In page 508 of this book (http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA508#v=onepage&q&f=true), Biarne says: "This would make Shape a private base of Circle, making Shape's public functions inaccessible for a Circle."

And also in page of 511 of that book says: "If a base of class D is private, its public and protected member names can be used only by the members of D.".

These two make a paradox to me! Any idea please?
Last edited on
Its the same... Circle cant access shape, which is a mistake, its the same without the : Shape, and it should've done : public Shape. And for 511, The base of D is private, which is B, which doesn't use the public, which is pointless and the same without it. Think about it, public, protected, and private members can be accessed inside the class, but for bases : //... their members are not in the class unless if its public.

edit: MiiNiPaa is right, But I was talking about virtual functions, you cant use them in private bases.
Last edited on
1) If you have an object of type Circle you could not call Shape member functions for it:
1
2
3
4
5
6
7
8
class Shape
{
public:
    int getSize();
//...

Circle x;
x.getSize(); // illegal 


2) Circle object itself can use public or protected function itself (inside member functions)
1
2
3
4
5
6
7
8
9
10
class Circle: private Shape
{
public:
    void doSmth()
    {
        int a = getSize(); //Allowed
//...

Circle x;
x.doSmth(); //Allowed, we do not call getSize directly 
Couldn't understand still!

class D: private base { /*.....*/};

Considering above code, I mean, finally, when a base class is private, are its (that is base's) member names (whether they are private, protected or public) accessible from class D which is derived from that base? If yes, so why in page 508 he says "inaccessible"? If no. why he says "can be used" in 511?
are its (that is base's) member names (whether they are private, protected or public) accessible from class D which is derived from that base?
Yes.

If yes, so why in page 508 he says "inaccessible"?
They are inaccessible by code which uses class D. Not the class D itself.

private inheritance is: take all accessible base class members and make them private so only I can use them.

A little table showing equivalent access modifiers for derived class for different base access modifiers and inheritance:
inheritance
v base -> | public  | protected | private
---------------------------------------------
public    | public  | protected | inaccessible
--------------------------------------------
protected |protected| protected | inaccessible
----------------------------------------
private   | private | private   | inaccessible
Thank you very much. That table clarified all the thing. Thanks again mate :)
Apparently this website doesn't consist reputation mechanism, what can I do in return?
Topic archived. No new replies allowed.