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{
publ开发者_C百科ic: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
In all forms of inheritance:
y
can look up to its base-class (x
) and seepublic
andprotected
.- Derived classes of
y
can see itspublic
andprotected
members. - Users of
y
can see itspublic
members. - Nobody can see anyone else's
private
s, unless they'refriend
s.
In public inheritance:
- users of
y
can look up tox
and seepublic
.
- users of
- In protected inheritance:
- both
public
andprotected
parts ofx
becomeprotected
iny
- Derived classes of
y
can see them. - Users of
y
cannot see them.
- both
- In private inheritance:
- both
public
andprotected
parts ofx
becomeprivate
iny
: - 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).
- both
This C++ FAQ has good information on private and protected inheritance.
- 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.
- 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.
精彩评论