A friend class is any class that is granted access to the private members of another class. The following minimal example demonstrates a simplistic friend class.
#include<iostream>
class A {
friend class B;
public:
A(int data=0):m_data(data){}
private:
int m_data;
};
class B{
public:
B(const A& a):m_data(a.m_data){}
int getdata()const{return(m_data);}
private:
int m_data;
};
int main()
{
A a(50);
B b(a);
std::cout<<b.getdata()<<std::endl; // prints 50
return(0);
}
In the above example, A's encapsulation is such that A:m_data can be initialised through A's constructor but is otherwise inaccessible outside of the class. The assumption is that the A::m_data is intended for internal use only. However, B's encapsulation is such that it can be constructed from an instance of A, but since there is no public accessor to A::m_data, B requires friend access to A, and only A can grant that access, hence class B is declared a friend of A.
Note that friends can be declared any where in a class declaration since private, protected and public access do not apply. Note also that there is no need for a forward declaration of B before declaring A because B is dependant upon A, not A upon B.
It should also be noted that although B could potentially undermine the encapsulation of A (a common complaint where friends are concerned), the assumption is that since you are in control of both A and B, then it is your responsibility to ensure that B obeys the same encapsulation rules you set out within A. That is, if A provided a private A::getdata() accessor and a corresponding A::setdata() mutator for its own internal use, then B should make use of that same accessor and mutator rather than access A::m_data directly. This ensures that should you alter the implementation of A::m_data, you will not affect the inner workings of B, thus enforcing A's encapsulation rather than undermining it, and therefore minimising maintenance.
Friend classes are often used wherever two classes work closely together. For instance, a parent and child class often work in tandem, thus it can often make sense to for the child class to declare the parent class a friend. However, there are often better ways than declaring an entire class to be a friend of another class. Friend functions (which can include specific class methods) greatly reduce the chances of undermining encapsulation by limiting the exposure of your private class members. Thus instead of declaring the entire parent class to be a friend of the child class, you could simply declare specific functions to be friends of both classes, thus those functions provide the "glue" that binds the two classes together.
Undermining encapsulation is always a danger with friends, thus you should never grant friendship unnecessarily. After all, the onus is upon you to enforce your own encapsulation rules -- the compiler cannot help you any more than it can help you enforce those rules within the class itself. However, if the public interface is sufficient for your needs, then you do not need friends. You should only use friends when the public interface is insufficient and where altering the interface to suit would actually do far more to undermine the encapsulation than a friend would.
Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.
The keyword is friend. The external function must be declared a friend of the class (from within the class itself) in order to become a member of the class and thus gain access to the private (and protected) members of the class.
Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.
s.
Sure.
Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.
The keyword "friend" allows a function or variable to have access to a protected member inside a class.
The keyword is friend. The external function must be declared a friend of the class (from within the class itself) in order to become a member of the class and thus gain access to the private (and protected) members of the class.
Yes, there can be friend functions in C++.
No.
Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.
Class wrappers (embedded objects), inheritance (derived objects) and friend classes.
An object in C++ is an instance of a C++ class.
s.
B. Class.
In C++, methods are simply class member functions.
Sure.