0% found this document useful (0 votes)
1 views

oops

The document discusses friend functions and static member functions in object-oriented programming, particularly in C++. Friend functions allow external functions to access private and protected members of a class, while static members belong to the class itself rather than any specific object. The document outlines their characteristics, advantages, disadvantages, and syntax, emphasizing their roles in data access and sharing across class instances.

Uploaded by

SHILPA NASKAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

oops

The document discusses friend functions and static member functions in object-oriented programming, particularly in C++. Friend functions allow external functions to access private and protected members of a class, while static members belong to the class itself rather than any specific object. The document outlines their characteristics, advantages, disadvantages, and syntax, emphasizing their roles in data access and sharing across class instances.

Uploaded by

SHILPA NASKAR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

St.

Thomas College of Engineering and Technology

Title: friend functions and static member


Name: Shilpa Naskar
Class roll no.: 44
University Roll No.: 12200321060
Department: Electronics and Communication
Engineering
Subject: object oriented programming (oe-ec 604c)
Date: 24.01.23
Course Outcome: CO2
Bloom’s Level: apply
INTRODUCTION

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

We know that, in an object-oriented programming language, the members of


the class can only access the data and functions of the class, but the outside
functions cannot do so. Sometimes, the outside functions need to access the private
members of the class. So in that case, friend function comes into the work.
A friend function is a function of the class defined outside the class scope but it
has the right to access all the private and protected members of the class.

Characteristics of FRIEND FUNCTION:


 The function is not in the scope of the class to which it has been declared as a friend.
 It cannot be called using the object as it is not in the scope of that class.
 It can be invoked like a normal function without using the object.
 It cannot access the member names directly and has to use an object name and dot
membership operator with the member name.
 It can be declared either in the private or the public part.
 Friend functions have the same implications on encapsulation as methods.
Continued ...

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

The term "static" is a C++ keyword that provides special characteristics to


variables, functions, and the different elements of the C++ programming language. It
deals with allocating storage for the elements that remain valid throughout the scope of
the whole program. But before further discussion about static member function, we
need to know about Static data members.
Static data members in C++ are used to store values common to the entire
class. A static data member is available globally for all the objects of the same class. It
remains in the memory for the entire program but is visible only within the class or
the function within which it is defined.
To make a data member static,
1. Declaration of the data member should be within the class definition.
2. Its definition should be outside the class definition.
3. We cannot declare a static data member if a class is defined inside a function.
Continued ...

Syntax for Static data member:


1. Within the class definition
class scaler{
static int number;
};
2. Outside the class definition
int scaler::number;

Syntax for Static member function:


static returntype function_name(){}

An Example :
class scaler{
static int number;
static void get_no_of_topics() //static function declaration
{
cout<<number<<"\n";
};
int scaler::number;
Continued ...

Properties of Static member function:


o It is independent of any object of the class.
o It can be called even if no objects of the class exist.
o It can also be accessed using the class name through the scope resolution operator.
o It can access static data members and static member functions inside or outside of
the class.
o Static member functions have a scope inside the class and cannot access the current
object pointer.

Need of Static member function:


 We can also use a static member function to determine how many objects of the
class have been created.
 Static member variables and functions are used when common values are to be
shared across all the objects.
 Static members are frequently used to store information that is shared by all objects
in a class.
Wrapping Up

 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

You might also like