Friend Function
Thread Tags
Adobe Illustrator Adobe InDesign Adobe Photoshop Android Development Android Programming Android Studio Automation Testing bitmap file Brochure design C / C++ Programming Career Option Career Options College Lounge css CSS/HTML sandbox digital marketing Digital Marketing / SEO Fireworks Graphic Design Graphic designing Graphics Design Graphics Designing HTML? Illustrator Internship Training Liquify Tools logo Design logo designing Mobile UI Development Photoshop QA react-native Responsive Design SEO Testing Typography UI UI/UX Development UI Design UI Designing UI development User Interface UX Design Web Designing Website Design-
Register for free!
Registration at Smart Mentors is completely free and takes only a few seconds. By registering you’ll gain:
- Full Posting Privileges.
- Access to Private Messaging.
- Optional Email Notification.
- Ability to Fully Participate.
Register Now, or check out the Site Tour and find out everything Smart Mentors has to offer.
Tagged: C / C++ Programming
This thread contains 1 reply, has 2 voices, and was last updated by Test 5 years, 10 months ago.
-
Author Replies
-
-
February 4, 2019 at 10:29 am #52987
Friend function goes about as a friend of the class. It can get to the private and protected members from the class. The friend function isn’t a member from the class, however, it must be recorded in the class definition.
The non-member function can’t get to the private data of the class. At times, it is essential for the non-member function to get to the data.
The friend function is a non-member function and can get to the private data of the class.
Following are the qualities of a friend function:
1) The friend function isn’t to the extent of the class in which it has been declared.
2) Since it isn’t to the extent of the class, so it can’t be called by utilizing the object of the class. Consequently, friend function can be invoked like a normal function.
3) A friend function can’t get to the private members directly, it needs to utilize an object name and dot operator with every member name.
4) Friend function utilizes objects as contentions.
Let’s understand this through an example:
#include <iostream>
using namespace std;
class Addition
{
int a=4;
int b=6;
public:
friend int add(Addition x)
{
return(x.a+x.b);
}
};
int main()
{
int result;
Addition x;
result=add(x);
cout<<result;
return 0;
}Output: 10
-
AuthorPosts
You must be logged in to reply to this thread.Please login or register. Registration is 100% free.