Popular
Data Science
Technology
Finance
Management
Future Tech
In the dynamic realm of programming, C++ stands tall as a versatile language, continually sought after by professionals ranging from software developers to game designers and backend engineers. Holding a reliable reputation, C++ boasts a rich feature set, including the intriguing concept of friend functions. In C++, a friend function is a unique entity defined outside the class’s boundaries yet bestowed with the remarkable authority to access both protected and private members of the class it befriends. Despite not being a member function itself, the prototype of a friend function finds its home within the class declaration, showcasing the language’s elegance and flexibility.
This capability isn’t limited solely to classes; it expands its scope to encompass class templates, function templates, and even standard functions and member functions, affirming its status as a potent asset in the toolkit of C++ developers. The TIOBE index of 2022 underscores C++’s prominence as the fourth most widely used language worldwide, underscoring its enduring significance and broad acceptance in the constantly evolving realm of technology.
In C++, the friend function is declared outside the class’s scope, granting it access to both protected and private members of the class. While not a member function, the prototypes for friend functions are specified within the class declaration. This encompasses class templates, classes, function templates, functions, and member functions, allowing the friend function to access all members of the class.
The friend class offers various advantages and applications. Some key scenarios include:
In C++, friend functions come in the following variations:
The syntax for implementing a friend class is as follows:
As evident from the syntax, simply prepend the keyword ‘friend’ before a class to designate it as a friend class. Employing this syntax will establish ClassB as a friend class of ClassA. As ClassB assumes the role of a friend class, it gains access to all public, private, and protected members of ClassA. Conversely, the reverse is not applicable. This is because C++ permits only the granting of the friend relationship, not its reciprocation. Consequently, ClassA won’t be able to access the private members of ClassB.
Let’s see the simple friend function in c++ example used to print the length of a box.
Output:
Let’s see a simple example when the function is friendly to two classes.
Output:
In the above example, min() function is friendly to two classes, i.e., the min() function can access the private members of both classes A and B.
The friend function is one of the powerful tools in C++ that violates the encapsulation barrier and provides access by non-member functions to private and protected members of the class. Such a facility provides ease with great versatility for relating to complex relationships among classes. As you further reach into the realm of software development, consider expanding your skills with an Advanced Certification Program in Data Science & Analytics. Empower yourself with critical skills and knowledge to enable you to be successful in this rapidly changing domain and prepare for many highly-rewarding careers within the context of data-driven innovation. Take advantage of the opportunity for increasing your skill level in data science and analytics today!
The DevOps Playbook
Simplify deployment with Docker containers.
Streamline development with modern practices.
Enhance efficiency with automated workflows.
Popular
Data Science
Technology
Finance
Management
Future Tech
Accelerator Program in Business Analytics & Data Science
Integrated Program in Data Science, AI and ML
Certificate Program in Full Stack Development with Specialization for Web and Mobile
Certificate Program in DevOps and Cloud Engineering
Certificate Program in Application Development
Certificate Program in Cybersecurity Essentials & Risk Assessment
Integrated Program in Finance and Financial Technologies
Certificate Program in Financial Analysis, Valuation and Risk Management
© 2024 Hero Vired. All rights reserved