Which access specifiers will not affect a friend function? - c++

Which access specifiers will not affect the friend function?
private and protected members of a class cannot be accessed from outside, private and protected member can be accessed anywhere or both

None of the access specifies affect a friend.
When you declare a function/method or class as a friend you are making it part of the public interface to your class (thus implying a tight cupping).
A friend is actually able to see all the members of your class (you could think of it as a member function but without the implied this parameter). As such it is actually tightly coupled to your class. Any change in the internal representation of your class is going to be reflect in changes to the implementation of all public interface members.

Related

C# CodeAnalysis ProtectedAndInternal vs ProtectedOrInternal

I'm unclear about the distinction between the Accessibility values ProtectedAndInternal and ProtectedOrInternal.
I found a reference stating ProtectedOrInternal maps to the source code construct protected internal.
But -- assuming that's correct -- what does it mean for something to be both protected and internal?
ProtectedOrInternal corresponds to protected internal:
The protected internal keyword combination is a member access modifier. A protected internal member is accessible from the current assembly or from types that are derived from the containing class. For a comparison of protected internal with the other access modifiers, see Accessibility Levels.
ProtectedAndInternal corresponds to private protected:
The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly. For a comparison of private protected with the other access modifiers, see Accessibility Levels.
It's a combination of both types of accesibility levels. Whether some object can be accessed from within the same assembly (internal) or can use the derived types (protected). They are not the same but can be used interchangeably

Prevent friend function from accessing private member of a class

How can we prevent friend function from accessing private member of a class. Can we do this at all?
This question was asked in an interview and he was confident that it can be done, he gave hint about functor / function object. So far I can't think of anything. I am excited about the answer, if any.
How can we prevent friend function from accessing private member of a class. Can we do this at all?
No, you cannot.
As soon something is declared as friend of your class the doors to access any private members are opened.
The idea of Encapsulation is to bundle data and methods (that work on the data) together and restrict access of private data members outside the class. In C++, a friend function or friend class can also access private data members.

Need clarification on interface in OOP

With regards to a class, what does an interface mean? I think it refers to all the public functions of the class. Am I correct or does it mean something else?
I keep hearing it a lot but never quite noticed the explicit definition.
This one's the real question. What does it mean for a derived class to retain the interface of the base class it's derived from? I think it means that the public functions in the base class must be public in the derived class as well (which will be the case in public and protected inheritance). Am I mistaken?
Yes, the interface of a class is the collection of its visible member functions to the outside world, i.e. its public member functions. Some also include member variables in the interface, but it is not usually common to have public member variables (unless declared static). Most often, interfaces are implemented via abstract base classes. This is in contrast to Java, which has a different keyword for specifying interfaces.
Retaining the interface means having the public member functions in the base class visible across the class hierarchy. Also, you can override virtual functions so you get polymorphic behaviour, keeping the common interface. Note that only public inheritance preserves the interface, protected and private do not. Another way of failing to retain an interface is via name hiding in C++. Example: re-declaring Base::f(int) as Derived::f(float,float). In this case, the Base::f(int) is not longer visible in Derived, unless via a using Base::f; statement.

How to set methods private to module?

I have two D classes in one module. I would like class A to have a property, which can only be accessed from class A and class B. How would I do this?
class A {
int a = 5; // Make this accessible to, and only to, B.
}
class B {
this(in A pA) {
int b = pA.a;
}
}
private is private to a module, not a class. So, marking a symbol as private makes it so that only stuff in that module can access it.
package makes it so that only stuff in the same package can access the symbol.
protected makes it so that only stuff in that class and in classes derived from that class can access the symbol (unlike the others, protected makes no sense outside of classes).
public makes it so that anything can access the symbol.
private and package functions are never virtual, whereas protected and public functions are always virtual unless the compiler is able to determine that they don't need to be (which at this point, can pretty much only happen when the function is final and doesn't override a function in a base class).
So, as long as your two classes are in the same module, and their members are private, they can access each others members - as can anything else within the module - but nothing outside the module can access them. There is no way to restrict access within a module except for when the symbol is local to a function, so if you want to make it so that one class cannot access the members of another, then you're going to need to put them in separate modules.
just add private
the access modifiers are module based with protected as the only exception
http://dlang.org/attribute.html#ProtectionAttribute (emph mine)
Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Private members cannot be overridden. Private module members are equivalent to static declarations in C programs.

C++ access specifiers

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?
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.
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 methods
protected is somewhat less trivial. It means that only the object, or its children, may access the attributes (bad idea*) or methods. (Plus, in C++, friends)
private means that only the objects of that class (and not their children) may access the attributes or methods (Plus, in C++, friends)
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.
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.
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).
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.