Login    New User    Forgot Password    Help      

Inheritance Access Specifiers

GO TO INDEX

Inheritance Access Specifiers

In an effort to reduce the number of nonconstructive comments posted by visitors who do not read the entire article or previous comments, I have decided to change the title of the article and the initial summary to something more accurate.

In C++ there are three inheritance access specifiers:

  • public
  • protected
  • private

Any of these three inheritance access specifiers can be modified with the virtual keyword. In my experience interviewing candidates for C++ positions, I've learned that the average programmer does not know how these are used, or even what they mean. So I thought I would go over them here.

The three access modifiers public, protected and private are analogous to the access modifiers used for class members.

public
When a base class is specified as public ie: class c : public base {}; the base class can be seen by anyone who has access to the derived class. That is, any members inherited from base can be seen by code accessing c.
protected
When a base class is specified as protected ie: class c : protected base {}; the base class can only be seen by subclasses of C.
private
When a base class is specified as private ie: class c : private base {}; the base class can only be seen by the class C itself.

Accessibility modes and Inheritance

We can use the following chart for seeing the accessibility of the members in the Base class (first class) and derived class (second class).

 
Inheritance-Mode

Example

struct X {
public:
void A() {}
};

struct Y {
public:
void B() {}
};

struct Z {
public:
void C() {}
};

struct Q : public X, protected Y, private Z {
public:
void Test()
{
A(); // OK
B(); // OK
C(); // OK
}
};

struct R : public Q {
public:
void Test2()
{
A(); // OK
B(); // OK
C(); // NOT OK

Q t;
Y *y = &t // OK
Z *z = &t // NOT OK
}
};

int main(int argc, char **argv) {
Q t1;
t1.A(); // OK
t1.B(); // NOT OK
t1.C(); // NOT OK

R t2;
t2.A(); // OK
t2.B(); // NOT OK
t2.C(); // NOT OK

X *x = &t1; // OK
Y *y = &t1; // NOT OK
Z *z = &t1; // NOT OK

x = &t2; // OK
y = &t2; // NOT OK
z = &t2; // NOT OK

}

GO TO INDEX