Default inheritance access specifier

C++Inheritance

C++ Problem Overview


If I have for example two classes A and B, such that class B inherits A as follows:

class B: public A

In this case, I'm doing public inheritance.

If I write the previous code as follows:

class B: A

What type of inheritance will I be doing here (i.e; public)? In other words, what is the default access specifier?

Just a side question here. Do I call the previous line of codes statements? Especially that I remember I read in the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book that statements are that that end with ;. What do you think about that?

Thanks.

C++ Solutions


Solution 1 - C++

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 {};

Solution 2 - C++

It's private for class and public for struct.

Side answer: No, these are definitions of the class according to the standard. Class definition end with a semicolon. On the other hand not all statements end with a semicolon (e.g. an if statement does not).

Solution 3 - C++

When you inherit a class from another class (inherit class Base from class Derived in this case), then the default access specifier is private.

#include <stdio.h>

class Base {
public:
int x;
};

class Derived : Base { }; // is equilalent to class Derived : private Base       {}

int main()
{
 Derived d;
 d.x = 20; // compiler error becuase inheritance is private
 getchar();
 return 0;
}

When you inherit a class from a structure (inherit class Base from struct Derived in this case), then the default access specifier is public.

#include < stdio.h >
  class Base {
    public:
      int x;
  };

struct Derived: Base {}; // is equilalent to struct Derived : public Base {}

int main() {
  Derived d;
  d.x = 20; // works fine becuase inheritance is public
  getchar();
  return 0;
}

Solution 4 - C++

If you use class to define your class, the default access specifier will be private. (I think it's wrong, too.) If you use struct, however, it will be public.

And class definitions are declarations, I think. A statement is what translates into actual code (unless optimized away, anyway).
However, a mildly exotic feature of C and C++ is that expressions are statements. That's why 3+4; is a syntactically legal statement in C++ (although many compilers will warn about it having no effect). While it is obviously nonsense in this case, in general expressions are evaluated for their side effects. (An obvious example is discarding a function's return value. You call the function not to obtain a result, but for its side effects.)

Solution 5 - C++

The "type" of inheritance depends on how the class is defined. There are default access specifiers applied to inheritance. From the C++ standard:

[class.access.base]/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. [ Example: > > class B { /* ... / }; > class D1 : private B { / ... / }; > class D2 : public B { / ... / }; > class D3 : B { / ... / }; // B private by default > struct D4 : public B { / ... / }; > struct D5 : private B { / ... / }; > struct D6 : B { / ... / }; // B public by default > class D7 : protected B { / ... / }; > struct D8 : protected B { / ... */ }; > > Here B is a public base of D2, D4, and D6, a private base of D1, D3, > and D5, and a protected base of D7 and D8.  — end example ]

Solution 6 - C++

If you do not choose an inheritance, C++ defaults to private inheritance in the same way class members default to private access for classes.

Solution 7 - C++

The default type of the inheritance is private in C++.

class B:A
{};

is equivalent to

class B: private A
{};

Solution 8 - C++

the default access specifier is an important differentiator between classes and structs. It is public by default for structs and private by default for classes.

Solution 9 - C++

AS other casting problem you have

class A { virtual void test() = 0; };
class B : virtual public A { virtual void testb() {} };
class C : virtual public A { virtual void testc() {} };
class D : public B, public C {
 virtual void test() override {}
}

void main() {
   D d;
   void* v = &d;
   A* a = &d;
   
   ((D*)A)->test(); //OK
   ((D*)v)->test(); //undefined behavior (that call testb() in vtable logic at 1st inheritance position)
   dynamic_cast<D*>(v)->test(); //compile error cast from void* not permitted

   //resolution
   void* x = a;
   ((D*)x)->test(); //OK but as you can see, you must to store a* in x*

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimplicityView Question on Stackoverflow
Solution 1 - C++anatolygView Answer on Stackoverflow
Solution 2 - C++Yakov GalkaView Answer on Stackoverflow
Solution 3 - C++Pradip KhomaneView Answer on Stackoverflow
Solution 4 - C++sbiView Answer on Stackoverflow
Solution 5 - C++StoryTeller - Unslander MonicaView Answer on Stackoverflow
Solution 6 - C++JamesView Answer on Stackoverflow
Solution 7 - C++SatishView Answer on Stackoverflow
Solution 8 - C++SullaView Answer on Stackoverflow
Solution 9 - C++user6933496View Answer on Stackoverflow