问题
I just want to make sure I got the idea of public and private right.
Regarding the private access specifier, does it mean:
- Only accessed inside the class
- Cannot be accessed from the object of the class unless there are public class methods that can be used to access them (Can other objects use those public functions?)
- No other object can access them
And for public:
- Accessed from the object of the class
- Accessed from any other object
Is that right?
回答1:
I think there is an issue of vocabulary to begin with.
In C++ (and most languages) a class
is a type
. You can think about it as a blueprint to actually build something.
- it describes the attributes that are held
- it describes the methods to operate on those attributes
- it describes the restrictions that apply: this is the "accessibility"
An object
is produced by actually instantiating a class, that is, building what the blueprint described. It is more or a less a bundle of attributes. You can have several objects of the same class as you can have several houses from the same blueprint: note that their physical location is different for obvious reasons :)
Now, on to the accessibility. There are 3 typical levels of accessibility: public
, protected
and private
.
public
, as expected, means that everyone is given access to either attributes or methodsprotected
is somewhat less trivial. It means that only the object, or its children, may access the attributes (bad idea*) or methods. (Plus, in C++,friend
s)private
means that only the objects of that class (and not their children) may access the attributes or methods (Plus, in C++,friend
s)
Note: whatever the level of accessibility, an object has unrestricted access to all the attributes and methods of any object of the same class.
(*) Even though it pops up now and there, it is generally a bad idea to use protected
attributes. The point of encapsulation is to hide the details, not only for the sake of it, but because by precisely controlling who can access the data, we can ensure that the class maintains its invariants (simple example, an array where you would store the size separately, you need to ensure that the "size" really represents the number of items in the array, at all times). Note: this restriction does not apply when you can seal a hierarchy, like in C# for example.
回答2:
private : Only member functions and friends of class can access it.
public : Can be accessed anywhere where the object has scope.
Answering the questions -
private:
- Yes
- Yes. (Can other objects use those public functions? With out having class relations, one object of class cannot communicate to the other's members. )
- Friends has access to private members of a class. So, answer depends upon your class has friends or not.
public:
- Yes
- Depends whether the object has hierarchical relations to the member's class you are trying to access.
回答3:
Private members can only be accessed by member functions and static functions of the same class and by friends of the class. It does not matter on which object that function is called. So the case
class Foo
{
private:
void bar() {}
public:
void baz(Foo& var)
{
var.bar();
}
}
is perfectly legal.
回答4:
That seems correct. Data members and functions marked public can be accessed from anywhere by anyone. Data members and functions marked private can only be accessed by the class and its friends. However, a member function of a class can access data with any access specifier, so a public function can read and write private data members (this is used universally in OOP).
回答5:
In c++ data and fn are encapsulated as 1 unit. We begin a program by writing preprocessor directives Then, class declaration Followed by function(fn) declaration where we also specify the access modifier ( public, private or protected)
& finally the main () program.
If we declare a fn Private:the data within an object of a class is only accessed by fn defined within it- (the object which has the data and the private fn)
Public:the data can be accessed by any fn
Protected:similar to private however data can also be accessed by sub-classes that inherit the properties of another class.
Example if class A inherits from class B, thenA is a subclass of B.
来源:https://stackoverflow.com/questions/4919460/c-access-specifiers