0

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

jack22
  • 21
  • 1
  • 1
  • 4
  • If you don't have one, I recommend getting one of the beginner books recommended in [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – James McNellis May 29 '10 at 03:23

3 Answers3

5
  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
  • 47,994
  • 7
  • 61
  • 70
0
  • 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.
0
  • 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
  • 1,956
  • 14
  • 14