C++ is a cross-platform language that is used to create high-performance applications. Edureify, the best AI Learning App has already provided an informative article on the C++ Programming Language.
In this article, Edureify brings the details of the Friend Function of C++, an essential component that enables C++ to be such a user-friendly programming language.
Read on to know more about the Friend Function of C++.
The Friend Function in C++
The Friend Function in C++ is defined as the function that authorizes access to all the protected and private members of the class. A Friend Function can be either a member of another class or a global function.
Some of the features of the Friend Function are-
- It is a special function in C++ that has access to private and protected data of a class without even being a member function of a class.
- It is a non-member function or ordinary function of a class. When a function is declared as a Friend, it gets permission to all the access.
- The keyword Friend is placed in the function declaration and not in the function definition
- This function uses the dot operator and the name of the object with the member name to access the names of the members.
- The Friend Function can be declared in any section of the class, i.e., either the public or private or protected sector of the class.
- The scope of the class does not declare the Friend Function
Friend Function Syntax
The following is the syntax of the Friend Function in C++ –
class Box { double width; public: double length; friend void printWidth (Box Box) ; void setWidth (double wid) ; };
Advantages of Friend Function
The following are the advantages of the Friend Function-
- The programmer can generate more efficient codes
- Permits the sharing of private class information by a non-member function
- Non-public members of a class can be easily accessed
- The friend function is used more when interrelated members relative to other parts of the program are contained in two or more classes
- Permits addition functionality that is not commonly used by the class
Declaring a Friend Function in C++
When a class is declared as a friend class, all the friend class members become friend functions. The Friend Function in C++ can be declared in the following ways-
Class class_name { Friend data_type function_name(argument/s); };
Another way of declaring the function is-
class classB; class classA { //ClassB is a friend class of ClassA friend class ClassB; …. } class ClassB { …. }
Here, the members of class A can be accessed from class B. But the members of class A cannot access the members of class B.
Example of the Friend Function-
The following is a simple program that shows the working of the Friend Function-
#include <iostream> using namespace std; class Distance { private: int meter; // friend function friend int addFive(Distance); public: Distance() : meter(0) {} }; //friend function definition int addFive(Distance d) { //accessing private members from the friend function d.meter += 5; return d.meter; } int main() { Distance D; cout << “Distance:” <<addFive(D); return 0; }
Output: Distance: 5
The following is another example of adding members of two different classes using the friend function-
#include <iostream> using namespace std; // forward declaration class ClassB; class ClassA { public: // constructor to initialize numA to 12 ClassA() : numA(12) {} private: int numA; // friend function declaration friend int add(ClassA. ClassB); }; class ClassB { public: // constructor to initialize numB to 1 ClassB() : numb(1) {} private: int numB; // friend function declaration friend int add(ClassA, ClassB); }; // access members of both classes int add(ClassA objectA, ClassB object) { return (objectA.numA + objectB.objectB); } int main() { ClassA objectA; ClassB objectB; cout << “Sum:” << add(objectA, objectB); return 0; } Output: Sum: 13
C++ Friend Class
When class B is the friend of class A, class B has access to all the members of class A. The add() function is created in class B. The add() function returns the sum of numA and numb. It is simple to create objects of class A inside class B for the latter is a friend class.
Here is a program that showcases the working of C++ friend class-
#include <iostream> using namespace std; // forward declaration class ClassB; class ClassA { private: //int numA; // friend class declaration friend class ClassB; public: // constructor to initialize numA to 12 ClassA() : numA(12) {} }; class ClassB { private: int numb; private: int numB; public: // constructor to initialize numB to 1 ClassB() : numb(1) {} // member function to add numA // from ClassA and numB from ClassB int add() { ClassA objectA; return (objectA.numA + objectB.objectB); } }; int main() { ClassB objectB; cout << “Sum:” << object.add(); return 0; } Output: Sum: 13
Here were the examples and details of the Friend Function in C++. Edureify with its coding Bootcamp job-ready courses aims to provide its students with all the knowledge that will help them understand these programming languages and develop their desired applications.
Joining the Edureify coding Bootcamp job-ready courses will benefit the students in the following ways-
- Students will get to learn from the industry experts by attending their live classes
- By joining our 6 months program that includes 2 months of coding Bootcamp, students will have 200+ learning hours
- Our courses aim to make the students job-ready, i.e., students can participate to build real-life projects and will have to work on 10+ real-world capstone projects and assignments
- Students can get all their doubts cleared on the go
- If a student misses a live class, he/she will have access to the recorded lectures
- We provide one-on-one mentorship and career guidance
Along with C++, Edureify also has educative articles on Heroku, Ruby, Swift, JavaScript, Python For Loop, and other programming languages and tools. Learn more with Edureify about such technical skills.
Some FAQs on Friend Function in C++ –
1. What is the Friend Function in C++?
The Friend Function in C++ is defined as the function that authorizes access to all the protected and private members of the class. A Friend Function can be either a member of another class or a global function.
2. Mention 4 features of the Friend Function in C++?
The 4 features of the Friend Function in C++ are-
- The scope of the class does not declare the Friend Function
- It is a special function in C++ that has access to private and protected data of a class without even being a member function of a class.
- It is a non-member function or ordinary function of a class. When a function is declared as a Friend, it gets permission to all the access.
- The keyword Friend is placed in the function declaration and not in the function definition
3. What is the C++ Friend Class?
When class B is the friend of class A, class B has access to all the members of class A. The add() function is created in class B. The add() function returns the sum of numA and numb. It is simple to create objects of class A inside class B for the latter is a friend class.
4. Provide the syntax for Friend Function in C++.
The syntax of the Friend Function in C++ is-
class Box { double width; public: double length; friend void printWidth (Box Box) ; void setWidth (double wid) ; }; 5. From where can I learn more about C++ and other programming languages?
Edureify with its coding Bootcamp job-ready courses will provide all the important courses and study materials that students who would like to know more about programming languages can avail of.