Saral Code logo
Saral Code
    HomeBlogTutorialsMCQs

No Related Topics Available


In C++, a friend function is a function that is granted access to the private and protected members of a class. It is declared inside the class, but it is not a member of the class. Friend functions are useful when you need to allow an external function to access and manipulate the private or protected data of a class. Let's explore the concept of friend functions in C++ with simple examples.

Introduction to Friend Functions

Consider a scenario where you have a class that has private data members, and you want to allow a specific function to access and modify those private members. Here's where friend functions come into play. A friend function is declared inside the class, and it has the keyword friend preceding its declaration.

C++
class MyClass {
private:
    int privateData;

public:
    MyClass(int data) : privateData(data) {}

    // Declaration of the friend function
    friend void AccessPrivateData(MyClass obj);
};

In this example, the AccessPrivateData function is declared as a friend function inside the MyClass class. This grants the AccessPrivateData function the ability to access the private data member privateData of any MyClass object.

Usage of Friend Functions

To define a friend function outside the class, you don't need to use the scope resolution operator ::. Instead, you can define it as a normal non-member function, but you need to declare it as a friend function within the class.

C++
void AccessPrivateData(MyClass obj) {
    std::cout << "Private data: " << obj.privateData << std::endl;
}

In this example, the AccessPrivateData function takes a MyClass object as a parameter and prints the value of its private data member privateData. Since the AccessPrivateData function is declared as a friend function within the MyClass class, it can directly access the private data member.

Real-World Example: Bank Account Class

Let's consider a practical example of using a friend function in a BankAccount class. The BankAccount class has a private data member balance, and we want to allow the function WithdrawFunds to access and modify the balance even though it is a private member.

C++
class BankAccount {
private:
    double balance;

public:
    BankAccount(double initialBalance) : balance(initialBalance) {}

    // Declaration of the friend function
    friend void WithdrawFunds(BankAccount& account, double amount);

    double GetBalance() const {
        return balance;
    }
};

In this example, the WithdrawFunds function is declared as a friend function within the BankAccount class, granting it access to the private member balance of BankAccount objects.

C++
void WithdrawFunds(BankAccount& account, double amount) {
    if (amount <= account.balance) {
        account.balance -= amount;
        std::cout << "Withdrawn amount: " << amount << std::endl;
    } else {
        std::cout << "Insufficient funds!" << std::endl;
    }
}

The WithdrawFunds function takes a BankAccount object and a withdrawal amount as parameters. It checks if the requested withdrawal amount is less than or equal to the account's balance. If it is, it deducts the amount from the account's balance. Otherwise, it prints an error message.

C++
int main() {
    BankAccount account(1000.0);
    std::cout << "Initial balance: " << account.GetBalance() << std::endl;

    WithdrawFunds(account, 500.0);
    std::cout << "Remaining balance: " << account.GetBalance() << std::endl;

    return 0;
}

In the main function, we create a BankAccount object account with an initial balance of 1000.0. We then call the WithdrawFunds function, passing the account object and a withdrawal amount of 500.0. The function is able to access and modify the private member balance of the account object.

By using a friend function, we provide controlled access to the private members of a class to external functions or classes, enabling them to perform specific tasks while maintaining encapsulation.

Summary

A friend function in C++ is a non-member function that is granted access to the private and protected members of a class. It is declared inside the class using the friend keyword. Friend functions are useful when you need to allow external functions to access and manipulate private or protected data of a class. They promote encapsulation by providing controlled access to class members.