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.
Related
In vscode how to check if a method is private, public or protected in editor?
If there is a solution to show this in outline that will help too.
EDIT: I can check the header file to see access level, but I want to see it in cc file in popup or in outline.
C++ access is defined by sections.
A class example
class Example{
public:
//anything that is public goes here
protected:
//anything protected goes here
private:
//anything private goes here
};
The default access is private.
You do not need to write all of these sections, nor in this order but it is generally good to write it like this public, protected, private.
It doesn't matter if you are in vscode, visual studio or code::blocks, the editor itself doesn't affect the code.
To access methods and fields from class you have 3 options
Public members can be access everywhere
Protected members act like private for every other scenario except for inherited classes. The classes that inherit base class can access protected fields and methods.
Private members of the class can only be accessed by that class itself.
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.
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.
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.
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.