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...

C++ Lec-1 (Introduction to Programming Languages)

  Introduction to Programming Languages ________________________________________________________________________ P rogramming languages are formal languages comprising a set of instructions that produce various kinds of output. They are used in computer programming to implement algorithms and control the behavior of machines. Language Generation Programming languages have evolved through several generations: First Generation (1GL) : Machine language, the most basic level of programming languages, consisting of binary code. Second Generation (2GL) : Assembly language, a low-level language with a strong correspondence to the machine language instructions. Third Generation (3GL) : High-level languages like C, C++, Java, Python, which are more abstract and easier for humans to understand. Fourth Generation (4GL) : Languages closer to human language, often used for database querying and report generation (e.g., ...

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 ·    ...