Reputation: 21
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
Upvotes: 0
Views: 1097
Reputation: 1966
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.
Upvotes: 0
Reputation: 1
X
) are accessible to the "users" of the derived class (Y
); now, the users could be a class derived from this 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. Upvotes: 0
Reputation: 49166
In all forms of inheritance:
y
can look up to its base-class (x
) and see public
and protected
.y
can see its public
and protected
members.y
can see its public
members.private
s, unless they're friend
s.In public inheritance:
y
can look up to x
and see public
.public
and protected
parts of x
become protected
in y
y
can see them.y
cannot see them.public
and protected
parts of x
become private
in y
:y
cannot see them.y
cannot see them.This C++ FAQ has good information on private and protected inheritance.
Upvotes: 5