

{"id":60617,"date":"2019-06-26T11:37:12","date_gmt":"2019-06-26T06:07:12","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=60617"},"modified":"2020-02-03T16:34:04","modified_gmt":"2020-02-03T11:04:04","slug":"friend-function-in-cpp","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/","title":{"rendered":"2 Ways to Implement Friend Function in C++ (Including Practical Example)"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1489,&quot;href&quot;:&quot;http:\\\/\\\/isocpp.org&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251011150840\\\/https:\\\/\\\/isocpp.org\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 08:59:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-13 05:48:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-16 20:22:08&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-20 04:04:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-23 18:51:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-27 09:06:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-30 12:29:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-03 20:51:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-07 14:02:22&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-11 18:34:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-16 05:03:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-19 19:05:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-23 14:06:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-27 05:21:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-30 17:35:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-03 15:23:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-08 14:52:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 00:35:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-16 01:54:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 03:33:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-24 16:10:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 10:34:35&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-06 05:38:44&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-13 21:14:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-18 07:54:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 13:35:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 18:02:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-30 11:02:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-03 10:53:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-06 11:25:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-10 14:35:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 16:31:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-17 19:01:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-21 18:12:32&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-26 14:26:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-02 18:59:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-09 19:52:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-13 17:09:34&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-16 22:24:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-20 16:07:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-25 10:53:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-30 18:56:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-04 02:12:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-10 17:45:28&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-10 17:45:28&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p><em><strong>C++ Friend Function: Where friends have access to your private members<\/strong><\/em><\/p>\n<p>It is one of the most popular and important features of classes and objects in C++. Let us understand the concept with a real-life example.<\/p>\n<p>There are numerous things that we keep confidential from strangers in order to maintain our own privacy, but we might encounter certain unforeseen situations where we need to share this confidential information with some people.<\/p>\n<p>We do not share this confidential or private data or information to any stranger, but only to the people, we are close to. Just like we share some of the private things of our lives with our friends, C++ allows classes to share their private data members and member functions with friend functions when required.<\/p>\n<h2>Friend Function in C++<\/h2>\n<p><em>In programming terminology, a friend function helps you access the private and protected members of another class in which it is declared with the keyword \u2018friend\u2019.<\/em><\/p>\n<p>It is important to note that in order to use a friend function and to access the non-public members of a class, we need to declare it within the body of the class itself.<\/p>\n<p><em><strong>Wait!! Have you\u00a0checked our Tutorial on <a href=\"https:\/\/data-flair.training\/blogs\/cpp-class-and-object\/\">Classes and Object in C++<\/a><\/strong><\/em><\/p>\n<h4><strong>Friend functions in C++ can be of two types:<\/strong><\/h4>\n<ul>\n<li><strong>A Method of another class:<\/strong> When we have more than one class and we want to access the non-public data members of a particular class, we declare a friend class.<\/li>\n<li><strong>A Global function:<\/strong> A global friend function allows you to access all the private and protected members of the class, unlike a normal friend function that allows you to access only specific members of the class.<\/li>\n<\/ul>\n<p><em>C++ gains an upper edge over other programming languages that do not support the concept of friend functions, like Java.<\/em><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60711\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg\" alt=\"Friend Function in C++\" width=\"802\" height=\"427\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C--150x80.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C--300x160.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C--768x409.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C--520x277.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Syntax of Friend Function in C++<\/h2>\n<p>Now that we have understood what are friend functions, let us proceed further by understanding the declaration and definition of a friend function in C++.<\/p>\n<h3>1. Declaration<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Class_name\r\n{\r\n\r\nAccess specifiers \/\/ private or protected\r\n.\r\n.\r\nfriend return_type function_name(argument(s));\r\n.\r\n.\r\n\r\n}<\/pre>\n<h3>2. Definition<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Class_name\r\n{\r\n\r\nAccess specifiers \/\/ private or protected\r\n.\r\n.\r\nfriend return_type function_name(parameter(s));\r\n.\r\n.\r\n}\r\n\r\nreturn_type function_name(parameter(s))\r\n{\r\n.\r\n.\r\n.\r\n\r\n\/* Accessing private and protected data members of the class with the help of friend function with name function_name *\/\r\n\r\n.\r\n.\r\n.\r\n\r\n}\r\n\r\n<\/pre>\n<h2>C++ Friend Function Implementation<\/h2>\n<p>After developing an understanding as to what friend functions are, let us see how we can implement it.<\/p>\n<p>As discussed earlier, there are two ways in which we can implement friend functions in C++ as a method of a class or as a global friend function.<\/p>\n<p><em><strong>Learn <a href=\"https:\/\/data-flair.training\/blogs\/virtual-function-in-cpp\/\">Virtual Function in C++<\/a> with Real-time Example<\/strong><\/em><\/p>\n<h3>1. Implementation through a method of another class<\/h3>\n<p>Here is a C++ that shows the implementation of friend functions as a method of a class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include &lt;iostream&gt; \r\nusing namespace std;\r\n\r\nclass Base \r\n{ \r\nprivate: \r\nint base_value; \r\npublic: \r\nBase() \/\/ Default constructor \r\n{ \r\nbase_value = 10; \r\n} \r\nfriend class Derived; \/\/ Friend Class \r\n}; \r\n\r\nclass Derived \r\n{ \r\nprivate: \r\nint derived_value; \r\npublic:\r\n\r\n\/*Accessing private members of the base class using friend class Derived *\/\r\nvoid display(Base&amp; x) \r\n{ \r\ncout&lt;&lt;\"The base value accessed using friend class is: \" &lt;&lt; x.base_value&lt;&lt;endl; \r\n} \r\n}; \r\n\r\nint main() \r\n{\r\n\r\ncout&lt;&lt;\"Welcome to DataFlair tutorials!\"&lt;&lt;endl&lt;&lt;endl;\r\n\r\nBase base_value; \r\nDerived derived_value; \r\nderived_value.display(base_value); \r\nreturn 0; \r\n}<\/pre>\n<p><strong>Code-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60702\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation.jpg\" alt=\" Implementation ofFriend Function in C++ with Another class\" width=\"1304\" height=\"678\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation.jpg 1304w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation-150x78.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation-300x156.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation-768x399.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation-1024x532.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/C-Friend-Function-Implementation-520x270.jpg 520w\" sizes=\"auto, (max-width: 1304px) 100vw, 1304px\" \/><\/a><\/p>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60703\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function.jpg\" alt=\"Output of C++ Friend Function\" width=\"1303\" height=\"540\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function.jpg 1303w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function-150x62.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function-300x124.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function-768x318.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function-1024x424.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-C-Friend-Function-520x216.jpg 520w\" sizes=\"auto, (max-width: 1303px) 100vw, 1303px\" \/><\/a><\/p>\n<p><em><strong>Have you checked the <a href=\"https:\/\/data-flair.training\/blogs\/inline-function-in-c-plus-plus\/\">Inline Function in C++<\/a>?<\/strong><\/em><\/p>\n<h3>2. Implementation through Global Friends<\/h3>\n<p>Here is a C++ that shows the implementation of global friend functions in a class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass Program \r\n{\r\n\/\/ private by default\r\nstring data;\r\n\r\npublic:\r\nfriend void display( Program value );\r\nvoid input( string val );\r\n};\r\n\r\nvoid Program::input( string val ) \/\/ Input definition of member function\r\n{\r\ndata = val;\r\n}\r\n\r\nvoid display( Program value ) \r\n{\r\ncout&lt;&lt;\"The data stored in the string is : \"&lt;&lt;value.data&lt;&lt;endl;\r\n}\r\n\r\nint main() \r\n{\r\n\r\ncout&lt;&lt;\"Welcome to DataFlair tutorials!\"&lt;&lt;endl&lt;&lt;endl;\r\n\r\nProgram value;\r\n\r\nvalue.input(\"DataFlair\");\r\ndisplay( value ); \/\/ Use of friend function to display the string\r\nreturn 0;\r\n}\r\n<\/pre>\n<p><strong>Code-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60704\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends.jpg\" alt=\"Implementation through Global Friends in C++\" width=\"1301\" height=\"697\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends.jpg 1301w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends-150x80.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends-300x161.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends-768x411.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends-1024x549.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Implementation-through-Global-Friends-520x279.jpg 520w\" sizes=\"auto, (max-width: 1301px) 100vw, 1301px\" \/><\/a><\/p>\n<p><strong>Output-<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60705\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C.jpg\" alt=\"Output of Global Friends in C++\" width=\"1301\" height=\"559\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C.jpg 1301w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C-150x64.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C-300x129.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C-768x330.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C-1024x440.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Output-of-Global-Friends-in-C-520x223.jpg 520w\" sizes=\"auto, (max-width: 1301px) 100vw, 1301px\" \/><\/a><\/p>\n<h2>Characteristics of Friend Function in C++<\/h2>\n<p>In order to summarize some of the glitches involved in friend functions, we need to keep the following key points while working with friend functions in<a href=\"http:\/\/isocpp.org\"> C++.<\/a><\/p>\n<p>The implementation of friend functions is not commutative in nature unlike seen in Inheritance. For example, if class A is a friend to class B, then it does not mean class B is also a friend to class A. This means that class A can access the non-public members of class B, but, class B cannot access the non-public members of class A.<\/p>\n<p><em>You cannot implement the concept of Inheritance and friend functions in the same class.<\/em><\/p>\n<p>Excessive use of friend functions in C++ degrades the <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/data-abstraction-in-c-plus-plus\/\">importance of data encapsulation<\/a><\/strong><\/em>. Therefore, it should be used only where it is necessary.<\/p>\n<h2>Summary<\/h2>\n<p>It is time to summarise our tutorial now. If you have gone through this entire tutorial, then you have developed a good understanding of friend functions in C++. We covered each and everything related to C++ friend functions in detail.<\/p>\n<p>We started off this tutorial by taking a simple real-life example and understood the relevance of friend function with Classes and Objects. Now, you\u2019ll not find any difficulties related to this topic. If you still find problems, let us know in the comment section we will help you in the best possible way.<\/p>\n<p><em><strong>Check out this amazing tutorial <a href=\"https:\/\/data-flair.training\/blogs\/polymorphism-in-c-plus-plus\/\">Polymorphism in C++ An Essential Guide<\/a><\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ Friend Function: Where friends have access to your private members It is one of the most popular and important features of classes and objects in C++. Let us understand the concept with a&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":60711,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20172],"tags":[20310,20312,20311],"class_list":["post-60617","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-c-friend-function","tag-c-friend-function-with-example","tag-friend-function-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>2 Ways to Implement Friend Function in C++ (Including Practical Example) - DataFlair<\/title>\n<meta name=\"description\" content=\"Friend function in C++ helps to access the private and protected members of another class in which it is declared with the keyword \u2018friend\u2019. Learn the Syntax, implementation, Example, Charactersitics\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"2 Ways to Implement Friend Function in C++ (Including Practical Example) - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Friend function in C++ helps to access the private and protected members of another class in which it is declared with the keyword \u2018friend\u2019. Learn the Syntax, implementation, Example, Charactersitics\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-26T06:07:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-02-03T11:04:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"427\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"2 Ways to Implement Friend Function in C++ (Including Practical Example) - DataFlair","description":"Friend function in C++ helps to access the private and protected members of another class in which it is declared with the keyword \u2018friend\u2019. Learn the Syntax, implementation, Example, Charactersitics","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"2 Ways to Implement Friend Function in C++ (Including Practical Example) - DataFlair","og_description":"Friend function in C++ helps to access the private and protected members of another class in which it is declared with the keyword \u2018friend\u2019. Learn the Syntax, implementation, Example, Charactersitics","og_url":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-06-26T06:07:12+00:00","article_modified_time":"2020-02-03T11:04:04+00:00","og_image":[{"width":802,"height":427,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"2 Ways to Implement Friend Function in C++ (Including Practical Example)","datePublished":"2019-06-26T06:07:12+00:00","dateModified":"2020-02-03T11:04:04+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/"},"wordCount":692,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg","keywords":["C++ Friend Function","C++ Friend Function with Example","Friend Function tutorial"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/","url":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/","name":"2 Ways to Implement Friend Function in C++ (Including Practical Example) - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg","datePublished":"2019-06-26T06:07:12+00:00","dateModified":"2020-02-03T11:04:04+00:00","description":"Friend function in C++ helps to access the private and protected members of another class in which it is declared with the keyword \u2018friend\u2019. Learn the Syntax, implementation, Example, Charactersitics","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Friend-Function-in-C-.jpg","width":802,"height":427,"caption":"Friend Function in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/friend-function-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C++ Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/cpp\/"},{"@type":"ListItem","position":3,"name":"2 Ways to Implement Friend Function in C++ (Including Practical Example)"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/60617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=60617"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/60617\/revisions"}],"predecessor-version":[{"id":61536,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/60617\/revisions\/61536"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/60711"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=60617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=60617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=60617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}