Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default inheritance access specifier

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.

like image 497
Simplicity Avatar asked Jan 25 '11 17:01

Simplicity


People also ask

What is the default access specifier in C++?

In a C++ structure type, the default access modifier for class member and member functions is "public".

Which access specifier is by default in class data member?

In a class, the default access modifier for class member and member functions is private.

Which is default inheritance of class Swift?

Inheritance allows us to create a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from which the child class is derived is known as superclass (parent or base class). Here, we are inheriting the Dog subclass from the Animal superclass.

Can inherited class access private members?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them. In the following example, class D is derived publicly from class B . Class B is declared a public base class by this declaration.


2 Answers

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 {}; 
like image 154
anatolyg Avatar answered Sep 20 '22 07:09

anatolyg


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).

like image 28
Yakov Galka Avatar answered Sep 20 '22 07:09

Yakov Galka