codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

46 / 87 topics
38OOP Concepts Overview39Classes and Objects40Class Methods41Constructors & Constructor Overloading42Destructors43Access Modifiers / Specifiers44Encapsulation45Abstraction46Friend Functions and Friend Classes47Operator Overloading
Tutorials/C++ Programming/Friend Functions and Friend Classes
⚡C++ Programming

Friend Functions and Friend Classes

Updated 2026-04-20
3 min read

Friend Functions and Friend Classes

In object-oriented programming, encapsulation is a fundamental concept that restricts direct access to some of an object's components. However, there are scenarios where you might need to allow certain functions or classes to bypass these restrictions. This is where friend functions and friend classes come into play. In this tutorial, we will explore how to use friend functions and friend classes in C++.

Introduction

Encapsulation ensures that the internal representation of an object is hidden from the outside world, which helps maintain data integrity and security. However, sometimes it's necessary for certain parts of your program to access private or protected members of a class. This is where friends come into the picture. A friend function or class can access the private and protected members of another class.

Friend Functions

A friend function is a non-member function that has been granted special access privileges by a class. It's not a member of the class, but it can access the private and protected members of the class as if it were.

Declaring a Friend Function

To declare a friend function within a class, you use the friend keyword followed by the function declaration. The function must be defined outside the class.

class MyClass {
private:
    int secret;

public:
    MyClass(int s) : secret(s) {}

    // Declare a friend function
    friend void displaySecret(const MyClass& obj);
};

// Define the friend function
void displaySecret(const MyClass& obj) {
    std::cout << "The secret is: " << obj.secret << std::endl;
}

Usage

In this example, displaySecret is declared as a friend of MyClass. It can access the private member secret of any MyClass object.

int main() {
    MyClass obj(42);
    displaySecret(obj);  // Outputs: The secret is: 42
    return 0;
}

Best Practices

  • Use sparingly: Friend functions break encapsulation, so they should be used only when necessary.
  • Document well: Since friend functions are not members of the class, it's important to document their purpose and usage clearly.

Friend Classes

A friend class is a class that has been granted special access privileges by another class. All public, protected, and private members of the granting class are accessible to the friend class.

Declaring a Friend Class

To declare a friend class within a class, you use the friend keyword followed by the class declaration.

class SecretHolder {
private:
    int secret;

public:
    SecretHolder(int s) : secret(s) {}

    // Declare a friend class
    friend class SecretViewer;
};

class SecretViewer {
public:
    void viewSecret(const SecretHolder& holder) {
        std::cout << "The secret is: " << holder.secret << std::endl;
    }
};

Usage

In this example, SecretViewer is declared as a friend of SecretHolder. It can access the private member secret of any SecretHolder object.

int main() {
    SecretHolder holder(42);
    SecretViewer viewer;
    viewer.viewSecret(holder);  // Outputs: The secret is: 42
    return 0;
}

Best Practices

  • Use carefully: Friend classes have even more access than friend functions, so they should be used with caution.
  • Minimize dependencies: Limit the number of classes that are friends to reduce coupling and improve maintainability.

Real-World Example: Database Access Control

Consider a scenario where you have a Database class with sensitive data. You want to allow only certain operations (like backup and restore) to access this data.

class Database {
private:
    std::string connectionString;

public:
    Database(const std::string& connStr) : connectionString(connStr) {}

    // Declare friend functions
    friend void backupDatabase(const Database& db);
    friend void restoreDatabase(Database& db, const std::string& backupData);

    // Public interface for other operations
    void connect() {
        std::cout << "Connecting to database: " << connectionString << std::endl;
    }
};

// Define the friend function for backup
void backupDatabase(const Database& db) {
    std::cout << "Backing up database with connection string: " << db.connectionString << std::endl;
}

// Define the friend function for restore
void restoreDatabase(Database& db, const std::string& backupData) {
    std::cout << "Restoring database from backup data." << std::endl;
    // Restore logic here
}

Usage

In this example, backupDatabase and restoreDatabase are friend functions of the Database class. They can access the private member connectionString.

int main() {
    Database db("server=myServerAddress;database=myDataBase;");
    db.connect();  // Outputs: Connecting to database: server=myServerAddress;database=myDataBase;
    backupDatabase(db);  // Outputs: Backing up database with connection string: server=myServerAddress;database=myDataBase;
    restoreDatabase(db, "backupData");  // Outputs: Restoring database from backup data.
    return 0;
}

Conclusion

Friend functions and friend classes provide a way to relax the strict encapsulation rules of C++. They are powerful tools that can be used to implement complex access control mechanisms. However, they should be used judiciously to maintain clean and maintainable code.

By understanding when and how to use friend functions and classes, you can write more flexible and robust C++ programs. Always consider the trade-offs between encapsulation and functionality, and document your design choices clearly.


This tutorial provides a comprehensive guide to using friend functions and friend classes in C++. By following the examples and best practices outlined here, you'll be able to effectively manage access control in your object-oriented designs.


PreviousAbstractionNext Operator Overloading

Recommended Gear

AbstractionOperator Overloading