About Inheritance in c++ - c++

If a class is inherited in public mode so public members and protected members of base class will go in public and protected section of derivied class.
My question is: if there is no protected section in derived class then what will happen?

Nothing will "happen". The class simply will not have protected members other than those of its base(s).
If you don't write protected:, that doesn't mean the class is incapable of having [inherited] protected members, or that it has no "protected section". Syntax and semantics are not that tightly coupled.

With public inheritance, it does not matter if derived class has no protected section at all in its definition, or if it is empty. The protected members of base class are inherited anyway and remain protected. Same for public, even if derived class has no public section in its definition.
The public:/protected:/private: in calss definitions are not really "sections", they are just syntax to mark member access for the members following them until the next public:/protected:/private:, and you can have any number of such "sections" in any order.

Related

Access Modifier for class name in C++

I was looking at inheritance concepts in http://www.geeksforgeeks.org/inheritance-in-c/. I was confused with few sentences written by the author. At one place author says
If we derive a sub class from a public base class. Then the public
member of the base class will become public in the derived class .......
That means do we have something like public class in C++? Also below table from the article indicates that there is a concept of public/Protected class.
I looked at few other SO posts (Use of "Public" in a derived class declaration?) and found no reference to Public, Private or protected class itself. The post https://stackoverflow.com/questions/4792614/making-classes-public-to-other-classes-in-c talks of public, but by means of header file.
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.
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.

Scope for Public and Private

There is a piece of code:
class BirdCarreau: public viscosityModel // Private data
{
dictionary BirdCarreauCoeffs_;
...
Why this data which is inside this PUBLIC type is said to be private data?
Default access protection for members in a class is private. So you need to put public: before.
The public you provided is only for the base class. But your members are extending the derived class.
The class itself is public but its members are private to the user. That's the general idea of classes. you can make them public by specifically stating their access modifier to be private
In your example public means that BirdCarreau inherit the data and methods from viscosityModel.So public is used to achieve public inheritance.By default, all members of a class have private access. So, the data in your class is
private.

what is the difference between public members and publicly inherited protected members? [duplicate]

This question already has answers here:
What is the difference between public, private, and protected inheritance in C++?
(16 answers)
Closed 9 years ago.
what is the difference between public members and publicly inherited protected members?
(as it is said that protected members can only be accessed by the base class and immediate next derived class. but if we inherit protected members publicly , we can inherited it further.)
A public method is visible to everyone.
A publicly inherited protected method is still protected, i.e. visible to the class itself and any derived classes.
Public member can be accessed from any class using object of that class and for accessing public members you do not have to inherit that class but to access protected members you have to inherit it. Mind it if a member is declared protected in the base class and you inherited publicly then also this member will be considered as protected member of derived class.
Public members can also be accessed by other classes (not just derived) and everywhere in general.
Well, public inheritance is your "normal" inheritance, so the semantics of public and protected members in this case are exactly what you'd think they are.
That is, your public members are public, and your protected members are visible only to the current and more-derived classes.
It's basically only when you start using private/protected inheritance that things get dicey and confusing and strange.
1:If protected members inherited by public then child class can
access it as well as it will remains protected in child class so that
we can access this member in next child class.
2: If protected members inherited by private then child class can
access it and it will make private in child class so that we can'not
access this member in next child class.
A public member is public. A protected member is protected. It doesn't matter whether they got there by being defined directly or being inherited from a public base.

difference between interface inheritance and implementation inheritance

I found those two terms in the book of Meyers, but what is the difference?
Interface inheritance is public inheritance, while implementation inheritance is private inheritance.
If class B publicly inherits from A, B is an A: it inherits the whole interface of A, and a (reference/pointer to) a B object can be automatically be upcasted to A, and used wherever an object of A is expected. However, if B privately inherits from A, B is-implemented-in-terms-of A: only the implementation of A is inherited, not its interface. Thus (references/pointers to) B objects can not be used in places where A objects are expected.
Update
To reflect on #Michal's comment, here are some links (based largely on googling "c++ implementation inheritance") to demonstrate the common usage of these terms in the context of C++:
C++ Design/Coding tips - Part 7
Interfaces
Uses and Abuses of Inheritance, Part 1
Implementation (or class) inheritance is when you separate a common part of implementation in the base class.
Interface inheritance is when you use virtual methods. It is intended to separate interface from implementation and minimize dependencies between program elements.
The major difference is interface is public inheritance and implementation is private inheritance.
The data members and method of the public and protected section will be inherited from base class to derived class in their respective access specifier in public inheritance.At the same time the object of derived class can access the data members of base class as the normal method.
The data members and methods of public and protected section will be inherited from base class to derived class in private access specifier
Here's the difference between the two types of inheritance according to "Taligent's Guide to Designing Programs".
Inheritance
There are two forms of inheritance in C++: type inheritance and implementation inheritance. In both forms of inheritance, a derived class can share or override behavior inherited from a base class. However, use type inheritance only when it is necessary for a derived class to inherit type information as well. The primary reason to inherit type information is to allow for polymorphism.
Express type inheritance by deriving a class from a public base class; express implementation inheritance by deriving a class from a private or protected base class.
More at:
https://root.cern/TaligentDocs/TaligentOnline/DocumentRoot/1.0/Docs/books/WM/WM_23.html

Visibility modes in Classes

We know that private members are not inherited whenever we inherit a base class to get a derived class, but is it possible for derived class member functions to access
the private members of the base class?
Private members are inherited, but they are not accessible. They are "private" for a reason. However, if your derived class needs to access them, it should be declared a friend to the base class.
Private members of the base class are inherited by the derived class, but are not (directly) accessible to it. They may still be accessed by protected and public members of the base class. As Hosam Aly says above one could access base class members by making derived class functions friends of the base class, or by turning the private members of the base class into protected members of the base class, whichever is preferable.