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.
In a C++ structure type, the default access modifier for class member and member functions is "public".
In a class, the default access modifier for class member and member functions is private.
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.
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.
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 {};
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With