×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Friend Function and Friend Class in C++ – Complete Tutorial



Last Updated on: 18th Dec 2025 18:10:43 PM

C++ follows the principle of data hiding, where class members are usually kept private.
However, in some real-world situations, trusted external functions or classes need access to private data.

 

For this purpose, C++ provides:

  • Friend Function

  • Friend Class

 

These features allow controlled access to private and protected members.

 

A friend function is a non-member function that is allowed to access the private and protected members of a class. Even though it is not part of the class, it behaves like a trusted helper.

 

Friend Function – Definition

A friend function is a function that is declared using the friend keyword inside a class and can access its private and protected data.

 

Real-life example (Friend Function)

A bank auditor:

  • Not an employee of the bank

  • But allowed to access confidential account details

 

1. Declaring and Using a Friend Function

 

Concept Explanation

  • Friend function is declared inside the class using friend

  • It is defined outside the class

  • It is called like a normal function

 

Example 

#include <iostream>
using namespace std;

class BankAccount {
private:
    int balance;

public:
    BankAccount(int b) {
        balance = b;
    }

    friend void showBalance(BankAccount acc);
};

void showBalance(BankAccount acc) {
    cout << "Account Balance: ₹" << acc.balance << endl;
}

int main() {
    BankAccount account(12000);
    showBalance(account);

    return 0;
}

 

Output

Account Balance: ₹12000

 

Explanation

The friend function accesses the private  balance  of the class directly.

 

Key Points about Friend Function

  • Not a class member

  • Can access private data

  • Declared inside class

  • Defined outside class

  • Called like a normal function

 

Friend Class – Concept Explanation

A friend class is a class that can access all private and protected members of another class. Once a class is declared as a friend, all its member functions gain access.

 

Friend Class – Definition

A friend class is a class whose objects can access private and protected members of another class.

 

Real-life example (Friend Class)

A hospital management system:

  • Doctor class accesses patient’s private medical records

 

1. Declaring and Using a Friend Class

 

Concept Explanation

  • Friend class is declared using  friend class 

  • All functions of friend class can access private data

 

Example

#include <iostream>
using namespace std;

class Patient {
private:
    string disease;

public:
    Patient(string d) {
        disease = d;
    }

    friend class Doctor;
};

class Doctor {
public:
    void diagnose(Patient p) {
        cout << "Patient Disease: " << p.disease << endl;
    }
};

int main() {
    Patient p("Viral Fever");
    Doctor d;
    d.diagnose(p);

    return 0;
}

 

Output

Patient Disease: Viral Fever

 

Explanation

The  Doctor  class accesses the private data of  Patient  using friendship.

 

Friend Function vs Friend Class

Feature Friend Function Friend Class
Access Scope Single function All functions of class
Declaration  friend void func()   friend class ClassName 
Use case Limited access Full class access
Control More restricted Less restricted

 

Important Rules of Friendship

  • Friendship is not mutual

  • Friendship is not inherited

  • Friendship is not transitive

  • Friendship breaks encapsulation (use carefully)

 

Advantages of Friend

  • Controlled access

  • Improves flexibility

  • Useful for operator overloading

  • Supports real-world relationships

 

Disadvantages of Friend

  • Breaks data hiding

  • Reduces security

  • Increases dependency

 

Summary

  • Friend function accesses private data of a class

  • Friend class grants access to all its members

  • Useful when two entities work closely

  • Should be used only when necessary

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo