Deprecated: Optional parameter $i declared before required parameter $data is implicitly treated as a required parameter in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php on line 4

Deprecated: Optional parameter $i declared before required parameter $data is implicitly treated as a required parameter in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php on line 245

Deprecated: Optional parameter $output declared before required parameter $attr is implicitly treated as a required parameter in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/snippets.php on line 431

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/mega-menu.php on line 147
Friend Function and Friend Class in C++ with example – AIPS ACADEMY
12th Computer Science BCA CBSE JAC

Friend Function and Friend Class in C++ with example

Friend Function and Friend Class in C++ with example
Written by AIPS

In this topic we will Discuss Best concept and program of Friend Function and Friend Class in C++ with example.

Friend Function :-

  • It is a non-member Function which allows to access private Data member outside of the class.
  • Friend Function is invoked like a simple Function.
  • Friend function is declared with ‘friend’ keyword inside a class in private or public section.
  • Friend Function Requires an object as a function Argument.

Properties of Friend Function

There are following properties of friend function.

  • Friend function is invoked without objects.
  • Friend function can’t access members directly i.e it requires an object to access private or public members of the class.
  • Friend function can be declared in private of public section.
  • Friend function is rarely used because it violets the rule of Data hiding and Encapsulations.

Syntax:-

class class_name

{

friend return_type function_name(Arguments);

};

Example

class num

{

private:

int a,b,c;

friend void sum(num);

};

void sum(num obj)

{

Accessible private or public data

}

About the author

AIPS

Leave a Comment