Skip to main content

C++: 5. Friend function

If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function.

By using the keyword friend compiler knows the given function is a friend function.

For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.

Syntax:-

class class_name    
{    
    friend data_type function_name(argument/s);            // syntax of friend function.  
};    

Example:- 
#include <iostream>    
using namespace std;    
class Box    
{    
    private:    
        int length;    
    public:    
        Box(): length(0) { }    
        friend int printLength(Box); //friend function    
};    
int printLength(Box b)    
{    
   b.length += 10;    
    return b.length;    
}    
int main()    
{    
    Box b;    
    cout<<"Length of box: "<< printLength(b)<<endl;    
    return 0;    
}    

Output:- 
Length of box: 10  


Characteristics of a Friend function:
  1. The function is not in the scope of the class to which it has been declared as a friend.
  2. It cannot be called using the object as it is not in the scope of that class.
  3. It can be invoked like a normal function without using the object.
  4. It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
  5. It can be declared either in the private or the public part.
Length of box: 10  
Length of box: 10  Length of box: 10  


Comments

Popular posts from this blog

WP: 8. Dynamically show post taxonomies and their data in tab format [Solved]

    Today I ran into a problem, I need to dynamically show post taxonomies and their data in tab format and found this solution. And nonetheless, It's better to fix the thing with some bunch of code rather than uploading one more bulky plugin on your website that will end up making your site heavy. Just follow the steps below and avoid heading up into any malicious or bulky plugin for this particular problem from now on forever. Step 1: Get the taxonomies terms first <?php     $terms = get_terms( array(       'taxonomy' => 'off_plan_categories',       'hide_empty' => true,     ) );     // echo '<pre>';     // print_r($terms);     // die; ?> Step 2:  Execute loop first for tab navigation <nav id="offPlanPropertyTabs" class="offplan-property-type-tabs nav nav-tabs"> <?php foreach($terms as $key => $term): ?>   <button class="nav-item nav-link <?php...

JS: Cheatsheet

   Datatypes Difference Between Var, Let and Const Functions and its types Asynchronous operations in JS

Py: 14. Python GUI

Python provides the standard library Tkinter for creating the graphical user interface for desktop-based applications. Developing desktop-based applications with python Tkinter is not a complex task. An empty Tkinter top-level window can be created by using the following steps. Exercises:-  Write a py program to create a window with a simple message. Write a py program to demonstrate Grid. Write a py program to generate a button. Write a py program to take user input in GUI format. Write a py program to perform arithmetic operation in GUI format.