views:

87

answers:

3

hi, i want to know some things about class derivation in c++ so i have super class x and an inherited class y and i did this

class x{
    public:a;
    private:b;
    protected:c;
}    
class y:public x{
    public:d;
}

in this case how y can access a,b,and c and by how i mean(public,protected,private)

the second case:

class x{
    public:a;
    private:b;
    protected:c;
}    
class y:private x{
    public:d;
}

the same question?

the third case:

class x{
    public:a;
    private:b;
    protected:c;
}    
class y:protected x{
    public:d;
}

again the same question?

sorry i think i wrote too much bye

+4  A: 
  1. In all forms of inheritance:

    • y can look up to its base-class (x) and see public and protected.
    • Derived classes of y can see its public and protected members.
    • Users of y can see its public members.
    • Nobody can see anyone else's privates, unless they're friends.
  2. In public inheritance:

    • users of y can look up to x and see public.
  3. In protected inheritance:
    • both public and protected parts of x become protected in y
    • Derived classes of y can see them.
    • Users of y cannot see them.
  4. In private inheritance:
    • both public and protected parts of x become private in y:
    • Derived lasses of y cannot see them.
    • Users of y cannot see them.
    • Private inheritance is essentially the same as composition (a private data member).

This C++ FAQ has good information on private and protected inheritance.

Stephen
In the land of C++, friends can see your privates. Never say never. ;-)
Chris
@Chris: hah, true. Edited.
Stephen
Nice answer. Thorough, complete, succint.
Dan
A: 
  • A derived class can only access public and protected members of the base class. It cannot access the private members of the base class.
  • private/public/protected inheritance affects how the members inherited from the base class (X) are accessible to the "users" of the derived class (Y); now, the users could be a class derived from this derived class.
  • private inheritance is akin to declaring the public and protected members of the base class as private in the derived class.
  • **protected interitance** is akin to declaring the public and protected members of the base class as protected in the derived class.
  • **public inheritance** makes the public members of the base class public in the derived class; but the protected members of the base class remain protected in the derived class.
A: 
  • Derived classes cannot access private members of the base class unless the derived class is a friend of the base class.
  • Inheritance permissions:
    • Public - all non-private members of the base class retain their permissions (base: public, derived: public; base: protected, derived: protected)
    • Protected - all public members of the base class become protected in the derived class; protected members in the base class are still protected (base: public, derived: protected; base: protected, derived: protected)
    • Private - all non-private members of the base class become private in the derived class (base: public, derived: private; base: protected, derived: private)

I can't stress enough the fact that private members of a base class are inaccessible by a derived class unless that derived class is declared to be a friend in the base class.

Dustin