oops
oops
Friend Function:
In object-oriented programming, a friend function, that is a "friend" of a
given class, is a function that is given the same access as methods to private and
protected data.
A friend function is a non member function or ordinary function of a class, which is
declared as a friend using the keyword “friend” inside the class.
Friend functions aren't considered class members; they're normal external
functions that are given special access privileges.
Static Member:
Static members are data members (variables) or methods that belong to a static or
a non static class itself, rather than to objects of the class.
By declaring a function member as static, we make it independent of any particular
object of the class. Static members always remain the same, regardless of where and how
they are used.
A static member function can be called even if no objects of the class exist and the
static functions are accessed using only the class name.
DISCUSSION ABOUT FRIEND FUNCTION
Syntax:
Friend functions allow alternative syntax to use objects, for instance f(x) instead
of x.f(), or g(x,y) instead of x.g(y). Format of Friend Function --
class class_name {
friend data_type function_name(argument/s);
};
Advantages:
• A friend function is able to access members without the need of inheriting the class.
• The friend function acts as a bridge between two classes by accessing their private
data.
• It can be used to increase the versatility of overloaded operators.
• It can be declared either in the public or private or protected part of the class.
Disadvantages:
o Friend functions have access to private members of a class from outside the class
which violates the law of data hiding.
o Friend functions cannot do any run-time polymorphism in their members.
DISCUSSION ABOUT STATIC MEMBER FUNCTION
An Example :
class scaler{
static int number;
static void get_no_of_topics() //static function declaration
{
cout<<number<<"\n";
};
int scaler::number;
Continued ...
Friend function in C++ is the creative function that breaks the data hiding in an object-
oriented programming language. The data hiding prevents the access of private
members externally from the class. The protected members are inaccessible from the
outside and can only be accessed by the derived classes.
While using the static member function in C++, We must keep in mind that a normal
function can access both non-static as well as static data members but a static member
function can only access the static data member.
Whenever we need the data values to be shared across the objects belonging to the
same class, the data members need to be declared as static.
There are several advantages and disadvantages and applications of the following two
functions, i.e. Friend function and Static member function. We discussed about all those
words in the following presentation.
Thank you