Friend function in C ++

The friend function in C ++ of a class is defined outside of that class scope, but it has access to all private and protected members of that class. Even if the prototypes for friend functions appear in the class definition, friend functions are not member functions.

A friend can be a function, a function pattern, or a member function, or a class or a class template, in this case, the entire class and all its members are friends.

To declare a function of friend function of a class, precede that function prototype in class definition with friend keyword in C ++, as follows:

 class Box { double chieurong ; public : double chieudai ; friend void inChieuRong ( Box box ); void setChieuRong ( double rong ); }; 

To declare all member functions of the LopMai class as friend type of LopMot class, put a following declaration in the definition of LopMot class:

 friend class LopHai ; 

You consider the following program:

 #include using namespace std ; class Box { double chieurong ; public : friend void inChieuRong ( Box box ); void setChieuRong ( double rong ); }; // phan dinh nghia ham thanh vien void Box :: setChieuRong ( double rong ) { chieurong = rong ; } // Ghi chu: inChieuRong() khong la ham thanh vien cua bat cu class nao. void inChieuRong ( Box box ) { /* Boi vi, ham inChieuRong() la ham friend cua Box, do vay no co the truc tiep truy cap bat cu thanh vien nao cua class nay */ cout << "Chieu rong cua box la: " << box . chieurong << endl ; } // ham main cua chuong trinh int main ( ) { Box box ; // thiet lap chieurong cua box, khong su dung ham thanh vien box . setChieuRong ( 25.3 ); // su dung ham friend de in chieu rong. inChieuRong ( box ); return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Friend function in C ++ Picture 1

According to Tutorialspoint

Previous article: Copy constructor in C ++

Next lesson: Inline function in C ++

3.5 ★ | 2 Vote

May be interested

  • DAY function in SQL ServerDAY function in SQL Server
    the day function in sql server returns an integer that is the day of the month (from 1 to 31) from the time passed.
  • MIN function in SQL ServerMIN function in SQL Server
    the article will explore and show you how to use the min function in sql server to find the smallest value in a set.
  • MAX function in SQL ServerMAX function in SQL Server
    the article will explore and show you how to use the max function in sql server to find the maximum value in a set.
  • SUM function in SQL ServerSUM function in SQL Server
    the sql server sum function returns the total value of a column, a data set, or an expression.
  • The ord () function in PythonThe ord () function in Python
    the built-in ord () function in python returns an integer representing the unicode code of the specified character.
  • RIGHT function in SQL ServerRIGHT function in SQL Server
    the article will explore and guide you how to use the right function in sql server to extract some characters from the right side of a given string.
  • Int () function in PythonInt () function in Python
    the int () function in python returns an integer object from any number or string.
  • AVG function in SQL ServerAVG function in SQL Server
    the avg function in sql server returns the average value of an expression or average value according to the specified column of the selected row.
  • Hex () function in PythonHex () function in Python
    the hex () function is one of python's built-in functions, used to convert an integer into the corresponding hexadecimal form.
  • ABS function in SQL ServerABS function in SQL Server
    this article will show you in detail how to use the abs () handling function in sql server with specific syntax and examples to better visualize and capture functions.