Skip to main content

Friend Function in C++

 

Friend Function in C++


Definition

A friend function in C++ is a special function that can access private and protected members of a class, even though it is not part of that class. Normally, private and protected members of a class can only be accessed by the class's own methods or its friends. To make a function a friend of a class, we declare it inside the class definition using the keyword friend.

 

Why Do We Use Friend Functions?

We use a friend function to allow an external function access to private or protected data of a class without making it a member of that class.

 

Need for Friend Functions

1.    Access Private Data: Helps an external function work with private or protected members of a class.

2.    Convenience: Simplifies code by avoiding the use of getters or setters.

3.    Specialized Use: Useful when an external function needs a special relationship with the class.

 

Advantages of Friend Functions

1.    Direct access to private and protected members of a class.

2.    Simplifies code by reducing the need for additional public methods.

3.    Increases flexibility by allowing interaction with multiple classes.

 

Disadvantages of Friend Functions

1.    Breaks encapsulation by exposing private data to external functions.

2.    Introduces tight coupling, making maintenance harder.

3.    Friendship is not inherited, so it must be declared for child classes explicitly.

 

Example of a Friend Function

#include <iostream>

using namespace std;


class abc {

private:

    int sal = 40000;

protected:

    int pm = 5000;

public:

    friend void show( abc &obj);

};

void show(abc &obj) {


    cout << "Salary is " << obj.sal <<endl;

cout<<"Pocket Money is " << obj.pm << endl;

}

int main() {

    abc o1;

    show(o1);

    return 0;

}


 

How Friend Functions Work?

  • Declared as a friend inside the class.
  • Defined outside the class.
  • Accesses private and protected members using an object of the class.

 

Keep in mind at the time of interview:

  • Friendship is not mutual.
  • Friendship is not transitive.
  • Friendship is not inherited by derived classes.

 

 

Real-Life Example

A house with locked rooms (private data) can only be accessed by the owner (class methods). However, the owner can give a trusted friend like me its Ajay Wadekar Sir (friend function) a key to access the rooms when needed.

 

When to Avoid Friend Functions?

1.    If getters and setters can achieve the same result.

2.    Overusing friend functions can harm code design and violate encapsulation.




#Happy Coding by AJAY WADEKAR

 

Comments

Popular posts from this blog

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...

Difference Between POP and OOP

  What is the Difference Between POP and OOP? Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP) are two fundamental programming paradigms. Both have their own methodologies, approaches, and use cases. Below, we will define both paradigms, outline their key differences, and provide examples of each for better understanding.   Definition of POP (Procedure-Oriented Programming) Procedure-Oriented Programming (POP) is a programming paradigm where programs are designed around procedures or functions. It focuses on dividing the program into smaller units called functions, which operate on data. Key Features of POP: Program is divided into functions or procedures. Functions are the main building blocks of the program. Emphasizes a step-by-step procedural approach. Data and functions are separate, with functions manipulating data.   Definition of OOP (Object-Oriented Programming) Object-Oriented Programming (OOP) is a p...