Default Class Inheritance Access

Default class inheritance access

You might have read something incomplete or misleading. To quote Bjarne Stroustrup from "The C++ programming Language", fourth Ed., p. 602:

In a class, members are by default private; in a struct, members
are by default public (§16.2.4).

This also holds for members inherited without access level specifier.

A widespread convention is, to use struct only for organization of pure data members. You correctly used a class to model and implement object behaviour.

Default inheritance access specifier

Just a small addition to all the existing answers: the default type of the inheritance depends on the inheriting (derived) type (B in the example), not on the one that is being inherited (base) (A in the example).

For example:

class A {};
struct B: /* public */ A {};
struct A {};
class B: /* private */ A {};

C++'s default inheritance access specifier?

BTW, it is not called access modifier. It is called access specifier

$11.2/2 - "In the absence of an
access-specifier for a base class,
public is assumed when the derived
class is defined with the class-key
struct and private is assumed when the
class is defined with the class-key
class."

In your context, 'Bar' is a private base class of 'Foo'

Inheritance default access modifier in C++

The ONLY difference between struct and class is that in a struct, everything is public until declared otherwise, and in a class, everything is private until declared otherwise. That includes inheritance. So class B : A will default to private inheritance, and struct B : A will default to public inheritance.

Access Modifier for class name in C++

The Public, Protected and Private keywords are the visibility labels in C++. There is no public, protected and private class type in c++ (like Java). These three keywords are also used in a completely different context to specify the visibility inheritance model.

The table given below lists all of the possible combinations of the component declaration and inheritance model presenting the resulting access to the components when the subclass is completely defined.

Sample Image

It reads in the following way (take a look at the first row):

if a component is declared as public and its class is inherited as
public the resulting access is public.

Have a look at an example below:

class Super {
private: int x;
protected: int y;
public: int z;
};
class Sub : protected Super {};

The resulting access for variables y, z in class Sub is protected and for variable x is none.

NOTE:

The lack of a modifier yields private.

Default access modifier of constructor in Inheritance

I did a little web search and came to know that default constructor's
access specifier is same as the access level of class

There is a difference between default constructor and the one which you've declared. The default constructor is the one which you don't declare inside the class.

The current one in your code is not a default constructor. And, it has default(package-private --- no explicit modifier) acccess as you've omitted any access modifier from it.

Which concept am I missing here ?

So, your class from other package is not able to find it, because of the limitation of default access.



Related Topics



Leave a reply



Submit